Skip to content

throw MojoExecutionException when artifact has no file in PropertiesMojo#1655

Open
elharo wants to merge 2 commits into
masterfrom
fix-1647-v2
Open

throw MojoExecutionException when artifact has no file in PropertiesMojo#1655
elharo wants to merge 2 commits into
masterfrom
fix-1647-v2

Conversation

@elharo

@elharo elharo commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

PropertiesMojo sets project properties to artifact file paths. artifact.getFile() can return null for unresolved or POM-packaged artifacts, and calling getAbsolutePath() on null throws NPE with an unhelpful stack trace. Since Properties.setProperty() also throws NPE on null values, there is no way to represent "no file" through the property mechanism.

Throw MojoExecutionException with a clear diagnostic message at both potential NPE sites instead.

Includes a failing (at HEAD) unit test that verifies MojoExecutionException is thrown when an artifact has no file.

fixes #1647

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens dependency:properties by replacing potential NullPointerExceptions (from artifact.getFile() being null) with explicit MojoExecutionExceptions, and adds a regression test to ensure the behavior is enforced.

Changes:

  • Add null checks for Artifact#getFile() when iterating project.getArtifacts() and when handling configured extraArtifacts.
  • Throw MojoExecutionException with a diagnostic message instead of NPEs at both former failure points.
  • Add a unit test that verifies MojoExecutionException is thrown when an artifact has no associated file.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/main/java/org/apache/maven/plugins/dependency/PropertiesMojo.java Adds null-file guards and throws MojoExecutionException with a diagnostic message instead of NPE.
src/test/java/org/apache/maven/plugins/dependency/TestPropertiesMojo.java Adds a regression test asserting the mojo throws when an artifact file is null.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +116 to +121
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());
Comment on lines +136 to +141
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());
when(artifact.getFile()).thenReturn(null);
when(this.project.getArtifacts()).thenReturn(new HashSet<>(Arrays.asList(artifact)));

assertThrows(MojoExecutionException.class, mojo::execute);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential NPE in PropertiesMojo when artifact.getFile() returns null

2 participants