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 57e25182db..c4bdc8eafb 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 @@ -66,7 +66,6 @@ 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; /** @@ -252,6 +251,15 @@ public class SetMojo extends AbstractVersionsUpdaterMojo { */ protected final ProjectBuilder projectBuilder; + /** + * If set to {@code false}, the plugin will not interpolate property values when looking for versions + * to be changed, but will instead operate on raw model. + * + * @since 2.15.0 + */ + @Parameter(property = "interpolateProperties", defaultValue = "true") + protected boolean interpolateProperties = true; + @Inject public SetMojo( RepositorySystem repositorySystem, @@ -350,17 +358,16 @@ public void execute() throws MojoExecutionException, MojoFailureException { Pattern.compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(oldVersion, "*"), true)); for (Model m : reactor.values()) { - Map 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) { + if (interpolateProperties) { + assert m.getProperties() != null; // always non-null + Map properties = m.getProperties().entrySet().stream() + .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue() + .toString())); + mGroupId = PomHelper.evaluate(mGroupId, properties); mArtifactId = PomHelper.evaluate(mArtifactId, properties); mVersion = PomHelper.evaluate(mVersion, properties); 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 2a8ac319f9..9bec548845 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 @@ -5,6 +5,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.function.Consumer; +import java.util.stream.Stream; import org.apache.maven.model.Model; import org.apache.maven.plugin.MojoExecutionException; @@ -159,11 +161,12 @@ public void testSetOldVersionMismatchProcessAllModules() throws Exception { not(containsString("bar"))); } - private void testSetParameterValue(String filename) throws Exception { + private void testSetParameterValue(String filename, Consumer... initializers) 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"); + Stream.of(initializers).forEachOrdered(i -> i.accept(mojo)); mojo.execute(); assertThat( String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))), @@ -175,6 +178,28 @@ public void testSetParameterValueSimple() throws Exception { testSetParameterValue("pom-simple.xml"); } + @Test + public void testSetParameterValueSimpleNoInterpolation() throws Exception { + try { + testSetParameterValue("pom-simple.xml", mojo -> mojo.interpolateProperties = false); + fail(); + } catch (AssertionError e) { + assertThat(e.getMessage(), containsString("Expected: a string containing \"testing")); + } + } + + @Test + public void testSetParameterValueSimpleNoInterpolationWildcard() throws Exception { + testSetParameterValue("pom-simple.xml", mojo -> { + mojo.interpolateProperties = false; + try { + setVariableValueToObject(mojo, "oldVersion", "*"); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }); + } + @Test public void testSetParameterValueBuildNumber() throws Exception { testSetParameterValue("pom-build-number.xml");