Skip to content

Commit

Permalink
Resolves mojohaus#474: Using raw model for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Dec 18, 2022
1 parent f034c73 commit d6c01d1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -94,17 +95,17 @@ protected void update(ModifiedPomXMLEventReader pom)
}

try {
Model rawModel = PomHelper.getRawModel(getProject());
if (isProcessingDependencyManagement()) {
DependencyManagement dependencyManagement =
PomHelper.getRawModel(getProject()).getDependencyManagement();
DependencyManagement dependencyManagement = rawModel.getDependencyManagement();
if (dependencyManagement != null) {
useDepVersion(
pom, dependencyManagement.getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY_MANAGEMENT);
}
}

if (getProject().getDependencies() != null && isProcessingDependencies()) {
useDepVersion(pom, getProject().getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY);
if (rawModel.getDependencies() != null && isProcessingDependencies()) {
useDepVersion(pom, rawModel.getDependencies(), ChangeRecord.ChangeKind.DEPENDENCY);
}

if (getProject().getParent() != null && isProcessingParent()) {
Expand All @@ -124,11 +125,6 @@ private void useDepVersion(
continue;
}

if (isHandledByProperty(dep)) {
getLog().debug("Ignoring dependency with property as version: " + toString(dep));
continue;
}

Artifact artifact = this.toArtifact(dep);

if (isIncluded(artifact)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@
* under the License.
*/

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.codehaus.mojo.versions.utils.TestUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;

/**
* Basic tests for {@linkplain UseDepVersionMojo}.
*
Expand All @@ -36,16 +49,51 @@ public class UseDepVersionMojoTest extends AbstractMojoTestCase {
@Rule
public MojoRule mojoRule = new MojoRule(this);

private Path tempDir;

@Before
public void setUp() throws Exception {
super.setUp();
tempDir = TestUtils.createTempDir("use-dep-version");
}

@After
public void tearDown() throws Exception {
try {
TestUtils.tearDownTempDir(tempDir);
} finally {
super.tearDown();
}
}

@Test
public void testIssue673() throws Exception {
UseDepVersionMojo mojo = (UseDepVersionMojo) mojoRule.lookupConfiguredMojo(
new File("target/test-classes/org/codehaus/mojo/use-dep-version/issue-637"), "use-dep-version");
setVariableValueToObject(mojo, "processDependencies", true);
setVariableValueToObject(mojo, "processDependencyManagement", true);
setVariableValueToObject(mojo, "excludeReactor", true);
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/use-dep-version/issue-637"), tempDir);
UseDepVersionMojo mojo = (UseDepVersionMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "use-dep-version");
setVariableValueToObject(mojo, "serverId", "serverId");
setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(mojo.getProject()));

mojo.execute();
}

@Test
public void testParameters() throws Exception {
TestUtils.copyDir(Paths.get("src/test/resources/org/codehaus/mojo/use-dep-version/issue-474"), tempDir);
TestChangeRecorder changeRecorder = new TestChangeRecorder();
UseDepVersionMojo mojo = (UseDepVersionMojo) mojoRule.lookupConfiguredMojo(tempDir.toFile(), "use-dep-version");
setVariableValueToObject(mojo, "reactorProjects", Collections.singletonList(mojo.getProject()));
setVariableValueToObject(mojo, "repositorySystem", mockRepositorySystem());
setVariableValueToObject(mojo, "aetherRepositorySystem", mockAetherRepositorySystem());
setVariableValueToObject(mojo, "changeRecorders", changeRecorder.asTestMap());

mojo.execute();

assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "artifactA", "${revision}", "2.0.0")));

assertThat(
String.join("", Files.readAllLines(tempDir.resolve(Paths.get("pom.xml")))),
containsString("<version>2.0.0</version>"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>

<groupId>test-group</groupId>
<artifactId>test-artifact</artifactId>
<version>DEVELOP-SNAPSHOT</version>

<properties>
<revision>1.0.0-SNAPSHOT</revision>
</properties>

<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactA</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<serverId>serverId</serverId>
<includesList>default-group</includesList>
<depVersion>2.0.0</depVersion>
</configuration>
</plugin>
</plugins>
</build>

</project>

0 comments on commit d6c01d1

Please sign in to comment.