Skip to content

Commit

Permalink
Resolves mojohaus#855: Set should evaluate expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Dec 16, 2022
1 parent 23c225f commit f75ae59
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 3 deletions.
Expand Up @@ -25,8 +25,16 @@
import java.io.File;
import java.io.StringReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Model;
import org.apache.maven.model.io.DefaultModelWriter;
import org.apache.maven.model.io.ModelWriter;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
Expand All @@ -49,6 +57,9 @@
* Tests the methods of {@link PomHelper}.
*/
public class PomHelperTest extends AbstractMojoTestCase {
private static final int NUMBER_OF_CHILD_PROJECTS = 30;
private static final String DEFAULT_VERSION = "1.0.0-SNAPSHOT";

@Rule
public MojoRule mojoRule = new MojoRule(this);

Expand Down Expand Up @@ -219,4 +230,50 @@ public void testIssue505ChildModules() throws Exception {
mojoRule.readMavenProject(new File("src/test/resources/org/codehaus/mojo/versions/api/issue-505"));
assertThat(PomHelper.getChildModels(project, new SystemStreamLog()).entrySet(), hasSize(3));
}

@Test
public void testChildModelsForMultiLevelProject() throws Exception {
Path tempDirectory = Files.createTempDirectory("testChildModelsForLargeNumberOfModules");
ModelWriter modelWriter = new DefaultModelWriter();
Map<Path, Model> createdModels = new LinkedHashMap<>();

try {
Model rootProject = createSimpleModel("root");
createdModels.put(tempDirectory, rootProject);
for (int levelOne = 0; levelOne < NUMBER_OF_CHILD_PROJECTS; levelOne++) {
Model levelOneProject = createSimpleModel("child-" + levelOne);
Path levelOneProjectDirectory = tempDirectory.resolve(levelOneProject.getArtifactId());
rootProject.addModule(levelOneProject.getArtifactId());
createdModels.put(levelOneProjectDirectory, levelOneProject);

for (int levelTwo = 0; levelTwo < NUMBER_OF_CHILD_PROJECTS; levelTwo++) {
Model levelTwoProject = createSimpleModel("child-" + levelOne + "-" + levelTwo);
Path levelTwoProjectDirectory = levelOneProjectDirectory.resolve(levelTwoProject.getArtifactId());
levelOneProject.addModule(levelTwoProject.getArtifactId());
createdModels.put(levelTwoProjectDirectory, levelTwoProject);
}
}

for (Map.Entry<Path, Model> entry : createdModels.entrySet()) {
modelWriter.write(entry.getKey().resolve("pom.xml").toFile(), Collections.emptyMap(), entry.getValue());
}

MavenProject project = mojoRule.readMavenProject(tempDirectory.toFile());

assertThat(
PomHelper.getChildModels(project, new SystemStreamLog()).entrySet(), hasSize(createdModels.size()));
} finally {
FileUtils.deleteDirectory(tempDirectory.toFile());
}
}

private Model createSimpleModel(String artifactId) {
Model module = new Model();
module.setGroupId("child.test");
module.setArtifactId(artifactId);
module.setVersion(DEFAULT_VERSION);
module.setPackaging("pom");
module.setModelVersion("4.0.0");
return module;
}
}
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
Expand Up @@ -158,4 +158,35 @@ public void testSetOldVersionMismatchProcessAllModules() throws Exception {
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
not(containsString("<version>bar</version>")));
}

private void testSetParameterValue(String filename) 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");
mojo.execute();
assertThat(
String.join("", Files.readAllLines(tempDir.resolve("pom.xml"))),
containsString("<version>testing</version>"));
}

@Test
public void testSetParameterValueSimple() throws Exception {
testSetParameterValue("pom-simple.xml");
}

@Test
public void testSetParameterValueBuildNumber() throws Exception {
testSetParameterValue("pom-build-number.xml");
}

@Test
public void testSetParameterValueUndefined() throws Exception {
testSetParameterValue("pom-undefined.xml");
}

@Test
public void testSetParameterValueMultipleProps() throws Exception {
testSetParameterValue("pom-multiple-props.xml");
}
}
@@ -0,0 +1,29 @@
<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.0.0-${buildNumber}-SNAPSHOT</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,0 +1,29 @@
<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.0.0-${buildNumber}-SNAPSHOT</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,0 +1,29 @@
<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,0 +1,29 @@
<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.0.0-${buildNumber}-SNAPSHOT</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 f75ae59

Please sign in to comment.