Skip to content

Commit

Permalink
Fixed mojohaus#469 Support.property file in set-property
Browse files Browse the repository at this point in the history
Possibility to use set-property with `-DpropertiesVersionsFile=test.properties`
to set multiple properties.
  • Loading branch information
arkel-s committed Jul 6, 2021
1 parent 5ea67ba commit 8cb61ad
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 8 deletions.
15 changes: 15 additions & 0 deletions 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]
Expand Down
62 changes: 54 additions & 8 deletions src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
Expand Up @@ -19,23 +19,27 @@
* 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;
import org.apache.maven.plugins.annotations.Parameter;
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;

/**
* Set a property to a given version without any sanity checks. Please be careful this can lead to changes which might
* not build anymore. The sanity checks are done by other goals like <code>update-properties</code> or
* <code>update-property</code> 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 )
Expand Down Expand Up @@ -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
Expand All @@ -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<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), new Property[] { propertyConfig }, property, "",
this.getHelper().getVersionPropertiesMap( getProject(), propertiesConfig, properties, "",
autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> 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");
}
}

Expand Down
@@ -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<Property> 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;
}
}
2 changes: 2 additions & 0 deletions src/site/apt/index.apt
Expand Up @@ -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.

Expand Down
@@ -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<String> listExpected = new HashSet<String>(Arrays.asList(csvExpected.split(",")));
Set<String> listActual = new HashSet<String>(Arrays.asList(csvActual.split(",")));
return listExpected.equals(listActual);
}
}
@@ -0,0 +1,3 @@
booking-api.version=1.2.3
booking-lib.version=4.5.6
be-air-impl.version=7.8.9

0 comments on commit 8cb61ad

Please sign in to comment.