Skip to content

Commit

Permalink
Resolves mojohaus#1042: Fixed set logic wrt processAllModules. Introd…
Browse files Browse the repository at this point in the history
…uced fullTreeMatch.
  • Loading branch information
jarmoniuk committed Feb 17, 2024
1 parent 71de55d commit 509d6ab
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TimeZone;
Expand Down Expand Up @@ -92,6 +93,16 @@ public class SetMojo extends AbstractVersionsUpdaterMojo {
@Parameter(property = "processAllModules", defaultValue = "false")
private boolean processAllModules;

/**
* <p>If set to {@code true}, will look up throughout the whole reactor to match modules
* being updated. By default, the goal only descends into modules' subtrees.</p>
* <p><em>Note.</em> This property was split off {@code processAllModules}.</p>
*
* @since 2.16.3
*/
@Parameter(property = "fullTreeMatch", defaultValue = "false")
private boolean fullTreeMatch;

/**
* <p>The <b>non-interpolated</b> groupId of the dependency/module to be selected for update.</p>
* <p>If not set, will be equal to the non-interpolated groupId of the project file.</p>
Expand Down Expand Up @@ -449,15 +460,12 @@ private void applyChange(
String oldVersion) {

getLog().debug("Applying change " + groupId + ":" + artifactId + ":" + oldVersion + " -> " + newVersion);
// this is a triggering change
addChange(groupId, artifactId, oldVersion, newVersion);
// now fake out the triggering change

Map.Entry<File, Model> current = PomHelper.getModelEntry(reactor, groupId, artifactId);
if (current != null) {
current.getValue().setVersion(newVersion);
files.add(current.getValue().getPomFile());
}
Optional.ofNullable(PomHelper.getModelEntry(reactor, groupId, artifactId))
.map(Map.Entry::getValue)
.map(Model::getPomFile)
.ifPresent(files::add);

for (Map.Entry<File, Model> sourceEntry : reactor.entrySet()) {
final File sourcePath = sourceEntry.getKey();
Expand Down Expand Up @@ -489,7 +497,7 @@ private void applyChange(
getLog().debug("Looking for modules which use "
+ ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + " as their parent");

for (Map.Entry<File, Model> stringModelEntry : processAllModules
for (Map.Entry<File, Model> stringModelEntry : fullTreeMatch
? reactor.entrySet()
: PomHelper.getChildModels(reactor, sourceGroupId, sourceArtifactId)
.entrySet()) {
Expand All @@ -498,12 +506,12 @@ private void applyChange(
getLog().debug("Module: " + stringModelEntry.getKey());
if (parent != null && sourceVersion.equals(parent.getVersion())) {
getLog().debug(" parent already is "
+ ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + ":" + sourceVersion);
+ ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId) + ":" + newVersion);
} else {
getLog().debug(" parent is " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId)
+ ":" + (parent == null ? "" : parent.getVersion()));
getLog().debug(" will become " + ArtifactUtils.versionlessKey(sourceGroupId, sourceArtifactId)
+ ":" + sourceVersion);
+ ":" + newVersion);
}
final boolean targetExplicit = PomHelper.isExplicitVersion(targetModel);
if ((updateMatchingVersions || !targetExplicit) //
Expand All @@ -517,13 +525,13 @@ private void applyChange(
getLog().debug(" will become "
+ ArtifactUtils.versionlessKey(
PomHelper.getGroupId(targetModel), PomHelper.getArtifactId(targetModel))
+ ":" + sourceVersion);
+ ":" + newVersion);
addChange(
PomHelper.getGroupId(targetModel),
PomHelper.getArtifactId(targetModel),
PomHelper.getVersion(targetModel),
sourceVersion);
targetModel.setVersion(sourceVersion);
newVersion);
targetModel.setVersion(newVersion);
} else {
getLog().debug(" module is "
+ ArtifactUtils.versionlessKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.matchesRegex;
import static org.hamcrest.Matchers.not;

public class SetMojoTest extends AbstractMojoTestCase {
Expand Down Expand Up @@ -212,4 +214,18 @@ public void testParentWithProperty() throws Exception {
String.join("", Files.readAllLines(tempDir.resolve("child/pom.xml"))),
containsString("<version>testing</version>"));
}

@Test
public void testIssue1042() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/issue-1042"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
matchesPattern(
".*\\Q<artifactId>child-reactor</artifactId>\\E\\s*" + "\\Q<version>1.0</version>\\E.*"));
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("child-webapp/pom.xml"))),
matchesRegex(".*\\Q<artifactId>child-webapp</artifactId>\\E\\s*" + "\\Q<version>1.0</version>\\E.*"));
}
}
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>default</groupId>
<artifactId>package-parent</artifactId>
<version>1.0</version>
<relativePath>../main-reactor/package-parent/pom.xml</relativePath>
</parent>

<artifactId>child-webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

</project>
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>default</groupId>
<artifactId>pom-parent</artifactId>
<version>1.0</version>
<relativePath>../pom-parent/pom.xml</relativePath>
</parent>

<artifactId>package-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<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>
<groupId>default</groupId>
<artifactId>pom-parent</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<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>
<groupId>default</groupId>
<artifactId>main-reactor</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<modules>
<module>pom-parent</module>
<module>package-parent</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<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>default</groupId>
<artifactId>pom-parent</artifactId>
<version>1.0</version>
<relativePath>main-reactor/pom-parent/pom.xml</relativePath>
</parent>

<artifactId>child-reactor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
<module>child-webapp</module>
</modules>

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

0 comments on commit 509d6ab

Please sign in to comment.