Skip to content

Commit

Permalink
Resolves #855: Set should evaluate expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Dec 15, 2022
1 parent 23c225f commit afa2da8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.TimeZone;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.model.Model;
Expand All @@ -65,6 +66,7 @@
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 @@ -348,9 +350,22 @@ public void execute() throws MojoExecutionException, MojoFailureException {
Pattern.compile(RegexUtils.convertWildcardsToRegex(fixNullOrEmpty(oldVersion, "*"), true));

for (Model m : reactor.values()) {
final String mGroupId = PomHelper.getGroupId(m);
final String mArtifactId = PomHelper.getArtifactId(m);
final String mVersion = PomHelper.getVersion(m);
Map<String, String> properties = ofNullable(m.getProperties())
.map(p -> p.entrySet().parallelStream()
.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) {
mGroupId = PomHelper.evaluate(mGroupId, properties);
mArtifactId = PomHelper.evaluate(mArtifactId, properties);
mVersion = PomHelper.evaluate(mVersion, properties);
}

if ((processAllModules
|| groupIdRegex.matcher(mGroupId).matches()
&& artifactIdRegex.matcher(mArtifactId).matches())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,14 @@ public void testSetOldVersionMismatchProcessAllModules() throws Exception {
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>bar</version>")));
}

@Test
public void testSetParameterValue() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/set/issue-855"), tempDir);
SetMojo mojo = (SetMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "set");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>${revision}</version>")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>org.example</groupId>
<artifactId>test-versions</artifactId>
<version>${revision}</version>
<modelVersion>4.0.0</modelVersion>

<properties>
<revision>1.3</revision>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<goals>
<goal>set</goal>
</goals>
<configuration>
<newVersion>testing</newVersion>
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
</plugins>
</build>
</project>

0 comments on commit afa2da8

Please sign in to comment.