Skip dependencies with no resolved artifact file instead of throwing NPE - #386
Open
kratos0718 wants to merge 1 commit into
Open
Skip dependencies with no resolved artifact file instead of throwing NPE#386kratos0718 wants to merge 1 commit into
kratos0718 wants to merge 1 commit into
Conversation
copyProperties() registered one path property per dependency by calling artifact.getFile().getPath() with no null check. An artifact's file is null when it was never resolved -- under partial or offline resolution, for instance -- so any such dependency aborted the whole mojo with a bare NullPointerException that named nothing, leaving no hint which dependency caused it. Such dependencies are now skipped with a warning naming the artifact, so the property is simply absent and a build that never references it still runs. One unresolvable dependency also no longer prevents the remaining ones from being registered. The loop moves into a package-private setDependencyFileProperties() so it can be tested: copyProperties() itself needs a MavenSession and a POM file, and the project has no mocking framework, so testing it directly would have meant adding one.
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.
Fixes #372.
Problem
copyProperties(MavenProject, Project)registers one path property per dependency:artifact.getFile()isnullwhen the artifact was never resolved — partial or offline resolution, for example — so the mojo aborts with a bareNullPointerExceptionthat names nothing:Nothing in that tells the user which dependency is at fault.
Fix
Skip such dependencies with a warning that names the artifact:
The property is simply absent, so a build that never references it still runs, and one unresolvable dependency no longer prevents the remaining ones from being registered.
Why skip rather than throw
The issue notes
getPathFromArtifactsthrowsDependencyResolutionRequiredExceptionin the same situation, and consistency with it would be reasonable. I did not do that here becausecopyPropertiesis public and does not declarethrows— adding a checked exception to it would break any caller outside this plugin. Skipping fixes the crash without touching the published signature.If you would rather have the failure, I am happy to switch it: the natural shape would be deprecating the current
copyPropertiesand adding one declared to throw, which is a larger change than this issue needs. Your call.Tests
The loop moved into a package-private
setDependencyFileProperties(Set<Artifact>, Project)so it can be tested at all —copyPropertiesitself requires aMavenSessionand a POM file, and this project has no mocking framework, so testing it directly would have meant adding one as a dependency. The extraction keeps the public API unchanged.Three cases, using
DefaultArtifact(whosegetFile()is null unless set), so no mocks are needed:unresolvedDependencyIsSkippedInsteadOfThrowingresolvedDependenciesStillGetTheirPathPropertynullArtifactSetIsToleratedThe second lists the unresolved artifact first, so it also proves one bad dependency no longer blocks the rest.