diff --git a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java index 363446d51..ab056ba65 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -20,6 +20,7 @@ import javax.inject.Inject; +import java.io.File; import java.util.List; import java.util.Set; @@ -112,10 +113,13 @@ public void execute() throws MojoExecutionException { Set artifacts = project.getArtifacts(); for (Artifact artifact : artifacts) { - project.getProperties() - .setProperty( - artifact.getDependencyConflictId(), - artifact.getFile().getAbsolutePath()); + String conflictId = artifact.getDependencyConflictId(); + File file = artifact.getFile(); + if (file == null) { + getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); + continue; + } + project.getProperties().setProperty(conflictId, file.getAbsolutePath()); } if (extraArtifacts != null) { @@ -130,10 +134,14 @@ public void execute() throws MojoExecutionException { resolverUtil.createArtifactFromParams(paramArtifact); artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories()); - this.project - .getProperties() - .setProperty( - toConflictId(artifact), artifact.getFile().getAbsolutePath()); + String conflictId = toConflictId(artifact); + File file = artifact.getFile(); + if (file == null) { + getLog().warn("Extra artifact " + conflictId + + " has no associated file; no property will be set for it."); + continue; + } + this.project.getProperties().setProperty(conflictId, file.getAbsolutePath()); } } catch (ArtifactResolutionException | ArtifactDescriptorException e) { throw new MojoExecutionException("Couldn't download artifact: " + e.getMessage(), e); diff --git a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java index 3d163d5c5..59f34b454 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -35,6 +35,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; @@ -102,4 +103,17 @@ void testSetPropertiesForExtractArtifacts(PropertiesMojo mojo) throws Exception assertTrue(project.getProperties().containsKey(depId2)); assertTrue(new File(project.getProperties().getProperty(depId1)).exists()); } + + @Test + @InjectMojo(goal = "properties") + void testNullFileSkipsProperty(PropertiesMojo mojo) throws Exception { + Artifact artifact = Mockito.mock(Artifact.class); + when(artifact.getDependencyConflictId()).thenReturn("group:artifact"); + when(artifact.getFile()).thenReturn(null); + when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(artifact))); + + mojo.execute(); + + assertFalse(project.getProperties().containsKey("group:artifact")); + } }