Skip to content

Commit

Permalink
Resolves mojohaus#855: Set should evaluate expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Dec 17, 2022
1 parent 6619919 commit 399da07
Show file tree
Hide file tree
Showing 8 changed files with 224 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().stream()
.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,49 @@ 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");
}

@Test
public void testParentWithProperty() throws Exception {
TestUtils.copyDir(
Paths.get("src/test/resources/org/codehaus/mojo/set/issue-855/parent-with-property"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>testing</version>"));
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("child/pom.xml"))),
containsString("<version>testing</version>"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.example</groupId>
<artifactId>test-parent</artifactId>
<version>${revision}</version>
</parent>

<artifactId>test-child</artifactId>
<version>1.0.0-SNAPSHOT</version>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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-parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modelVersion>4.0.0</modelVersion>

<modules>
<module>child</module>
</modules>

<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.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 399da07

Please sign in to comment.