From 60ef287060181e7de4fd479a5ae84855206584c2 Mon Sep 17 00:00:00 2001 From: elharo Date: Tue, 7 Jul 2026 15:37:27 +0000 Subject: [PATCH 1/2] throw MojoExecutionException when artifact has no file in PropertiesMojo --- .../plugins/dependency/PropertiesMojo.java | 21 ++++++++++++------- .../dependency/TestPropertiesMojo.java | 13 ++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) 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..c5ce86b59 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,12 @@ public void execute() throws MojoExecutionException { Set artifacts = project.getArtifacts(); for (Artifact artifact : artifacts) { - project.getProperties() - .setProperty( - artifact.getDependencyConflictId(), - artifact.getFile().getAbsolutePath()); + File file = artifact.getFile(); + if (file == null) { + throw new MojoExecutionException("Could not resolve artifact " + artifact.getDependencyConflictId() + + " to a file; the dependency:properties goal requires a resolved artifact."); + } + project.getProperties().setProperty(artifact.getDependencyConflictId(), file.getAbsolutePath()); } if (extraArtifacts != null) { @@ -130,10 +133,12 @@ public void execute() throws MojoExecutionException { resolverUtil.createArtifactFromParams(paramArtifact); artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories()); - this.project - .getProperties() - .setProperty( - toConflictId(artifact), artifact.getFile().getAbsolutePath()); + File file = artifact.getFile(); + if (file == null) { + throw new MojoExecutionException("Could not resolve extra artifact " + toConflictId(artifact) + + " to a file; the dependency:properties goal requires a resolved artifact."); + } + this.project.getProperties().setProperty(toConflictId(artifact), 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..0b9f24f07 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -29,12 +29,14 @@ import org.apache.maven.api.plugin.testing.MojoExtension; import org.apache.maven.api.plugin.testing.MojoTest; import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.dependency.utils.ParamArtifact; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.ReflectionUtils; import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; @@ -102,4 +104,15 @@ void testSetPropertiesForExtractArtifacts(PropertiesMojo mojo) throws Exception assertTrue(project.getProperties().containsKey(depId2)); assertTrue(new File(project.getProperties().getProperty(depId1)).exists()); } + + @Test + @InjectMojo(goal = "properties") + void testNullFileThrowsException(PropertiesMojo mojo) { + 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))); + + assertThrows(MojoExecutionException.class, mojo::execute); + } } From ea7218eb4077bc7cea5d11254e062e231b4c04e2 Mon Sep 17 00:00:00 2001 From: elharo Date: Tue, 7 Jul 2026 16:15:36 +0000 Subject: [PATCH 2/2] warn and skip instead of throwing MojoExecutionException for null-file artifacts --- .../maven/plugins/dependency/PropertiesMojo.java | 15 +++++++++------ .../plugins/dependency/TestPropertiesMojo.java | 9 +++++---- 2 files changed, 14 insertions(+), 10 deletions(-) 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 c5ce86b59..ab056ba65 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -113,12 +113,13 @@ public void execute() throws MojoExecutionException { Set artifacts = project.getArtifacts(); for (Artifact artifact : artifacts) { + String conflictId = artifact.getDependencyConflictId(); File file = artifact.getFile(); if (file == null) { - throw new MojoExecutionException("Could not resolve artifact " + artifact.getDependencyConflictId() - + " to a file; the dependency:properties goal requires a resolved artifact."); + getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); + continue; } - project.getProperties().setProperty(artifact.getDependencyConflictId(), file.getAbsolutePath()); + project.getProperties().setProperty(conflictId, file.getAbsolutePath()); } if (extraArtifacts != null) { @@ -133,12 +134,14 @@ public void execute() throws MojoExecutionException { resolverUtil.createArtifactFromParams(paramArtifact); artifact = resolverUtil.resolveArtifact(artifact, project.getRemoteProjectRepositories()); + String conflictId = toConflictId(artifact); File file = artifact.getFile(); if (file == null) { - throw new MojoExecutionException("Could not resolve extra artifact " + toConflictId(artifact) - + " to a file; the dependency:properties goal requires a resolved artifact."); + getLog().warn("Extra artifact " + conflictId + + " has no associated file; no property will be set for it."); + continue; } - this.project.getProperties().setProperty(toConflictId(artifact), file.getAbsolutePath()); + 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 0b9f24f07..59f34b454 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java @@ -29,14 +29,13 @@ import org.apache.maven.api.plugin.testing.MojoExtension; import org.apache.maven.api.plugin.testing.MojoTest; import org.apache.maven.artifact.Artifact; -import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.dependency.utils.ParamArtifact; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.ReflectionUtils; import org.junit.jupiter.api.Test; import org.mockito.Mockito; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; @@ -107,12 +106,14 @@ void testSetPropertiesForExtractArtifacts(PropertiesMojo mojo) throws Exception @Test @InjectMojo(goal = "properties") - void testNullFileThrowsException(PropertiesMojo mojo) { + 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))); - assertThrows(MojoExecutionException.class, mojo::execute); + mojo.execute(); + + assertFalse(project.getProperties().containsKey("group:artifact")); } }