Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #888: New optional parameter to SetMojo: interpolateProperties #889

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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