diff --git a/pom.xml b/pom.xml index f423625..0dd9d3c 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,4 @@ - - + 4.0.0 + org.codehaus.mojo mojo-parent 76 - 4.0.0 - properties-maven-plugin 1.2.0-SNAPSHOT - Properties Maven Plugin - - The Properties Maven Plugin is here to make life a little easier when dealing - with properties. It provides goals to read and write properties from files. - - - 2009 - maven-plugin + Properties Maven Plugin + The Properties Maven Plugin is here to make life a little easier when dealing + with properties. It provides goals to read and write properties from files. + https://www.mojohaus.org/properties-maven-plugin + 2009 + The Apache Software License, Version 2.0 @@ -52,29 +48,6 @@ - - 3.5.4 - 11 - target/staging/${project.artifactId} - 2022-03-19T15:53:05Z - - - - - scm:git:https://github.com/mojohaus/properties-maven-plugin.git - scm:git:ssh://git@github.com/mojohaus/properties-maven-plugin.git - https://github.com/mojohaus/properties-maven-plugin/tree/master - master - - - GitHub - https://github.com/mojohaus/properties-maven-plugin/issues/ - - - GitHub - https://github.com/mojohaus/properties-maven-plugin/actions/ - - arsenalist @@ -94,6 +67,28 @@ + + scm:git:https://github.com/mojohaus/properties-maven-plugin.git + scm:git:ssh://git@github.com/mojohaus/properties-maven-plugin.git + master + https://github.com/mojohaus/properties-maven-plugin/tree/master + + + GitHub + https://github.com/mojohaus/properties-maven-plugin/issues/ + + + GitHub + https://github.com/mojohaus/properties-maven-plugin/actions/ + + + + 3.5.4 + 11 + target/staging/${project.artifactId} + 2022-03-19T15:53:05Z + + org.apache.maven diff --git a/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java b/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java index d1277d9..f2ee3b3 100644 --- a/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/properties/AbstractWritePropertiesMojo.java @@ -19,16 +19,10 @@ * under the License. */ -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Parameter; -import org.apache.maven.project.MavenProject; - +import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringReader; import java.io.StringWriter; @@ -36,19 +30,21 @@ import java.util.Collections; import java.util.List; import java.util.Properties; -import java.io.BufferedReader; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.project.MavenProject; /** * @author Zarar Siddiqi */ -public abstract class AbstractWritePropertiesMojo - extends AbstractMojo -{ +public abstract class AbstractWritePropertiesMojo extends AbstractMojo { - @Parameter( defaultValue = "${project}", required = true, readonly = true ) + @Parameter(defaultValue = "${project}", required = true, readonly = true) private MavenProject project; - @Parameter( required = true, property = "properties.outputFile" ) + @Parameter(required = true, property = "properties.outputFile") private File outputFile; /** @@ -56,51 +52,38 @@ public abstract class AbstractWritePropertiesMojo * @param file {@link File} * @throws MojoExecutionException {@link MojoExecutionException} */ - protected void writeProperties( Properties properties, File file ) - throws MojoExecutionException - { - try - { - storeWithoutTimestamp( properties, file, "Properties" ); - } - catch ( FileNotFoundException e ) - { - getLog().error( "Could not create FileOutputStream: " + file ); - throw new MojoExecutionException( e.getMessage(), e ); - } - catch ( IOException e ) - { - getLog().error( "Error writing properties: " + file ); - throw new MojoExecutionException( e.getMessage(), e ); + protected void writeProperties(Properties properties, File file) throws MojoExecutionException { + try { + storeWithoutTimestamp(properties, file, "Properties"); + } catch (FileNotFoundException e) { + getLog().error("Could not create FileOutputStream: " + file); + throw new MojoExecutionException(e.getMessage(), e); + } catch (IOException e) { + getLog().error("Error writing properties: " + file); + throw new MojoExecutionException(e.getMessage(), e); } } // https://github.com/apache/maven-archiver/blob/master/src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java#L81 - private void storeWithoutTimestamp( Properties properties, File outputFile, String comments ) - throws IOException - { - try ( PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" ); StringWriter sw = new StringWriter() ) - { - properties.store( sw, comments ); + private void storeWithoutTimestamp(Properties properties, File outputFile, String comments) throws IOException { + try (PrintWriter pw = new PrintWriter(outputFile, "ISO-8859-1"); + StringWriter sw = new StringWriter()) { + properties.store(sw, comments); comments = '#' + comments; List lines = new ArrayList<>(); - try ( BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) ) ) - { + try (BufferedReader r = new BufferedReader(new StringReader(sw.toString()))) { String line; - while ( ( line = r.readLine() ) != null ) - { - if ( !line.startsWith( "#" ) || line.equals( comments ) ) - { - lines.add( line ); + while ((line = r.readLine()) != null) { + if (!line.startsWith("#") || line.equals(comments)) { + lines.add(line); } } } - Collections.sort( lines ); - for ( String l : lines ) - { - pw.println( l ); + Collections.sort(lines); + for (String l : lines) { + pw.println(l); } } } @@ -108,16 +91,12 @@ private void storeWithoutTimestamp( Properties properties, File outputFile, Stri /** * @throws MojoExecutionException {@link MojoExecutionException} */ - protected void validateOutputFile() - throws MojoExecutionException - { - if ( outputFile.isDirectory() ) - { - throw new MojoExecutionException( "outputFile must be a file and not a directory" ); + protected void validateOutputFile() throws MojoExecutionException { + if (outputFile.isDirectory()) { + throw new MojoExecutionException("outputFile must be a file and not a directory"); } // ensure path exists - if ( outputFile.getParentFile() != null ) - { + if (outputFile.getParentFile() != null) { outputFile.getParentFile().mkdirs(); } } @@ -125,17 +104,14 @@ protected void validateOutputFile() /** * @return {@link MavenProject} */ - public MavenProject getProject() - { + public MavenProject getProject() { return project; } /** * @return {@link #outputFile} */ - public File getOutputFile() - { + public File getOutputFile() { return outputFile; } - } diff --git a/src/main/java/org/codehaus/mojo/properties/CircularDefinitionPreventer.java b/src/main/java/org/codehaus/mojo/properties/CircularDefinitionPreventer.java index d638a40..a2832c2 100644 --- a/src/main/java/org/codehaus/mojo/properties/CircularDefinitionPreventer.java +++ b/src/main/java/org/codehaus/mojo/properties/CircularDefinitionPreventer.java @@ -1,87 +1,77 @@ -package org.codehaus.mojo.properties; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -class CircularDefinitionPreventer -{ - private static class VisitedProperty - { - private final String key; - - private final String value; - - private VisitedProperty( String key, String value ) - { - this.key = key; - this.value = value; - } - } - - private final List entriesVisited = new LinkedList<>(); - - private final Set keysUsed = new HashSet<>(); - - /** - * @param key The key. - * @param value The values. - * @return {@link CircularDefinitionPreventer} - */ - public CircularDefinitionPreventer visited( String key, String value ) - { - entriesVisited.add( new VisitedProperty( key, value ) ); - if ( keysUsed.contains( key ) && !isValueResolved(value) ) - { - circularDefinition(); - } - else - { - keysUsed.add( key ); - } - - return this; - } - - private void circularDefinition() - { - StringBuilder buffer = new StringBuilder( "Circular property definition: " ); - for ( Iterator iterator = entriesVisited.iterator(); iterator.hasNext(); ) - { - VisitedProperty visited = (VisitedProperty) iterator.next(); - buffer.append( visited.key ).append( "=" ).append( visited.value ); - if ( iterator.hasNext() ) - { - buffer.append( " -> " ); - } - } - throw new IllegalArgumentException( buffer.toString() ); - } - - private boolean isValueResolved(String value) { - int prefixPos = value.indexOf( "${" ); - int suffixPos = value.indexOf( "}" ); - return !(prefixPos >= 0 && suffixPos > prefixPos); - } -} +package org.codehaus.mojo.properties; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +class CircularDefinitionPreventer { + private static class VisitedProperty { + private final String key; + + private final String value; + + private VisitedProperty(String key, String value) { + this.key = key; + this.value = value; + } + } + + private final List entriesVisited = new LinkedList<>(); + + private final Set keysUsed = new HashSet<>(); + + /** + * @param key The key. + * @param value The values. + * @return {@link CircularDefinitionPreventer} + */ + public CircularDefinitionPreventer visited(String key, String value) { + entriesVisited.add(new VisitedProperty(key, value)); + if (keysUsed.contains(key) && !isValueResolved(value)) { + circularDefinition(); + } else { + keysUsed.add(key); + } + + return this; + } + + private void circularDefinition() { + StringBuilder buffer = new StringBuilder("Circular property definition: "); + for (Iterator iterator = entriesVisited.iterator(); iterator.hasNext(); ) { + VisitedProperty visited = (VisitedProperty) iterator.next(); + buffer.append(visited.key).append("=").append(visited.value); + if (iterator.hasNext()) { + buffer.append(" -> "); + } + } + throw new IllegalArgumentException(buffer.toString()); + } + + private boolean isValueResolved(String value) { + int prefixPos = value.indexOf("${"); + int suffixPos = value.indexOf("}"); + return !(prefixPos >= 0 && suffixPos > prefixPos); + } +} diff --git a/src/main/java/org/codehaus/mojo/properties/DefaultValuesAwareExpansionBufferImpl.java b/src/main/java/org/codehaus/mojo/properties/DefaultValuesAwareExpansionBufferImpl.java index 7a7cc22..284aa67 100644 --- a/src/main/java/org/codehaus/mojo/properties/DefaultValuesAwareExpansionBufferImpl.java +++ b/src/main/java/org/codehaus/mojo/properties/DefaultValuesAwareExpansionBufferImpl.java @@ -29,8 +29,7 @@ public DefaultValuesAwareExpansionBufferImpl(String unresolved) { super(unresolved); } - public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() - { + public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() { advanceToNextPrefix(); discardPrefix(); @@ -44,27 +43,23 @@ public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() return new KeyAndDefaultValue(key, defaultValue); } - protected String beforeNextSuffix() - { + protected String beforeNextSuffix() { int defValuePos = unresolved.indexOf(":"); int propertySuffixPos = unresolved.indexOf("}"); - //check default value separator only if before next suffix - if (defValuePos != -1 && propertySuffixPos != -1 && defValuePos < propertySuffixPos) - { - return unresolved.substring( 0, defValuePos ); + // check default value separator only if before next suffix + if (defValuePos != -1 && propertySuffixPos != -1 && defValuePos < propertySuffixPos) { + return unresolved.substring(0, defValuePos); } - return unresolved.substring( 0, propertySuffixPos ); + return unresolved.substring(0, propertySuffixPos); } - private String defaultValue() - { - int defValuePos = unresolved.indexOf( ":" ); - int propertySuffixPos = unresolved.indexOf( "}" ); - if (defValuePos != -1 && propertySuffixPos != -1 && (defValuePos+1) < propertySuffixPos) - { - return unresolved.substring( defValuePos+1, propertySuffixPos ); + private String defaultValue() { + int defValuePos = unresolved.indexOf(":"); + int propertySuffixPos = unresolved.indexOf("}"); + if (defValuePos != -1 && propertySuffixPos != -1 && (defValuePos + 1) < propertySuffixPos) { + return unresolved.substring(defValuePos + 1, propertySuffixPos); } return null; diff --git a/src/main/java/org/codehaus/mojo/properties/ExpansionBuffer.java b/src/main/java/org/codehaus/mojo/properties/ExpansionBuffer.java index c20c5f6..535586c 100644 --- a/src/main/java/org/codehaus/mojo/properties/ExpansionBuffer.java +++ b/src/main/java/org/codehaus/mojo/properties/ExpansionBuffer.java @@ -1,99 +1,84 @@ -package org.codehaus.mojo.properties; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -abstract class ExpansionBuffer -{ - private final StringBuilder resolved = new StringBuilder(); - - protected String unresolved; - - public ExpansionBuffer( String unresolved ) - { - this.unresolved = unresolved != null ? unresolved : ""; - } - - public boolean hasMoreLegalPlaceholders() - { - int prefixPos = unresolved.indexOf( "${" ); - int suffixPos = unresolved.indexOf( "}", prefixPos + 2 ); - return prefixPos >= 0 && suffixPos >= 0; - } - - public abstract KeyAndDefaultValue extractPropertyKeyAndDefaultValue(); - - public String toString() - { - StringBuilder sb = new StringBuilder(resolved); - return sb.append( unresolved ).toString(); - } - - public void add( String newKey, String newValue ) - { - if ( replaced( newValue ) ) - { - expandFurther( newValue ); - } - else - { - skipUnresolvedPlaceholder( newKey ); - } - } - - private boolean replaced( String value ) - { - return value != null; - } - - private void expandFurther( String value ) - { - unresolved = value + unresolved; - } - - private void skipUnresolvedPlaceholder( String newKey ) - { - resolved.append( "${" ).append( newKey ).append( "}" ); - } - - protected void discardToAfterNextSuffix() - { - int propertySuffixPos = unresolved.indexOf( "}" ); - unresolved = unresolved.substring( propertySuffixPos + 1 ); - } - - protected void advanceToNextPrefix() - { - resolved.append( beforePrefix() ); - } - - protected void discardPrefix() - { - int propertyPrefixPos = unresolved.indexOf( "${" ); - unresolved = unresolved.substring( propertyPrefixPos + 2 ); - } - - private String beforePrefix() - { - int propertyPrefixPos = unresolved.indexOf( "${" ); - return unresolved.substring( 0, propertyPrefixPos ); - } - - protected abstract String beforeNextSuffix(); -} +package org.codehaus.mojo.properties; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +abstract class ExpansionBuffer { + private final StringBuilder resolved = new StringBuilder(); + + protected String unresolved; + + protected ExpansionBuffer(String unresolved) { + this.unresolved = unresolved != null ? unresolved : ""; + } + + public boolean hasMoreLegalPlaceholders() { + int prefixPos = unresolved.indexOf("${"); + int suffixPos = unresolved.indexOf("}", prefixPos + 2); + return prefixPos >= 0 && suffixPos >= 0; + } + + public abstract KeyAndDefaultValue extractPropertyKeyAndDefaultValue(); + + public String toString() { + StringBuilder sb = new StringBuilder(resolved); + return sb.append(unresolved).toString(); + } + + public void add(String newKey, String newValue) { + if (replaced(newValue)) { + expandFurther(newValue); + } else { + skipUnresolvedPlaceholder(newKey); + } + } + + private boolean replaced(String value) { + return value != null; + } + + private void expandFurther(String value) { + unresolved = value + unresolved; + } + + private void skipUnresolvedPlaceholder(String newKey) { + resolved.append("${").append(newKey).append("}"); + } + + protected void discardToAfterNextSuffix() { + int propertySuffixPos = unresolved.indexOf("}"); + unresolved = unresolved.substring(propertySuffixPos + 1); + } + + protected void advanceToNextPrefix() { + resolved.append(beforePrefix()); + } + + protected void discardPrefix() { + int propertyPrefixPos = unresolved.indexOf("${"); + unresolved = unresolved.substring(propertyPrefixPos + 2); + } + + private String beforePrefix() { + int propertyPrefixPos = unresolved.indexOf("${"); + return unresolved.substring(0, propertyPrefixPos); + } + + protected abstract String beforeNextSuffix(); +} diff --git a/src/main/java/org/codehaus/mojo/properties/ExpansionBufferImpl.java b/src/main/java/org/codehaus/mojo/properties/ExpansionBufferImpl.java index f337548..9271c8d 100644 --- a/src/main/java/org/codehaus/mojo/properties/ExpansionBufferImpl.java +++ b/src/main/java/org/codehaus/mojo/properties/ExpansionBufferImpl.java @@ -29,8 +29,7 @@ public ExpansionBufferImpl(String unresolved) { super(unresolved); } - public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() - { + public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() { advanceToNextPrefix(); discardPrefix(); @@ -42,9 +41,8 @@ public KeyAndDefaultValue extractPropertyKeyAndDefaultValue() return new KeyAndDefaultValue(key, null); } - protected String beforeNextSuffix() - { - int propertySuffixPos = unresolved.indexOf( "}" ); - return unresolved.substring( 0, propertySuffixPos ); + protected String beforeNextSuffix() { + int propertySuffixPos = unresolved.indexOf("}"); + return unresolved.substring(0, propertySuffixPos); } } diff --git a/src/main/java/org/codehaus/mojo/properties/PropertyResolver.java b/src/main/java/org/codehaus/mojo/properties/PropertyResolver.java index 37e1d3a..22c2cdf 100644 --- a/src/main/java/org/codehaus/mojo/properties/PropertyResolver.java +++ b/src/main/java/org/codehaus/mojo/properties/PropertyResolver.java @@ -1,108 +1,103 @@ -package org.codehaus.mojo.properties; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.util.Properties; - -class PropertyResolver -{ - - /** - * Retrieves a property value, replacing values like ${token} using the Properties to look them up. Shamelessly - * adapted from: - * http://maven.apache.org/plugins/maven-war-plugin/xref/org/apache/maven/plugin/war/PropertyUtils.html It will - * leave unresolved properties alone, trying for System properties, and environment variables and implements - * reparsing (in the case that the value of a property contains a key), and will not loop endlessly on a pair like - * test = ${test} - * - * @param key property key - * @param properties project properties - * @param environment environment variables - * @return resolved property value - * @throws IllegalArgumentException when properties are circularly defined - */ - public String getPropertyValue( String key, Properties properties, Properties environment ) { - return getPropertyValue(key, properties, environment, false); - } - - /** - * Same as the previous method. Accepts an extra flag to indicate whether default values should be - * processed within property placeholders or not. - * - * @param key property key - * @param properties project properties - * @param environment environment variables - * @param useDefaultValues process default values flag - * @return resolved property value - * @throws IllegalArgumentException when properties are circularly defined - */ - public String getPropertyValue( String key, Properties properties, Properties environment, boolean useDefaultValues ) - { - String value = properties.getProperty( key ); - - ExpansionBuffer buffer; - if ( useDefaultValues ) { - buffer = new DefaultValuesAwareExpansionBufferImpl(value); - } else { - buffer = new ExpansionBufferImpl( value ); - } - - CircularDefinitionPreventer circularDefinitionPreventer = - new CircularDefinitionPreventer().visited( key, value ); - - while ( buffer.hasMoreLegalPlaceholders() ) - { - KeyAndDefaultValue kv = buffer.extractPropertyKeyAndDefaultValue(); - String newKey = kv.getKey(); - String newValue = fromPropertiesThenSystemThenEnvironment( newKey, kv.getDefaultValue(), properties, environment ); - - circularDefinitionPreventer.visited( newKey, newValue ); - - buffer.add( newKey, newValue ); - } - - return buffer.toString(); - } - - private String fromPropertiesThenSystemThenEnvironment( String key, String defaultValue, Properties properties, Properties environment ) - { - String value = properties.getProperty( key ); - - // try global environment - if ( value == null ) - { - value = System.getProperty( key ); - } - - // try environment variable - if ( value == null && key.startsWith( "env." ) && environment != null ) - { - value = environment.getProperty( key.substring( 4 ) ); - } - - // try default value - if ( value == null ) - { - value = defaultValue; - } - - return value; - } -} +package org.codehaus.mojo.properties; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Properties; + +class PropertyResolver { + + /** + * Retrieves a property value, replacing values like ${token} using the Properties to look them up. Shamelessly + * adapted from: + * http://maven.apache.org/plugins/maven-war-plugin/xref/org/apache/maven/plugin/war/PropertyUtils.html It will + * leave unresolved properties alone, trying for System properties, and environment variables and implements + * reparsing (in the case that the value of a property contains a key), and will not loop endlessly on a pair like + * test = ${test} + * + * @param key property key + * @param properties project properties + * @param environment environment variables + * @return resolved property value + * @throws IllegalArgumentException when properties are circularly defined + */ + public String getPropertyValue(String key, Properties properties, Properties environment) { + return getPropertyValue(key, properties, environment, false); + } + + /** + * Same as the previous method. Accepts an extra flag to indicate whether default values should be + * processed within property placeholders or not. + * + * @param key property key + * @param properties project properties + * @param environment environment variables + * @param useDefaultValues process default values flag + * @return resolved property value + * @throws IllegalArgumentException when properties are circularly defined + */ + public String getPropertyValue( + String key, Properties properties, Properties environment, boolean useDefaultValues) { + String value = properties.getProperty(key); + + ExpansionBuffer buffer; + if (useDefaultValues) { + buffer = new DefaultValuesAwareExpansionBufferImpl(value); + } else { + buffer = new ExpansionBufferImpl(value); + } + + CircularDefinitionPreventer circularDefinitionPreventer = new CircularDefinitionPreventer().visited(key, value); + + while (buffer.hasMoreLegalPlaceholders()) { + KeyAndDefaultValue kv = buffer.extractPropertyKeyAndDefaultValue(); + String newKey = kv.getKey(); + String newValue = + fromPropertiesThenSystemThenEnvironment(newKey, kv.getDefaultValue(), properties, environment); + + circularDefinitionPreventer.visited(newKey, newValue); + + buffer.add(newKey, newValue); + } + + return buffer.toString(); + } + + private String fromPropertiesThenSystemThenEnvironment( + String key, String defaultValue, Properties properties, Properties environment) { + String value = properties.getProperty(key); + + // try global environment + if (value == null) { + value = System.getProperty(key); + } + + // try environment variable + if (value == null && key.startsWith("env.") && environment != null) { + value = environment.getProperty(key.substring(4)); + } + + // try default value + if (value == null) { + value = defaultValue; + } + + return value; + } +} diff --git a/src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java b/src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java index 714e912..3736bbd 100644 --- a/src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/properties/ReadPropertiesMojo.java @@ -2,20 +2,20 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, + * + * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations * under the License. */ @@ -46,11 +46,9 @@ * @author Zarar Siddiqi * @author Krystian Nowak */ -@Mojo( name = "read-project-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true ) -public class ReadPropertiesMojo - extends AbstractMojo -{ - @Parameter( defaultValue = "${project}", readonly = true, required = true ) +@Mojo(name = "read-project-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true) +public class ReadPropertiesMojo extends AbstractMojo { + @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject project; /** @@ -62,16 +60,12 @@ public class ReadPropertiesMojo /** * @param files The files to set for tests. */ - public void setFiles( File[] files ) - { - if ( files == null ) - { + public void setFiles(File[] files) { + if (files == null) { this.files = new File[0]; - } - else - { + } else { this.files = new File[files.length]; - System.arraycopy( files, 0, this.files, 0, files.length ); + System.arraycopy(files, 0, this.files, 0, files.length); } } @@ -88,23 +82,19 @@ public void setFiles( File[] files ) * * @param urls The URLs to set for tests. */ - public void setUrls( String[] urls ) - { - if ( urls == null ) - { + public void setUrls(String[] urls) { + if (urls == null) { this.urls = null; - } - else - { + } else { this.urls = new String[urls.length]; - System.arraycopy( urls, 0, this.urls, 0, urls.length ); + System.arraycopy(urls, 0, this.urls, 0, urls.length); } } /** * If the plugin should be quiet if any of the files was not found */ - @Parameter( defaultValue = "false" ) + @Parameter(defaultValue = "false") private boolean quiet; /** @@ -114,12 +104,11 @@ public void setUrls( String[] urls ) @Parameter private String keyPrefix = null; - public void setKeyPrefix( String keyPrefix ) - { + public void setKeyPrefix(String keyPrefix) { this.keyPrefix = keyPrefix; } - @Parameter( defaultValue = "false", property = "prop.skipLoadProperties" ) + @Parameter(defaultValue = "false", property = "prop.skipLoadProperties") private boolean skipLoadProperties; /** @@ -127,7 +116,7 @@ public void setKeyPrefix( String keyPrefix ) * * @parameter default-value="false" */ - @Parameter( defaultValue = "false" ) + @Parameter(defaultValue = "false") private boolean useDefaultValues; /** @@ -135,11 +124,10 @@ public void setKeyPrefix( String keyPrefix ) * * @since 1.2.0 */ - @Parameter( defaultValue = "true" ) + @Parameter(defaultValue = "true") private boolean override = true; - public void setOverride( boolean override ) - { + public void setOverride(boolean override) { this.override = override; } @@ -149,165 +137,116 @@ public void setOverride( boolean override ) private final PropertyResolver resolver = new PropertyResolver(); /** {@inheritDoc} */ - public void execute() - throws MojoExecutionException, MojoFailureException - { - if ( !skipLoadProperties ) - { + public void execute() throws MojoExecutionException, MojoFailureException { + if (!skipLoadProperties) { checkParameters(); loadFiles(); loadUrls(); resolveProperties(); - } - else - { - getLog().warn( "The properties are ignored" ); + } else { + getLog().warn("The properties are ignored"); } } - private void checkParameters() - throws MojoExecutionException - { - if ( files.length > 0 && urls.length > 0 ) - { - throw new MojoExecutionException( "Set files or URLs but not both - otherwise " - + "no order of precedence can be guaranteed" ); + private void checkParameters() throws MojoExecutionException { + if (files.length > 0 && urls.length > 0) { + throw new MojoExecutionException( + "Set files or URLs but not both - otherwise " + "no order of precedence can be guaranteed"); } } - private void loadFiles() - throws MojoExecutionException - { - for ( File file : files ) - { - load( new FileResource( file ) ); + private void loadFiles() throws MojoExecutionException { + for (File file : files) { + load(new FileResource(file)); } } - private void loadUrls() - throws MojoExecutionException - { - for ( String url : urls ) - { - load( new UrlResource( url ) ); + private void loadUrls() throws MojoExecutionException { + for (String url : urls) { + load(new UrlResource(url)); } } - private void load( Resource resource ) - throws MojoExecutionException - { - if ( resource.canBeOpened() ) - { - loadProperties( resource ); - } - else - { - missing( resource ); + private void load(Resource resource) throws MojoExecutionException { + if (resource.canBeOpened()) { + loadProperties(resource); + } else { + missing(resource); } } - private void loadProperties( Resource resource ) - throws MojoExecutionException - { - try - { - getLog().debug( "Loading properties from " + resource ); + private void loadProperties(Resource resource) throws MojoExecutionException { + try { + getLog().debug("Loading properties from " + resource); - try ( InputStream stream = resource.getInputStream() ) - { + try (InputStream stream = resource.getInputStream()) { String effectivePrefix = ""; - if ( keyPrefix != null ) - { + if (keyPrefix != null) { effectivePrefix = keyPrefix; } Properties properties = new Properties(); - properties.load( stream ); + properties.load(stream); Properties projectProperties = project.getProperties(); - for( String key: properties.stringPropertyNames() ) - { + for (String key : properties.stringPropertyNames()) { String propertyName = effectivePrefix + key; - if ( override || !projectProperties.containsKey( propertyName ) ) - { - projectProperties.put( propertyName, properties.get( key ) ); + if (override || !projectProperties.containsKey(propertyName)) { + projectProperties.put(propertyName, properties.get(key)); } } } - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Error reading properties from " + resource, e ); + } catch (IOException e) { + throw new MojoExecutionException("Error reading properties from " + resource, e); } } - private void missing( Resource resource ) - throws MojoExecutionException - { - if ( quiet ) - { - getLog().info( "Quiet processing - ignoring properties cannot be loaded from " + resource ); - } - else - { - throw new MojoExecutionException( "Properties could not be loaded from " + resource ); + private void missing(Resource resource) throws MojoExecutionException { + if (quiet) { + getLog().info("Quiet processing - ignoring properties cannot be loaded from " + resource); + } else { + throw new MojoExecutionException("Properties could not be loaded from " + resource); } } - private void resolveProperties() - throws MojoExecutionException, MojoFailureException - { + private void resolveProperties() throws MojoExecutionException, MojoFailureException { Properties environment = loadSystemEnvironmentPropertiesWhenDefined(); Properties projectProperties = project.getProperties(); - for ( Enumeration n = projectProperties.propertyNames(); n.hasMoreElements(); ) - { + for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements(); ) { String k = (String) n.nextElement(); - projectProperties.setProperty( k, getPropertyValue( k, projectProperties, environment ) ); + projectProperties.setProperty(k, getPropertyValue(k, projectProperties, environment)); } } - private Properties loadSystemEnvironmentPropertiesWhenDefined() - throws MojoExecutionException - { + private Properties loadSystemEnvironmentPropertiesWhenDefined() throws MojoExecutionException { Properties projectProperties = project.getProperties(); boolean useEnvVariables = false; - for ( Enumeration n = projectProperties.propertyNames(); n.hasMoreElements(); ) - { + for (Enumeration n = projectProperties.propertyNames(); n.hasMoreElements(); ) { String k = (String) n.nextElement(); - String p = (String) projectProperties.get( k ); - if ( p.contains( "${env." ) ) - { + String p = (String) projectProperties.get(k); + if (p.contains("${env.")) { useEnvVariables = true; break; } } Properties environment = null; - if ( useEnvVariables ) - { - try - { + if (useEnvVariables) { + try { environment = getSystemEnvVars(); - } - catch ( IOException e ) - { - throw new MojoExecutionException( "Error getting system environment variables: ", e ); + } catch (IOException e) { + throw new MojoExecutionException("Error getting system environment variables: ", e); } } return environment; } - private String getPropertyValue( String k, Properties p, Properties environment ) - throws MojoFailureException - { - try - { - return resolver.getPropertyValue( k, p, environment , useDefaultValues); - } - catch ( IllegalArgumentException e ) - { - throw new MojoFailureException( e.getMessage() ); + private String getPropertyValue(String k, Properties p, Properties environment) throws MojoFailureException { + try { + return resolver.getPropertyValue(k, p, environment, useDefaultValues); + } catch (IllegalArgumentException e) { + throw new MojoFailureException(e.getMessage()); } } @@ -317,9 +256,7 @@ private String getPropertyValue( String k, Properties p, Properties environment * @return The shell environment variables, can be empty but never null. * @throws IOException If the environment variables could not be queried from the shell. */ - Properties getSystemEnvVars() - throws IOException - { + Properties getSystemEnvVars() throws IOException { return CommandLineUtils.getSystemEnvVars(); } @@ -328,8 +265,7 @@ Properties getSystemEnvVars() * * @param quiet Set to true if missing files can be skipped. */ - void setQuiet( boolean quiet ) - { + void setQuiet(boolean quiet) { this.quiet = quiet; } @@ -337,16 +273,14 @@ void setQuiet( boolean quiet ) * * @param skipLoadProperties Set to true if you don't want to load properties. */ - void setSkipLoadProperties( boolean skipLoadProperties ) - { + void setSkipLoadProperties(boolean skipLoadProperties) { this.skipLoadProperties = skipLoadProperties; } /** * @param useDefaultValues set to true if default values need to be processed within property placeholders */ - public void setUseDefaultValues(boolean useDefaultValues) - { + public void setUseDefaultValues(boolean useDefaultValues) { this.useDefaultValues = useDefaultValues; } @@ -355,8 +289,7 @@ public void setUseDefaultValues(boolean useDefaultValues) * * @param project The test project. */ - void setProject( MavenProject project ) - { + void setProject(MavenProject project) { this.project = project; } @@ -368,56 +301,42 @@ public MavenProject getProject() { return project; } - private static abstract class Resource - { + private abstract static class Resource { private InputStream stream; public abstract boolean canBeOpened(); - protected abstract InputStream openStream() - throws IOException; + protected abstract InputStream openStream() throws IOException; - public InputStream getInputStream() - throws IOException - { - if ( stream == null ) - { + public InputStream getInputStream() throws IOException { + if (stream == null) { stream = openStream(); } return stream; } } - private static class FileResource - extends Resource - { + private static class FileResource extends Resource { private final File file; - public FileResource( File file ) - { + FileResource(File file) { this.file = file; } - public boolean canBeOpened() - { + public boolean canBeOpened() { return file.exists(); } - protected InputStream openStream() - throws IOException - { - return new BufferedInputStream( new FileInputStream( file ) ); + protected InputStream openStream() throws IOException { + return new BufferedInputStream(new FileInputStream(file)); } - public String toString() - { + public String toString() { return "File: " + file; } } - private static class UrlResource - extends Resource - { + private static class UrlResource extends Resource { private static final String CLASSPATH_PREFIX = "classpath:"; private static final String SLASH_PREFIX = "/"; @@ -428,63 +347,44 @@ private static class UrlResource private String classpathUrl; - public UrlResource( String url ) - throws MojoExecutionException - { - if ( url.startsWith( CLASSPATH_PREFIX ) ) - { - String resource = url.substring( CLASSPATH_PREFIX.length() ); - if ( resource.startsWith( SLASH_PREFIX ) ) - { - resource = resource.substring( 1 ); + UrlResource(String url) throws MojoExecutionException { + if (url.startsWith(CLASSPATH_PREFIX)) { + String resource = url.substring(CLASSPATH_PREFIX.length()); + if (resource.startsWith(SLASH_PREFIX)) { + resource = resource.substring(1); } - this.url = getClass().getClassLoader().getResource( resource ); - if ( this.url == null ) - { + this.url = getClass().getClassLoader().getResource(resource); + if (this.url == null) { isMissingClasspathResouce = true; classpathUrl = url; } - } - else - { - try - { - this.url = new URL( url ); - } - catch ( MalformedURLException e ) - { - throw new MojoExecutionException( "Badly formed URL " + url + " - " + e.getMessage() ); + } else { + try { + this.url = new URL(url); + } catch (MalformedURLException e) { + throw new MojoExecutionException("Badly formed URL " + url + " - " + e.getMessage()); } } } - public boolean canBeOpened() - { - if ( isMissingClasspathResouce ) - { + public boolean canBeOpened() { + if (isMissingClasspathResouce) { return false; } - try - { + try { openStream(); - } - catch ( IOException e ) - { + } catch (IOException e) { return false; } return true; } - protected InputStream openStream() - throws IOException - { - return new BufferedInputStream( url.openStream() ); + protected InputStream openStream() throws IOException { + return new BufferedInputStream(url.openStream()); } - public String toString() - { - if ( !isMissingClasspathResouce ) - { + public String toString() { + if (!isMissingClasspathResouce) { return "URL " + url.toString(); } return classpathUrl; diff --git a/src/main/java/org/codehaus/mojo/properties/SetSystemPropertiesMojo.java b/src/main/java/org/codehaus/mojo/properties/SetSystemPropertiesMojo.java index a19fa27..9d6b945 100644 --- a/src/main/java/org/codehaus/mojo/properties/SetSystemPropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/properties/SetSystemPropertiesMojo.java @@ -2,20 +2,20 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, + * + * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations * under the License. */ @@ -23,27 +23,23 @@ import java.util.Properties; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; /** * Sets system properties. - * + * * @author Mark Hobson */ -@Mojo( name = "set-system-properties", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true ) -public class SetSystemPropertiesMojo - extends AbstractMojo -{ +@Mojo(name = "set-system-properties", defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) +public class SetSystemPropertiesMojo extends AbstractMojo { // fields ----------------------------------------------------------------- /** * The system properties to set. */ - @Parameter( required = true ) + @Parameter(required = true) private Properties properties; // Mojo methods ----------------------------------------------------------- @@ -51,29 +47,26 @@ public class SetSystemPropertiesMojo /** * {@inheritDoc} */ - public void execute() - { - if ( properties.isEmpty() ) - { - getLog().debug( "No system properties found" ); + public void execute() { + if (properties.isEmpty()) { + getLog().debug("No system properties found"); return; } - getLog().debug( "Setting system properties:" ); + getLog().debug("Setting system properties:"); - for ( Enumeration propertyNames = properties.propertyNames(); propertyNames.hasMoreElements(); ) - { + for (Enumeration propertyNames = properties.propertyNames(); propertyNames.hasMoreElements(); ) { String propertyName = propertyNames.nextElement().toString(); - String propertyValue = properties.getProperty( propertyName ); + String propertyValue = properties.getProperty(propertyName); - getLog().debug( "- " + propertyName + " = " + propertyValue ); + getLog().debug("- " + propertyName + " = " + propertyValue); - System.setProperty( propertyName, propertyValue ); + System.setProperty(propertyName, propertyValue); } int count = properties.size(); - getLog().info( "Set " + count + " system " + ( count > 1 ? "properties" : "property" ) ); + getLog().info("Set " + count + " system " + (count > 1 ? "properties" : "property")); } } diff --git a/src/main/java/org/codehaus/mojo/properties/WriteActiveProfileProperties.java b/src/main/java/org/codehaus/mojo/properties/WriteActiveProfileProperties.java index d1f75f8..812194b 100644 --- a/src/main/java/org/codehaus/mojo/properties/WriteActiveProfileProperties.java +++ b/src/main/java/org/codehaus/mojo/properties/WriteActiveProfileProperties.java @@ -2,59 +2,52 @@ /* * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file + * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, + * + * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations * under the License. */ +import java.util.List; +import java.util.Properties; + import org.apache.maven.model.Profile; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; -import java.util.List; -import java.util.Properties; - /** * Writes properties of all active profiles to a file. * * @author Zarar Siddiqi */ -@Mojo( name = "write-active-profile-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true ) -public class WriteActiveProfileProperties - extends AbstractWritePropertiesMojo -{ +@Mojo(name = "write-active-profile-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true) +public class WriteActiveProfileProperties extends AbstractWritePropertiesMojo { /** {@inheritDoc} */ - public void execute() - throws MojoExecutionException - { + public void execute() throws MojoExecutionException { validateOutputFile(); List list = getProject().getActiveProfiles(); - if ( getLog().isInfoEnabled() ) - { - getLog().debug( list.size() + " profile(s) active" ); + if (getLog().isInfoEnabled()) { + getLog().debug(list.size() + " profile(s) active"); } Properties properties = new Properties(); - for ( Profile profile : list ) - { - if ( profile.getProperties() != null ) - { - properties.putAll( profile.getProperties() ); + for (Profile profile : list) { + if (profile.getProperties() != null) { + properties.putAll(profile.getProperties()); } } - writeProperties( properties, getOutputFile() ); + writeProperties(properties, getOutputFile()); } } diff --git a/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java b/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java index 2bfff15..9f093dc 100644 --- a/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java +++ b/src/main/java/org/codehaus/mojo/properties/WriteProjectProperties.java @@ -20,26 +20,6 @@ */ import java.util.Enumeration; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - import java.util.Properties; import org.apache.maven.plugin.MojoExecutionException; @@ -51,33 +31,26 @@ * * @author Zarar Siddiqi */ -@Mojo( name = "write-project-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true ) -public class WriteProjectProperties - extends AbstractWritePropertiesMojo -{ +@Mojo(name = "write-project-properties", defaultPhase = LifecyclePhase.NONE, threadSafe = true) +public class WriteProjectProperties extends AbstractWritePropertiesMojo { /** {@inheritDoc} */ - public void execute() - throws MojoExecutionException - { + public void execute() throws MojoExecutionException { validateOutputFile(); Properties projProperties = new Properties(); - projProperties.putAll( getProject().getProperties() ); + projProperties.putAll(getProject().getProperties()); Properties systemProperties = System.getProperties(); // allow system properties to over write key/value found in maven properties Enumeration enumeration = systemProperties.keys(); - while ( enumeration.hasMoreElements() ) - { + while (enumeration.hasMoreElements()) { String key = (String) enumeration.nextElement(); - String value = systemProperties.getProperty( key ); - if ( projProperties.get( key ) != null ) - { - projProperties.put( key, value ); + String value = systemProperties.getProperty(key); + if (projProperties.get(key) != null) { + projProperties.put(key, value); } - } - writeProperties( projProperties, getOutputFile() ); + writeProperties(projProperties, getOutputFile()); } } diff --git a/src/test/java/org/codehaus/mojo/properties/PropertyResolverTest.java b/src/test/java/org/codehaus/mojo/properties/PropertyResolverTest.java index bc7ac06..2481bf4 100644 --- a/src/test/java/org/codehaus/mojo/properties/PropertyResolverTest.java +++ b/src/test/java/org/codehaus/mojo/properties/PropertyResolverTest.java @@ -1,314 +1,290 @@ -package org.codehaus.mojo.properties; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import static org.hamcrest.CoreMatchers.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; -import org.junit.Test; -import org.apache.maven.plugin.MojoFailureException; -import java.util.Properties; - -/** - * Tests the support class that produces concrete values from a set of properties. - */ -public class PropertyResolverTest -{ - private final PropertyResolver resolver = new PropertyResolver(); - - @Test - public void validPlaceholderIsResolved() - { - Properties properties = new Properties(); - properties.setProperty( "p1", "${p2}" ); - properties.setProperty( "p2", "value" ); - - String value1 = resolver.getPropertyValue( "p1", properties, new Properties() ); - String value2 = resolver.getPropertyValue( "p2", properties, new Properties() ); - - assertEquals( "value", value1 ); - assertEquals( "value", value2 ); - } - - @Test - public void unknownPlaceholderIsLeftAsIs() - { - Properties properties = new Properties(); - properties.setProperty( "p1", "${p2}" ); - properties.setProperty( "p2", "value" ); - properties.setProperty( "p3", "${unknown}" ); - - String value1 = resolver.getPropertyValue( "p1", properties, new Properties() ); - String value2 = resolver.getPropertyValue( "p2", properties, new Properties() ); - String value3 = resolver.getPropertyValue( "p3", properties, new Properties() ); - - assertEquals( "value", value1 ); - assertEquals( "value", value2 ); - assertEquals( "${unknown}", value3 ); - } - - @Test - public void multipleValuesAreResolved() - { - Properties properties = new Properties(); - properties.setProperty( "hostname", "localhost" ); - properties.setProperty( "port", "8080" ); - properties.setProperty( "base.url", "http://${hostname}:${port}/" ); - - String value = resolver.getPropertyValue( "base.url", properties, new Properties() ); - - assertEquals( "http://localhost:8080/", value ); - } - - @Test - public void propertyIncludesAnotherPropertyMoreThanOnce() - throws MojoFailureException - { - Properties properties = new Properties(); - properties.setProperty( "p1", "value" ); - properties.setProperty( "p2", "${p1} ${p1}" ); - - String value = resolver.getPropertyValue( "p2", properties, new Properties() ); - - assertEquals( "value value", value ); - } - - @Test - public void malformedPlaceholderIsLeftAsIs() - { - Properties properties = new Properties(); - properties.setProperty( "p1", "${p2}" ); - properties.setProperty( "p2", "value" ); - properties.setProperty( "p4", "${malformed" ); - - String value1 = resolver.getPropertyValue( "p1", properties, new Properties() ); - String value2 = resolver.getPropertyValue( "p2", properties, new Properties() ); - String value4 = resolver.getPropertyValue( "p4", properties, new Properties() ); - - assertEquals( "value", value1 ); - assertEquals( "value", value2 ); - assertEquals( "${malformed", value4 ); - } - - @Test - public void propertyDefinedAsItselfIsIllegal() - { - Properties properties = new Properties(); - properties.setProperty( "p1", "${p2}" ); - properties.setProperty( "p2", "value" ); - properties.setProperty( "p5", "${p5}" ); - properties.setProperty( "p6", "${p7}" ); - properties.setProperty( "p7", "${p6}" ); - - String value1 = resolver.getPropertyValue( "p1", properties, new Properties() ); - String value2 = resolver.getPropertyValue( "p2", properties, new Properties() ); - String value5 = null; - try - { - value5 = resolver.getPropertyValue( "p5", properties, new Properties() ); - fail(); - } - catch ( IllegalArgumentException e ) - { - assertThat( e.getMessage(), containsString( "p5" ) ); - } - String value6 = null; - try - { - value6 = resolver.getPropertyValue( "p6", properties, new Properties() ); - fail(); - } - catch ( IllegalArgumentException e ) - { - assertThat( e.getMessage(), containsString( "p7" ) ); - } - - assertEquals( "value", value1 ); - assertEquals( "value", value2 ); - assertNull( value5 ); - assertNull( value6 ); - } - - @Test - public void circularReferenceIsIllegal() - throws MojoFailureException - { - Properties properties = new Properties(); - properties.setProperty( "p1", "${p2}" ); - properties.setProperty( "p2", "${p1}}" ); - - String value = null; - try { - value = resolver.getPropertyValue( "p2", properties, new Properties() ); - } catch (IllegalArgumentException e) { - assertThat( e.getMessage(), containsString("p1")); - assertThat( e.getMessage(), containsString("p2")); - } - - assertNull(value); - } - - @Test - public void valueIsObtainedFromSystemProperty() - { - Properties saved = System.getProperties(); - System.setProperty( "system.property", "system.value" ); - - Properties properties = new Properties(); - properties.setProperty( "p1", "${system.property}" ); - - String value = resolver.getPropertyValue( "p1", properties, new Properties() ); - - try - { - assertEquals( "system.value", value ); - } - finally - { - System.setProperties( saved ); - } - } - - @Test - public void valueIsObtainedFromEnvironmentProperty() - { - Properties environment = new Properties(); - environment.setProperty( "PROPERTY", "env.value" ); - - Properties properties = new Properties(); - properties.setProperty( "p1", "${env.PROPERTY}" ); - - String value = resolver.getPropertyValue( "p1", properties, environment ); - - assertEquals( "env.value", value ); - } - - @Test - public void missingPropertyIsTolerated() - { - assertEquals( "", resolver.getPropertyValue( "non-existent", new Properties(), null ) ); - } - - public void testDefaultValueForUnresolvedPropertyWithEnabledFlag() - { - Properties properties = new Properties(); - properties.setProperty("p1", "${unknown:}"); - properties.setProperty("p2", "${unknown:defaultValue}"); - properties.setProperty("p3", "http://${uhost:localhost}:${uport:8080}"); - properties.setProperty("p4", "http://${host:localhost}:${port:8080}"); - properties.setProperty("p5", "${unknown:${fallback}}"); - properties.setProperty("p6", "${unknown:${double.unknown}}"); - properties.setProperty("p7", "${unknown:with space}"); - properties.setProperty("p8", "${unknown:with extra :}"); - properties.setProperty("p9", "${malformed:defVal"); - properties.setProperty("p10", "${malformed:with space"); - properties.setProperty("p11", "${malformed:with extra :"); - properties.setProperty("p12", "${unknown::}"); - properties.setProperty("p13", "${unknown: }"); - - properties.setProperty("host", "example.com"); - properties.setProperty("port", "9090"); - properties.setProperty("fallback", "fallback value"); - - - String value1 = resolver.getPropertyValue("p1", properties, new Properties(), true); - String value2 = resolver.getPropertyValue("p2", properties, new Properties(), true); - String value3 = resolver.getPropertyValue("p3", properties, new Properties(), true); - String value4 = resolver.getPropertyValue("p4", properties, new Properties(), true); - String value5 = resolver.getPropertyValue("p5", properties, new Properties(), true); - String value6 = resolver.getPropertyValue("p6", properties, new Properties(), true); - String value7 = resolver.getPropertyValue("p7", properties, new Properties(), true); - String value8 = resolver.getPropertyValue("p8", properties, new Properties(), true); - String value9 = resolver.getPropertyValue("p9", properties, new Properties(), true); - String value10 = resolver.getPropertyValue("p10", properties, new Properties(), true); - String value11 = resolver.getPropertyValue("p11", properties, new Properties(), true); - String value12 = resolver.getPropertyValue("p12", properties, new Properties(), true); - String value13 = resolver.getPropertyValue("p13", properties, new Properties(), true); - - assertEquals("${unknown}", value1); - assertEquals("defaultValue", value2); - assertEquals("http://localhost:8080", value3); - assertEquals("http://example.com:9090", value4); - assertEquals("fallback value", value5); - assertEquals("${double.unknown}", value6); - assertEquals("with space", value7); - assertEquals("with extra :", value8); - assertEquals("${malformed:defVal", value9); - assertEquals("${malformed:with space", value10); - assertEquals("${malformed:with extra :", value11); - assertEquals(":", value12); - assertEquals(" ", value13); - } - - /** - * with the flag disabled (default behavior) nothing gets replaced - * ':' is treated as a regular character and part of the property name - */ - public void testDefaultValueForUnresolvedPropertyWithDisabledFlag() - { - Properties properties = new Properties(); - properties.setProperty("p1", "${unknown:}"); - properties.setProperty("p2", "${unknown:defaultValue}"); - properties.setProperty("p3", "http://${uhost:localhost}:${uport:8080}"); - properties.setProperty("p4", "http://${host:localhost}:${port:8080}"); - properties.setProperty("p5", "${unknown:${fallback}}"); - properties.setProperty("p6", "${unknown:${double.unknown}}"); - properties.setProperty("p7", "${unknown:with space}"); - properties.setProperty("p8", "${unknown:with extra :}"); - properties.setProperty("p9", "${malformed:defVal"); - properties.setProperty("p10", "${malformed:with space"); - properties.setProperty("p11", "${malformed:with extra :"); - properties.setProperty("p12", "${unknown::}"); - properties.setProperty("p13", "${unknown: }"); - - properties.setProperty("host", "example.com"); - properties.setProperty("port", "9090"); - properties.setProperty("fallback", "fallback value"); - - - String value1 = resolver.getPropertyValue("p1", properties, new Properties()); - String value2 = resolver.getPropertyValue("p2", properties, new Properties()); - String value3 = resolver.getPropertyValue("p3", properties, new Properties()); - String value4 = resolver.getPropertyValue("p4", properties, new Properties()); - String value5 = resolver.getPropertyValue("p5", properties, new Properties()); - String value6 = resolver.getPropertyValue("p6", properties, new Properties()); - String value7 = resolver.getPropertyValue("p7", properties, new Properties()); - String value8 = resolver.getPropertyValue("p8", properties, new Properties()); - String value9 = resolver.getPropertyValue("p9", properties, new Properties()); - String value10 = resolver.getPropertyValue("p10", properties, new Properties()); - String value11 = resolver.getPropertyValue("p11", properties, new Properties()); - String value12 = resolver.getPropertyValue("p12", properties, new Properties()); - String value13 = resolver.getPropertyValue("p13", properties, new Properties()); - - assertEquals("${unknown:}", value1); - assertEquals("${unknown:defaultValue}", value2); - assertEquals("http://${uhost:localhost}:${uport:8080}", value3); - assertEquals("http://${host:localhost}:${port:8080}", value4); - assertEquals("${unknown:${fallback}}", value5); - assertEquals("${unknown:${double.unknown}}", value6); - assertEquals("${unknown:with space}", value7); - assertEquals("${unknown:with extra :}", value8); - assertEquals("${malformed:defVal", value9); - assertEquals("${malformed:with space", value10); - assertEquals("${malformed:with extra :", value11); - assertEquals("${unknown::}", value12); - assertEquals("${unknown: }", value13); - } -} +package org.codehaus.mojo.properties; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.util.Properties; + +import org.apache.maven.plugin.MojoFailureException; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.*; + +/** + * Tests the support class that produces concrete values from a set of properties. + */ +public class PropertyResolverTest { + private final PropertyResolver resolver = new PropertyResolver(); + + @Test + public void validPlaceholderIsResolved() { + Properties properties = new Properties(); + properties.setProperty("p1", "${p2}"); + properties.setProperty("p2", "value"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties()); + String value2 = resolver.getPropertyValue("p2", properties, new Properties()); + + assertEquals("value", value1); + assertEquals("value", value2); + } + + @Test + public void unknownPlaceholderIsLeftAsIs() { + Properties properties = new Properties(); + properties.setProperty("p1", "${p2}"); + properties.setProperty("p2", "value"); + properties.setProperty("p3", "${unknown}"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties()); + String value2 = resolver.getPropertyValue("p2", properties, new Properties()); + String value3 = resolver.getPropertyValue("p3", properties, new Properties()); + + assertEquals("value", value1); + assertEquals("value", value2); + assertEquals("${unknown}", value3); + } + + @Test + public void multipleValuesAreResolved() { + Properties properties = new Properties(); + properties.setProperty("hostname", "localhost"); + properties.setProperty("port", "8080"); + properties.setProperty("base.url", "http://${hostname}:${port}/"); + + String value = resolver.getPropertyValue("base.url", properties, new Properties()); + + assertEquals("http://localhost:8080/", value); + } + + @Test + public void propertyIncludesAnotherPropertyMoreThanOnce() throws MojoFailureException { + Properties properties = new Properties(); + properties.setProperty("p1", "value"); + properties.setProperty("p2", "${p1} ${p1}"); + + String value = resolver.getPropertyValue("p2", properties, new Properties()); + + assertEquals("value value", value); + } + + @Test + public void malformedPlaceholderIsLeftAsIs() { + Properties properties = new Properties(); + properties.setProperty("p1", "${p2}"); + properties.setProperty("p2", "value"); + properties.setProperty("p4", "${malformed"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties()); + String value2 = resolver.getPropertyValue("p2", properties, new Properties()); + String value4 = resolver.getPropertyValue("p4", properties, new Properties()); + + assertEquals("value", value1); + assertEquals("value", value2); + assertEquals("${malformed", value4); + } + + @Test + public void propertyDefinedAsItselfIsIllegal() { + Properties properties = new Properties(); + properties.setProperty("p1", "${p2}"); + properties.setProperty("p2", "value"); + properties.setProperty("p5", "${p5}"); + properties.setProperty("p6", "${p7}"); + properties.setProperty("p7", "${p6}"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties()); + String value2 = resolver.getPropertyValue("p2", properties, new Properties()); + String value5 = null; + try { + value5 = resolver.getPropertyValue("p5", properties, new Properties()); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("p5")); + } + String value6 = null; + try { + value6 = resolver.getPropertyValue("p6", properties, new Properties()); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("p7")); + } + + assertEquals("value", value1); + assertEquals("value", value2); + assertNull(value5); + assertNull(value6); + } + + @Test + public void circularReferenceIsIllegal() throws MojoFailureException { + Properties properties = new Properties(); + properties.setProperty("p1", "${p2}"); + properties.setProperty("p2", "${p1}}"); + + String value = null; + try { + value = resolver.getPropertyValue("p2", properties, new Properties()); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), containsString("p1")); + assertThat(e.getMessage(), containsString("p2")); + } + + assertNull(value); + } + + @Test + public void valueIsObtainedFromSystemProperty() { + Properties saved = System.getProperties(); + System.setProperty("system.property", "system.value"); + + Properties properties = new Properties(); + properties.setProperty("p1", "${system.property}"); + + String value = resolver.getPropertyValue("p1", properties, new Properties()); + + try { + assertEquals("system.value", value); + } finally { + System.setProperties(saved); + } + } + + @Test + public void valueIsObtainedFromEnvironmentProperty() { + Properties environment = new Properties(); + environment.setProperty("PROPERTY", "env.value"); + + Properties properties = new Properties(); + properties.setProperty("p1", "${env.PROPERTY}"); + + String value = resolver.getPropertyValue("p1", properties, environment); + + assertEquals("env.value", value); + } + + @Test + public void missingPropertyIsTolerated() { + assertEquals("", resolver.getPropertyValue("non-existent", new Properties(), null)); + } + + public void testDefaultValueForUnresolvedPropertyWithEnabledFlag() { + Properties properties = new Properties(); + properties.setProperty("p1", "${unknown:}"); + properties.setProperty("p2", "${unknown:defaultValue}"); + properties.setProperty("p3", "http://${uhost:localhost}:${uport:8080}"); + properties.setProperty("p4", "http://${host:localhost}:${port:8080}"); + properties.setProperty("p5", "${unknown:${fallback}}"); + properties.setProperty("p6", "${unknown:${double.unknown}}"); + properties.setProperty("p7", "${unknown:with space}"); + properties.setProperty("p8", "${unknown:with extra :}"); + properties.setProperty("p9", "${malformed:defVal"); + properties.setProperty("p10", "${malformed:with space"); + properties.setProperty("p11", "${malformed:with extra :"); + properties.setProperty("p12", "${unknown::}"); + properties.setProperty("p13", "${unknown: }"); + + properties.setProperty("host", "example.com"); + properties.setProperty("port", "9090"); + properties.setProperty("fallback", "fallback value"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties(), true); + String value2 = resolver.getPropertyValue("p2", properties, new Properties(), true); + String value3 = resolver.getPropertyValue("p3", properties, new Properties(), true); + String value4 = resolver.getPropertyValue("p4", properties, new Properties(), true); + String value5 = resolver.getPropertyValue("p5", properties, new Properties(), true); + String value6 = resolver.getPropertyValue("p6", properties, new Properties(), true); + String value7 = resolver.getPropertyValue("p7", properties, new Properties(), true); + String value8 = resolver.getPropertyValue("p8", properties, new Properties(), true); + String value9 = resolver.getPropertyValue("p9", properties, new Properties(), true); + String value10 = resolver.getPropertyValue("p10", properties, new Properties(), true); + String value11 = resolver.getPropertyValue("p11", properties, new Properties(), true); + String value12 = resolver.getPropertyValue("p12", properties, new Properties(), true); + String value13 = resolver.getPropertyValue("p13", properties, new Properties(), true); + + assertEquals("${unknown}", value1); + assertEquals("defaultValue", value2); + assertEquals("http://localhost:8080", value3); + assertEquals("http://example.com:9090", value4); + assertEquals("fallback value", value5); + assertEquals("${double.unknown}", value6); + assertEquals("with space", value7); + assertEquals("with extra :", value8); + assertEquals("${malformed:defVal", value9); + assertEquals("${malformed:with space", value10); + assertEquals("${malformed:with extra :", value11); + assertEquals(":", value12); + assertEquals(" ", value13); + } + + /** + * with the flag disabled (default behavior) nothing gets replaced + * ':' is treated as a regular character and part of the property name + */ + public void testDefaultValueForUnresolvedPropertyWithDisabledFlag() { + Properties properties = new Properties(); + properties.setProperty("p1", "${unknown:}"); + properties.setProperty("p2", "${unknown:defaultValue}"); + properties.setProperty("p3", "http://${uhost:localhost}:${uport:8080}"); + properties.setProperty("p4", "http://${host:localhost}:${port:8080}"); + properties.setProperty("p5", "${unknown:${fallback}}"); + properties.setProperty("p6", "${unknown:${double.unknown}}"); + properties.setProperty("p7", "${unknown:with space}"); + properties.setProperty("p8", "${unknown:with extra :}"); + properties.setProperty("p9", "${malformed:defVal"); + properties.setProperty("p10", "${malformed:with space"); + properties.setProperty("p11", "${malformed:with extra :"); + properties.setProperty("p12", "${unknown::}"); + properties.setProperty("p13", "${unknown: }"); + + properties.setProperty("host", "example.com"); + properties.setProperty("port", "9090"); + properties.setProperty("fallback", "fallback value"); + + String value1 = resolver.getPropertyValue("p1", properties, new Properties()); + String value2 = resolver.getPropertyValue("p2", properties, new Properties()); + String value3 = resolver.getPropertyValue("p3", properties, new Properties()); + String value4 = resolver.getPropertyValue("p4", properties, new Properties()); + String value5 = resolver.getPropertyValue("p5", properties, new Properties()); + String value6 = resolver.getPropertyValue("p6", properties, new Properties()); + String value7 = resolver.getPropertyValue("p7", properties, new Properties()); + String value8 = resolver.getPropertyValue("p8", properties, new Properties()); + String value9 = resolver.getPropertyValue("p9", properties, new Properties()); + String value10 = resolver.getPropertyValue("p10", properties, new Properties()); + String value11 = resolver.getPropertyValue("p11", properties, new Properties()); + String value12 = resolver.getPropertyValue("p12", properties, new Properties()); + String value13 = resolver.getPropertyValue("p13", properties, new Properties()); + + assertEquals("${unknown:}", value1); + assertEquals("${unknown:defaultValue}", value2); + assertEquals("http://${uhost:localhost}:${uport:8080}", value3); + assertEquals("http://${host:localhost}:${port:8080}", value4); + assertEquals("${unknown:${fallback}}", value5); + assertEquals("${unknown:${double.unknown}}", value6); + assertEquals("${unknown:with space}", value7); + assertEquals("${unknown:with extra :}", value8); + assertEquals("${malformed:defVal", value9); + assertEquals("${malformed:with space", value10); + assertEquals("${malformed:with extra :", value11); + assertEquals("${unknown::}", value12); + assertEquals("${unknown: }", value13); + } +} diff --git a/src/test/java/org/codehaus/mojo/properties/ReadPropertiesMojoTest.java b/src/test/java/org/codehaus/mojo/properties/ReadPropertiesMojoTest.java index 159f9e3..cb22eff 100644 --- a/src/test/java/org/codehaus/mojo/properties/ReadPropertiesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/properties/ReadPropertiesMojoTest.java @@ -6,10 +6,10 @@ import java.io.IOException; import java.util.Properties; -import org.apache.maven.project.MavenProject; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.project.MavenProject; import org.junit.Before; import org.junit.Test; @@ -17,84 +17,79 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; -public class ReadPropertiesMojoTest -{ - private static final String NEW_LINE = System.getProperty( "line.separator" ); +public class ReadPropertiesMojoTest { + private static final String NEW_LINE = System.getProperty("line.separator"); private MavenProject projectStub; private ReadPropertiesMojo readPropertiesMojo; @Before - public void setUp() - { + public void setUp() { projectStub = new MavenProject(); readPropertiesMojo = new ReadPropertiesMojo(); - readPropertiesMojo.setProject( projectStub ); + readPropertiesMojo.setProject(projectStub); } @Test - public void readPropertiesWithoutKeyprefix() throws Exception - { - try ( FileReader fr = new FileReader( getPropertyFileForTesting() ) ) - { + public void readPropertiesWithoutKeyprefix() throws Exception { + try (FileReader fr = new FileReader(getPropertyFileForTesting())) { // load properties directly for comparison later Properties testProperties = new Properties(); - testProperties.load( fr ); + testProperties.load(fr); // do the work - readPropertiesMojo.setFiles( new File[] {getPropertyFileForTesting()} ); + readPropertiesMojo.setFiles(new File[] {getPropertyFileForTesting()}); readPropertiesMojo.execute(); // check results Properties projectProperties = projectStub.getProperties(); - assertNotNull( projectProperties ); + assertNotNull(projectProperties); // it should not be empty - assertNotEquals( 0, projectProperties.size() ); + assertNotEquals(0, projectProperties.size()); // we are not adding prefix, so properties should be same as in file - assertEquals( testProperties.size(), projectProperties.size() ); - assertEquals( testProperties, projectProperties ); + assertEquals(testProperties.size(), projectProperties.size()); + assertEquals(testProperties, projectProperties); } } @Test - public void readPropertiesWithKeyprefix() throws Exception - { + public void readPropertiesWithKeyprefix() throws Exception { String keyPrefix = "testkey-prefix."; - try ( FileReader fs1 = new FileReader( getPropertyFileForTesting( keyPrefix ) ); - FileReader fs2 = new FileReader( getPropertyFileForTesting() ) ) - { + try (FileReader fs1 = new FileReader(getPropertyFileForTesting(keyPrefix)); + FileReader fs2 = new FileReader(getPropertyFileForTesting())) { Properties testPropertiesWithoutPrefix = new Properties(); - testPropertiesWithoutPrefix.load( fs2 ); + testPropertiesWithoutPrefix.load(fs2); // do the work - readPropertiesMojo.setKeyPrefix( keyPrefix ); - readPropertiesMojo.setFiles( new File[] {getPropertyFileForTesting()} ); + readPropertiesMojo.setKeyPrefix(keyPrefix); + readPropertiesMojo.setFiles(new File[] {getPropertyFileForTesting()}); readPropertiesMojo.execute(); // load properties directly and add prefix for comparison later Properties testPropertiesPrefix = new Properties(); - testPropertiesPrefix.load( fs1 ); + testPropertiesPrefix.load(fs1); // check results Properties projectProperties = projectStub.getProperties(); - assertNotNull( projectProperties ); + assertNotNull(projectProperties); // it should not be empty - assertNotEquals( 0, projectProperties.size() ); + assertNotEquals(0, projectProperties.size()); // we are adding prefix, so prefix properties should be same as in projectProperties - assertEquals( testPropertiesPrefix.size(), projectProperties.size() ); - assertEquals( testPropertiesPrefix, projectProperties ); + assertEquals(testPropertiesPrefix.size(), projectProperties.size()); + assertEquals(testPropertiesPrefix, projectProperties); // properties with and without prefix shouldn't be same - assertNotEquals( testPropertiesPrefix, testPropertiesWithoutPrefix ); - assertNotEquals( testPropertiesWithoutPrefix, projectProperties ); + assertNotEquals(testPropertiesPrefix, testPropertiesWithoutPrefix); + assertNotEquals(testPropertiesWithoutPrefix, projectProperties); } } @Test - public void testDefaultValueForUnresolvedPropertyWithEnabledFlag() throws MojoFailureException, MojoExecutionException, IOException { + public void testDefaultValueForUnresolvedPropertyWithEnabledFlag() + throws MojoFailureException, MojoExecutionException, IOException { Properties properties = new Properties(); properties.setProperty("p1", "${unknown:}"); properties.setProperty("p2", "${unknown:defaultValue}"); @@ -155,28 +150,27 @@ public void testDefaultValueForUnresolvedPropertyWithEnabledFlag() throws MojoFa @Test public void readPropertiesOverridingExisting() throws Exception { - File testPropertyFile = getPropertyFileForTesting(); - // load properties directly for comparison later - Properties testProperties = new Properties(); - testProperties.load(new FileReader(testPropertyFile)); - - // Existing property value should be overridden - projectStub.getProperties().put("test.property2", "old-value"); + File testPropertyFile = getPropertyFileForTesting(); + // load properties directly for comparison later + Properties testProperties = new Properties(); + testProperties.load(new FileReader(testPropertyFile)); - // do the work - readPropertiesMojo.setFiles(new File[]{testPropertyFile}); - readPropertiesMojo.execute(); + // Existing property value should be overridden + projectStub.getProperties().put("test.property2", "old-value"); - // check results - Properties projectProperties = projectStub.getProperties(); - assertNotNull(projectProperties); - // it should not be empty - assertNotEquals(0, projectProperties.size()); + // do the work + readPropertiesMojo.setFiles(new File[] {testPropertyFile}); + readPropertiesMojo.execute(); - // we are not adding prefix, so properties should be same as in file - assertEquals(testProperties.size(), projectProperties.size()); - assertEquals(testProperties, projectProperties); + // check results + Properties projectProperties = projectStub.getProperties(); + assertNotNull(projectProperties); + // it should not be empty + assertNotEquals(0, projectProperties.size()); + // we are not adding prefix, so properties should be same as in file + assertEquals(testProperties.size(), projectProperties.size()); + assertEquals(testProperties, projectProperties); } @Test @@ -189,10 +183,10 @@ public void readPropertiesPreserveExisting() throws Exception { // Existing property should keep the value testProperties.put("test.property2", "old-value"); - projectStub.getProperties().put("test.property2", "old-value"); + projectStub.getProperties().put("test.property2", "old-value"); // do the work - readPropertiesMojo.setFiles(new File[]{testPropertyFile}); + readPropertiesMojo.setFiles(new File[] {testPropertyFile}); readPropertiesMojo.setOverride(false); readPropertiesMojo.execute(); @@ -212,7 +206,8 @@ public void readPropertiesPreserveExisting() throws Exception { * ':' is treated as a regular character and part of the property name */ @Test - public void testDefaultValueForUnresolvedPropertyWithDisabledFlag() throws MojoFailureException, MojoExecutionException, IOException { + public void testDefaultValueForUnresolvedPropertyWithDisabledFlag() + throws MojoFailureException, MojoExecutionException, IOException { Properties properties = new Properties(); properties.setProperty("p1", "${unknown:}"); properties.setProperty("p2", "${unknown:defaultValue}"); @@ -269,30 +264,24 @@ public void testDefaultValueForUnresolvedPropertyWithDisabledFlag() throws MojoF assertEquals("${unknown: }", value13); } - private File getPropertyFileForTesting() throws IOException - { - return getPropertyFileForTesting( null ); + private File getPropertyFileForTesting() throws IOException { + return getPropertyFileForTesting(null); } - private File getPropertyFileForTesting( String keyPrefix ) throws IOException - { - File f = File.createTempFile( "prop-test", ".properties" ); + private File getPropertyFileForTesting(String keyPrefix) throws IOException { + File f = File.createTempFile("prop-test", ".properties"); f.deleteOnExit(); - FileWriter writer = new FileWriter( f ); + FileWriter writer = new FileWriter(f); String prefix = keyPrefix; - if ( prefix == null ) - { + if (prefix == null) { prefix = ""; } - try - { - writer.write( prefix + "test.property1=value1" + NEW_LINE ); - writer.write( prefix + "test.property2=value2" + NEW_LINE ); - writer.write( prefix + "test.property3=value3" + NEW_LINE ); + try { + writer.write(prefix + "test.property1=value1" + NEW_LINE); + writer.write(prefix + "test.property2=value2" + NEW_LINE); + writer.write(prefix + "test.property3=value3" + NEW_LINE); writer.flush(); - } - finally - { + } finally { writer.close(); } return f;