From 14f8e12adf8943e65ea598fd9ffdc7de33f03b4a Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 30 Dec 2022 20:18:46 +0100 Subject: [PATCH] Resolves #888: New optional parameter to SetMojo: interpolateProperties - allows to disable property interpolation, so that it's possible to match against raw property values as well as replace properties with interpolated values if the value is the same as the property value --- .../org/codehaus/mojo/versions/SetMojo.java | 23 ++++++++++------ .../codehaus/mojo/versions/SetMojoTest.java | 27 ++++++++++++++++++- 2 files changed, 41 insertions(+), 9 deletions(-) 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");