Skip to content

Commit

Permalink
Resolves #855: Set should evaluate expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Dec 16, 2022
1 parent 23c225f commit 885f561
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Model;
Expand All @@ -65,6 +66,7 @@
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.StringUtils;

import static java.util.Optional.ofNullable;
import static org.codehaus.plexus.util.StringUtils.isEmpty;

/**
Expand Down Expand Up @@ -348,9 +350,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Pattern.compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(oldVersion, "*"), true));

for (Model m : reactor.values()) {
final String mGroupId = PomHelper.getGroupId(m);
final String mArtifactId = PomHelper.getArtifactId(m);
final String mVersion = PomHelper.getVersion(m);
Map<String, String> properties = ofNullable(m.getProperties())
.map(p -> p.entrySet().parallelStream()
.collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue()
.toString())))
.orElse(null);

String mGroupId = PomHelper.getGroupId(m);
String mArtifactId = PomHelper.getArtifactId(m);
String mVersion = PomHelper.getVersion(m);

if (properties != null) {
mGroupId = PomHelper.evaluate(mGroupId, properties);
mArtifactId = PomHelper.evaluate(mArtifactId, properties);
mVersion = PomHelper.evaluate(mVersion, properties);
}

if ((processAllModules
|| groupIdRegex.matcher(mGroupId).matches()
&& artifactIdRegex.matcher(mArtifactId).matches())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,35 @@ public void testSetOldVersionMismatchProcessAllModules() throws Exception {
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>bar</version>")));
}

private void testSetParameterValue(String filename) throws Exception {
Files.copy(
Paths.get("src/test/resources/org/codehaus/mojo/set/issue-855/").resolve(filename),
tempDir.resolve("pom.xml"));
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>testing</version>"));
}

@Test
public void testSetParameterValueSimple() throws Exception {
testSetParameterValue("pom-simple.xml");
}

@Test
public void testSetParameterValueBuildNumber() throws Exception {
testSetParameterValue("pom-build-number.xml");
}

@Test
public void testSetParameterValueUndefined() throws Exception {
testSetParameterValue("pom-undefined.xml");
}

@Test
public void testSetParameterValueMultipleProps() throws Exception {
testSetParameterValue("pom-multiple-props.xml");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.example</groupId>
<artifactId>test-versions</artifactId>
<version>${revision}</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<revision>1.0.0-${buildNumber}-SNAPSHOT</revision>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<newVersion>testing</newVersion>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.example</groupId>
<artifactId>test-versions</artifactId>
<version>${revision}</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<revision>1.0.0-${buildNumber}-SNAPSHOT</revision>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<newVersion>testing</newVersion>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.example</groupId>
<artifactId>test-versions</artifactId>
<version>${revision}</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<revision>1.3</revision>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<newVersion>testing</newVersion>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.example</groupId>
<artifactId>test-versions</artifactId>
<version>${revision}</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<revision>1.0.0-${buildNumber}-SNAPSHOT</revision>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<newVersion>testing</newVersion>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 885f561

Please sign in to comment.