From 60ef287060181e7de4fd479a5ae84855206584c2 Mon Sep 17 00:00:00 2001 From: elharo Date: Tue, 7 Jul 2026 15:37:27 +0000 Subject: [PATCH 1/4] 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/4] 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")); } } From eadc43fccde89b5d43fc1714767d1616889a4cd0 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sun, 23 Aug 2026 13:44:37 -0400 Subject: [PATCH 3/4] improve --- .../maven/plugins/dependency/PropertiesMojo.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 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 ab056ba65..129d1d7d5 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -45,13 +45,11 @@ * @author Paul Gier * @since 2.2 */ -// CHECKSTYLE_OFF: LineLength @Mojo( name = "properties", requiresDependencyResolution = ResolutionScope.TEST, defaultPhase = LifecyclePhase.INITIALIZE, threadSafe = true) -// CHECKSTYLE_ON: LineLength public class PropertiesMojo extends AbstractMojo { /** @@ -115,11 +113,12 @@ public void execute() throws MojoExecutionException { for (Artifact artifact : artifacts) { 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; + if (file != null) { + project.getProperties().setProperty(conflictId, file.getAbsolutePath()); + } + else { + getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); } - project.getProperties().setProperty(conflictId, file.getAbsolutePath()); } if (extraArtifacts != null) { From 8857a123913df85eb27d17290003cc4ebcc6828e Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Sun, 23 Aug 2026 13:49:33 -0400 Subject: [PATCH 4/4] improve --- .../plugins/dependency/PropertiesMojo.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 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 129d1d7d5..5c694836b 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java @@ -114,17 +114,15 @@ public void execute() throws MojoExecutionException { String conflictId = artifact.getDependencyConflictId(); File file = artifact.getFile(); if (file != null) { - project.getProperties().setProperty(conflictId, file.getAbsolutePath()); - } - else { - getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); + project.getProperties().setProperty(conflictId, file.getAbsolutePath()); + } else { + getLog().warn("Artifact " + conflictId + " has no associated file; no property will be set for it."); } } if (extraArtifacts != null) { try { for (ParamArtifact paramArtifact : extraArtifacts) { - if (!paramArtifact.isDataSet()) { throw new MojoExecutionException("You must specify an artifact OR GAV separately"); } @@ -135,12 +133,13 @@ public void execute() throws MojoExecutionException { String conflictId = toConflictId(artifact); File file = artifact.getFile(); - if (file == null) { - getLog().warn("Extra artifact " + conflictId + if (file != null) { + this.project.getProperties().setProperty(conflictId, file.getAbsolutePath()); + } + else { + 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);