Skip to content

Commit

Permalink
Issue mojohaus#925: Protect against an NPE if a dependency version is…
Browse files Browse the repository at this point in the history
… defined in dependencyManagement and dependency processing is enabled
  • Loading branch information
andrzejj0 committed Feb 28, 2023
1 parent 35a2879 commit 179bbff
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.model.Dependency;
Expand Down Expand Up @@ -355,38 +356,43 @@ private void useDepVersion(

Artifact artifact = toArtifact(dep);
if (isIncluded(artifact)) {
if (!forceVersion) {
if (!getHelper().lookupArtifactVersions(artifact, false).containsVersion(depVersion)) {
throw new MojoExecutionException(String.format(
"Version %s is not available for artifact %s:%s",
depVersion, artifact.getGroupId(), artifact.getArtifactId()));
}
}
if (!propertyName.isPresent()) {
updateDependencyVersion(node.getModifiedPomXMLEventReader(), dep, depVersion, changeKind);
if (dep.getVersion() == null) {
getLog().warn(String.format("Not updating %s:%s in dependencies: version defined "
+ "in dependencyManagement", dep.getGroupId(), dep.getArtifactId()));
} else {
// propertyName is present
ofNullable(propertyConflicts.get(propertyName.get()))
.map(conflict -> {
getLog().warn("Cannot update property ${" + propertyName.get() + "}: "
+ "controls more than one dependency: "
+ conflict.stream()
.map(Dependency::getArtifactId)
.collect(Collectors.joining(", ")));
return false;
})
.orElseGet(() -> {
if (!updatePropertyValue(node, propertyName.get())) {
propertyBacklog.add(propertyName.get());
} else {
if (getLog().isDebugEnabled()) {
getLog().debug(String.format(
"Updated the %s property value to %s.",
propertyName.get(), depVersion));
if (!forceVersion) {
if (!getHelper().lookupArtifactVersions(artifact, false).containsVersion(depVersion)) {
throw new MojoExecutionException(String.format(
"Version %s is not available for artifact %s:%s",
depVersion, artifact.getGroupId(), artifact.getArtifactId()));
}
}
if (!propertyName.isPresent()) {
updateDependencyVersion(node.getModifiedPomXMLEventReader(), dep, depVersion, changeKind);
} else {
// propertyName is present
ofNullable(propertyConflicts.get(propertyName.get()))
.map(conflict -> {
getLog().warn("Cannot update property ${" + propertyName.get() + "}: "
+ "controls more than one dependency: "
+ conflict.stream()
.map(Dependency::getArtifactId)
.collect(Collectors.joining(", ")));
return false;
})
.orElseGet(() -> {
if (!updatePropertyValue(node, propertyName.get())) {
propertyBacklog.add(propertyName.get());
} else {
if (getLog().isDebugEnabled()) {
getLog().debug(String.format(
"Updated the %s property value to %s.",
propertyName.get(), depVersion));
}
}
}
return true;
});
return true;
});
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -24,6 +25,7 @@
import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.codehaus.mojo.versions.change.DefaultDependencyVersionChange;
import org.codehaus.mojo.versions.change.DefaultPropertyVersionChange;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.codehaus.mojo.versions.utils.TestUtils;
Expand Down Expand Up @@ -348,4 +350,85 @@ public void testPropertyFromParent() throws Exception {
assertThat(changeRecorder.getChanges(), empty());
assertThat(log.toString(), containsString("[WARN] Not updating property ${revision}: defined in parent"));
}

@Test
public void testVersionlessDependency() throws Exception {
Log logger = mock(Log.class);
StringBuilder log = new StringBuilder();
doAnswer(i -> log.append("[WARN] ").append(i.getArgument(0).toString()))
.when(logger)
.warn(anyString());
TestUtils.copyDir(
Paths.get("src/test/resources/org/codehaus/mojo/use-dep-version/issue-925"),
tempDir);
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());
setVariableValueToObject(mojo, "log", logger);
mojo.depVersion = "2.0.0";
mojo.forceVersion = true;
setVariableValueToObject(mojo, "processDependencies", true);
setVariableValueToObject(mojo, "processDependencyManagement", false);
setVariableValueToObject(mojo, "includes", new String[]{"default-group:artifactA"});

mojo.execute();

assertThat(changeRecorder.getChanges(), empty());
assertThat(log.toString(), containsString("[WARN] Not updating default-group:artifactA in dependencies: version defined in dependencyManagement"));
}

@Test
public void testDependencyManagemenent() throws Exception {
TestUtils.copyDir(
Paths.get("src/test/resources/org/codehaus/mojo/use-dep-version/issue-925"),
tempDir);
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.depVersion = "2.0.0";
mojo.forceVersion = true;
setVariableValueToObject(mojo, "processDependencies", false);
setVariableValueToObject(mojo, "processDependencyManagement", true);
setVariableValueToObject(mojo, "includes", new String[]{"default-group:artifactA"});

mojo.execute();

assertThat(
changeRecorder.getChanges(), hasItem(new DefaultDependencyVersionChange("default-group",
"artifactA", "1.0.0", "2.0.0")));
}

@Test
public void testVersionDefinedInDependencyManagemenent() throws Exception {
Log logger = mock(Log.class);
StringBuilder log = new StringBuilder();
doAnswer(i -> log.append("[WARN] ").append(i.getArgument(0).toString()))
.when(logger)
.warn(anyString());
TestUtils.copyDir(
Paths.get("src/test/resources/org/codehaus/mojo/use-dep-version/issue-925"),
tempDir);
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());
setVariableValueToObject(mojo, "log", logger);
mojo.depVersion = "2.0.0";
mojo.forceVersion = true;
setVariableValueToObject(mojo, "processDependencies", true);
setVariableValueToObject(mojo, "processDependencyManagement", true);
setVariableValueToObject(mojo, "includes", new String[]{"default-group:artifactA"});

mojo.execute();

assertThat(log.toString(), containsString("[WARN] Not updating default-group:artifactA in dependencies: version defined in dependencyManagement"));
assertThat(
changeRecorder.getChanges(), hasItem(new DefaultDependencyVersionChange("default-group",
"artifactA", "1.0.0", "2.0.0")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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>

<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactA</artifactId>
</dependency>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactB</artifactId>
</dependency>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactC</artifactId>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactA</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactB</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>default-group</groupId>
<artifactId>artifactC</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

0 comments on commit 179bbff

Please sign in to comment.