throw MojoExecutionException when artifact has no file in PropertiesMojo#1655
Open
elharo wants to merge 2 commits into
Open
throw MojoExecutionException when artifact has no file in PropertiesMojo#1655elharo wants to merge 2 commits into
elharo wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
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
nullchecks forArtifact#getFile()when iteratingproject.getArtifacts()and when handling configuredextraArtifacts. - Throw
MojoExecutionExceptionwith a diagnostic message instead of NPEs at both former failure points. - Add a unit test that verifies
MojoExecutionExceptionis 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PropertiesMojosets project properties to artifact file paths.artifact.getFile()can returnnullfor unresolved or POM-packaged artifacts, and callinggetAbsolutePath()on null throws NPE with an unhelpful stack trace. SinceProperties.setProperty()also throws NPE on null values, there is no way to represent "no file" through the property mechanism.Throw
MojoExecutionExceptionwith a clear diagnostic message at both potential NPE sites instead.Includes a failing (at HEAD) unit test that verifies
MojoExecutionExceptionis thrown when an artifact has no file.fixes #1647