From a49d2b5cc6cb19bdd856ae507328890649df49bd Mon Sep 17 00:00:00 2001 From: "Andrzej J." <1554729+andrzejj0@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:23:58 +0200 Subject: [PATCH] Resolves #1137: using PomHelper.getGroupId if groupId of the current project root is empty (#1162) --- .../org/codehaus/mojo/versions/SetMojo.java | 2 +- .../codehaus/mojo/versions/SetMojoTest.java | 17 +++ .../mojo/set/issue-1137/child/pom.xml | 11 ++ .../org/codehaus/mojo/set/issue-1137/pom.xml | 11 ++ .../codehaus/mojo/versions/utils/TestLog.java | 102 ++++++++++++++++++ 5 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/child/pom.xml create mode 100644 versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/pom.xml create mode 100644 versions-test/src/main/java/org/codehaus/mojo/versions/utils/TestLog.java diff --git a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java index 2be7d472b5..b1b9fef939 100644 --- a/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java +++ b/versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/SetMojo.java @@ -345,7 +345,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { // so that the main project can be selected Model rootModel = reactorModels.get(session.getCurrentProject().getFile()); if (groupId == null) { - groupId = rootModel.getGroupId(); + groupId = PomHelper.getGroupId(rootModel); } if (artifactId == null) { artifactId = rootModel.getArtifactId(); diff --git a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java index 898d895bd8..7303827785 100644 --- a/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java +++ b/versions-maven-plugin/src/test/java/org/codehaus/mojo/versions/SetMojoTest.java @@ -6,14 +6,17 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.function.Consumer; +import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.commons.lang3.tuple.Triple; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.MojoRule; import org.apache.maven.project.MavenProject; +import org.codehaus.mojo.versions.utils.TestLog; import org.codehaus.mojo.versions.utils.TestUtils; import org.junit.After; import org.junit.Before; @@ -231,4 +234,18 @@ public void testIssue1042() throws Exception { String.join("", Files.readAllLines(tempDir.resolve("child-webapp/pom.xml"))), matchesRegex(".*\\Qchild-webapp\\E\\s*" + "\\Q1.0\\E.*")); } + + @Test + public void testIssue1137() throws Exception { + TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/issue-1137"), tempDir); + TestLog testLog = new TestLog(); + SetMojo mojo = + (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.resolve("child").toFile(), "set"); + setVariableValueToObject(mojo, "newVersion", "1.1"); + setVariableValueToObject(mojo, "log", testLog); + mojo.execute(); + assertThat( + testLog.getLoggedMessages().stream().map(Triple::getMiddle).collect(Collectors.joining("\n")), + not(containsString("Processing change of null:child"))); + } } diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/child/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/child/pom.xml new file mode 100644 index 0000000000..0eb49500dc --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/child/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + + group + parent + 1.0 + + child + 1.0 + diff --git a/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/pom.xml b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/pom.xml new file mode 100644 index 0000000000..3f5b9e95b2 --- /dev/null +++ b/versions-maven-plugin/src/test/resources/org/codehaus/mojo/set/issue-1137/pom.xml @@ -0,0 +1,11 @@ + + 4.0.0 + group + parent + 1.0 + pom + + child + + diff --git a/versions-test/src/main/java/org/codehaus/mojo/versions/utils/TestLog.java b/versions-test/src/main/java/org/codehaus/mojo/versions/utils/TestLog.java new file mode 100644 index 0000000000..daef1f4f1d --- /dev/null +++ b/versions-test/src/main/java/org/codehaus/mojo/versions/utils/TestLog.java @@ -0,0 +1,102 @@ +package org.codehaus.mojo.versions.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.lang3.tuple.Triple; +import org.apache.maven.plugin.logging.Log; + +public class TestLog implements Log { + private final List> loggedMessages = new ArrayList<>(); + + public List> getLoggedMessages() { + return loggedMessages; + } + + @Override + public boolean isDebugEnabled() { + return false; + } + + @Override + public void debug(CharSequence charSequence) { + debug(charSequence, null); + } + + @Override + public void debug(CharSequence charSequence, Throwable throwable) { + loggedMessages.add(Triple.of(LogLevel.DEBUG, charSequence, throwable)); + } + + @Override + public void debug(Throwable throwable) { + debug(null, throwable); + } + + @Override + public boolean isInfoEnabled() { + return false; + } + + @Override + public void info(CharSequence charSequence) { + info(charSequence, null); + } + + @Override + public void info(CharSequence charSequence, Throwable throwable) { + loggedMessages.add(Triple.of(LogLevel.INFO, charSequence, throwable)); + } + + @Override + public void info(Throwable throwable) { + info(null, throwable); + } + + @Override + public boolean isWarnEnabled() { + return false; + } + + @Override + public void warn(CharSequence charSequence) { + warn(charSequence, null); + } + + @Override + public void warn(CharSequence charSequence, Throwable throwable) { + loggedMessages.add(Triple.of(LogLevel.WARN, charSequence, throwable)); + } + + @Override + public void warn(Throwable throwable) { + warn(null, throwable); + } + + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public void error(CharSequence charSequence) { + error(charSequence, null); + } + + @Override + public void error(CharSequence charSequence, Throwable throwable) { + loggedMessages.add(Triple.of(LogLevel.ERROR, charSequence, throwable)); + } + + @Override + public void error(Throwable throwable) { + error(null, throwable); + } + + public enum LogLevel { + DEBUG, + INFO, + WARN, + ERROR + } +}