Skip to content

Commit

Permalink
Resolves mojohaus#888: New optional parameter to SetMojo: interpolate…
Browse files Browse the repository at this point in the history
…Properties

- 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
  • Loading branch information
jarmoniuk committed Dec 30, 2022
1 parent 09ce5a2 commit 53079aa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -350,17 +358,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Pattern.compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(oldVersion, "*"), true));

for (Model m : reactor.values()) {
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) {
if (interpolateProperties) {
assert m.getProperties() != null; // always non-null
Map<String, String> 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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -159,11 +161,12 @@ public void testSetOldVersionMismatchProcessAllModules() throws Exception {
not(containsString("<version>bar</version>")));
}

private void testSetParameterValue(String filename) throws Exception {
private void testSetParameterValue(String filename, Consumer<SetMojo>... 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"))),
Expand All @@ -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 \"<version>testing</version>"));
}
}

@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");
Expand Down

0 comments on commit 53079aa

Please sign in to comment.