diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 8134287409..af71add4a4 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -1,5 +1,20 @@
# Release Notes
+* [Fixed Issue 469][issue-469]
+
+ Possibility to use set-property with `-DpropertiesVersionsFile=test.properties`
+ to set multiple properties at the same time.
+ Example:
+ ```properties
+ #test.properties
+ compo1.version=1.2.3
+ compo2.version=4.5.6
+ ```
+ To update all properties in pom.xml in one call:
+ ```
+ mvn version:set-property -DpropertiesVersionsFile=test.properties
+ ```
+
## 2.6
* [Pull Request #252][pull-252]
diff --git a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
index 02ddbf7dd4..75a2bb4ca5 100644
--- a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
+++ b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
@@ -26,8 +27,11 @@
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.PropertyVersions;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
+import org.codehaus.mojo.versions.utils.PropertiesVersionsFileReader;
import javax.xml.stream.XMLStreamException;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.util.Map;
/**
@@ -35,7 +39,7 @@
* not build anymore. The sanity checks are done by other goals like update-properties
or
* update-property
etc. they are not done here. So use this goal with care.
*
- * @author Karl Heinz Marbaise
+ * @author Karl Heinz Marbaise
* @since 2.5
*/
@Mojo( name = "set-property", requiresProject = true, requiresDirectInvocation = true, threadSafe = true )
@@ -63,6 +67,15 @@ public class SetPropertyMojo
@Parameter( property = "autoLinkItems", defaultValue = "true" )
private boolean autoLinkItems;
+ /**
+ * A property file name containing: property=value, to update several properties at the same time.
+ * If 'property' and 'newVersion' are also used, they will be ignored.
+ * @since 2.9
+ */
+
+ @Parameter( property = "propertiesVersionsFile" )
+ private String propertiesVersionsFile;
+
/**
* @param pom the pom to update.
* @throws MojoExecutionException when things go wrong
@@ -73,24 +86,57 @@ public class SetPropertyMojo
protected void update( ModifiedPomXMLEventReader pom )
throws MojoExecutionException, MojoFailureException, XMLStreamException
{
- Property propertyConfig = new Property( property );
- propertyConfig.setVersion( newVersion );
+ Property[] propertiesConfig = null;
+ String properties = "";
+ if (!StringUtils.isEmpty(propertiesVersionsFile) ) {
+ logWrongConfigWarning();
+ getLog().debug( "Reading properties and versions to update from file: " + propertiesVersionsFile );
+ PropertiesVersionsFileReader reader = new PropertiesVersionsFileReader(propertiesVersionsFile);
+ try {
+ reader.read();
+ } catch (IOException e) {
+ getLog().error("Unable to read property file " + propertiesVersionsFile
+ + ". re-run with -X option for more details.");
+ getLog().debug("Error while reading property file " + propertiesVersionsFile, e);
+ throw new MojoFailureException("Unable to read property file " + propertiesVersionsFile);
+ }
+ propertiesConfig = reader.getPropertiesConfig();
+ properties = reader.getProperties();
+ } else {
+ getLog().debug( "Reading properties and versions to update from property and newVersion " );
+ Property propertyConfig = new Property(property);
+ propertyConfig.setVersion(newVersion);
+ propertiesConfig = new Property[] { propertyConfig };
+ properties = property;
+ }
+ update(pom, propertiesConfig, properties);
+ }
+
+ private void update(ModifiedPomXMLEventReader pom, Property[] propertiesConfig, String properties) throws MojoExecutionException, XMLStreamException {
Map propertyVersions =
- this.getHelper().getVersionPropertiesMap( getProject(), new Property[] { propertyConfig }, property, "",
+ this.getHelper().getVersionPropertiesMap( getProject(), propertiesConfig, properties, "",
autoLinkItems );
for ( Map.Entry entry : propertyVersions.entrySet() )
{
- Property property = entry.getKey();
+ Property currentProperty = entry.getKey();
PropertyVersions version = entry.getValue();
+ String newVersionGiven = currentProperty.getVersion();
- final String currentVersion = getProject().getProperties().getProperty( property.getName() );
+ final String currentVersion = getProject().getProperties().getProperty( currentProperty.getName() );
if ( currentVersion == null )
{
continue;
}
+ PomHelper.setPropertyVersion(pom, version.getProfileId(), currentProperty.getName(), newVersionGiven );
+ }
+ }
- PomHelper.setPropertyVersion( pom, version.getProfileId(), property.getName(), newVersion );
-
+ private void logWrongConfigWarning() {
+ if (!StringUtils.isEmpty(property)) {
+ getLog().warn("-Dproperty provided but will be ignored as -DpropertiesVersionsFile is used");
+ }
+ if (!StringUtils.isEmpty(property)) {
+ getLog().warn("-DnewVersion provided but will be ignored as -DpropertiesVersionsFile is used");
}
}
diff --git a/src/main/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReader.java b/src/main/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReader.java
new file mode 100644
index 0000000000..299831a412
--- /dev/null
+++ b/src/main/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReader.java
@@ -0,0 +1,59 @@
+package org.codehaus.mojo.versions.utils;
+
+import org.codehaus.mojo.versions.Property;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+public class PropertiesVersionsFileReader {
+
+ /**
+ * Commas-separated list of properties
+ */
+ private String propertiesCsv;
+
+ private Property[] propertiesConfig;
+
+ private String propertyFilePath;
+
+ public PropertiesVersionsFileReader(String filePath) {
+ propertyFilePath = filePath;
+ }
+
+ public void read() throws IOException {
+ try (InputStream input = new FileInputStream(propertyFilePath)) {
+
+ Properties prop = new Properties();
+
+ // load a properties file
+ prop.load(input);
+
+ prop.propertyNames();
+
+ propertiesCsv = prop.keySet().stream().map(Object::toString).collect(Collectors.joining(","));
+
+ List propertiesConfigList = new ArrayList<>();
+ prop.forEach((name, version) -> {
+ Property propertyConfig = new Property((String)name);
+ propertyConfig.setVersion((String)version);
+ propertiesConfigList.add(propertyConfig);
+ });
+
+ propertiesConfig = propertiesConfigList.toArray(new Property[0]);
+ }
+ }
+
+ public String getProperties() {
+ return propertiesCsv;
+ }
+
+ public Property[] getPropertiesConfig() {
+ return propertiesConfig;
+ }
+}
diff --git a/src/site/apt/index.apt b/src/site/apt/index.apt
index b2e6501da3..ed21158305 100644
--- a/src/site/apt/index.apt
+++ b/src/site/apt/index.apt
@@ -72,6 +72,8 @@ Versions Maven Plugin
* {{{./set-mojo.html}versions:set}} can be used to set the project version from the command line.
+ * {{{./set-property-mojo.html}versions:set-property}} can be used to set properties to a given version from the command line.
+
* {{{./use-releases-mojo.html}versions:use-releases}} searches the pom for all -SNAPSHOT versions which have been
released and replaces them with the corresponding release version.
diff --git a/src/test/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReaderTest.java b/src/test/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReaderTest.java
new file mode 100644
index 0000000000..5f369ecc12
--- /dev/null
+++ b/src/test/java/org/codehaus/mojo/versions/utils/PropertiesVersionsFileReaderTest.java
@@ -0,0 +1,36 @@
+package org.codehaus.mojo.versions.utils;
+
+import junit.framework.TestCase;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Test;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.*;
+
+public class PropertiesVersionsFileReaderTest extends TestCase {
+
+ private static final String TEST_PROPERTIES_FILE = "src/test/resources/org/codehaus/mojo/versions/utils/testPropertiesVersionsFile.properties";
+
+ @Test
+ public void testRead() {
+ PropertiesVersionsFileReader reader = new PropertiesVersionsFileReader(TEST_PROPERTIES_FILE);
+ try {
+ reader.read();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ int numberOfPropertiesConfig=3;
+ assertTrue(equalsCvsUnordered("booking-api.version,booking-lib.version,be-air-impl.version", reader.getProperties()));
+ assertEquals(numberOfPropertiesConfig, reader.getPropertiesConfig().length);
+ }
+
+ private boolean equalsCvsUnordered(String csvExpected, String csvActual) {
+ if (StringUtils.isEmpty(csvExpected)) {
+ return false;
+ }
+ Set listExpected = new HashSet(Arrays.asList(csvExpected.split(",")));
+ Set listActual = new HashSet(Arrays.asList(csvActual.split(",")));
+ return listExpected.equals(listActual);
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/org/codehaus/mojo/versions/utils/testPropertiesVersionsFile.properties b/src/test/resources/org/codehaus/mojo/versions/utils/testPropertiesVersionsFile.properties
new file mode 100644
index 0000000000..f84d8fb6a3
--- /dev/null
+++ b/src/test/resources/org/codehaus/mojo/versions/utils/testPropertiesVersionsFile.properties
@@ -0,0 +1,3 @@
+booking-api.version=1.2.3
+booking-lib.version=4.5.6
+be-air-impl.version=7.8.9
\ No newline at end of file