You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[MNG-6641] NPE from AttachedArtifact.getVersion rather than meaningful error #8393
This issue is basically re-reporting issue https://issues.apache.org/jira/browse/MNG-4731, which was closed in the great Jira clean up of 2014. 5 years later I can report it is still an issue.
The issue occurs when the information passed to AttachedArtifact violates any of the validations in DefaultArtifact.validateIdentity(). The call to getVersion() will throw an NPE because it is overridable in the DefaultArtifact and parent has not been initialized yet in AttachedArtifact. MNG-4731 explains this more clearly.
A comment on that ticket suggests that the problem is that AttachedArtifact was called directly instead of using MavenProjectHelper.attachArtifact(). That is not true as can be seen from my stack trace:
Caused by: java.lang.NullPointerException
at org.apache.maven.project.artifact.AttachedArtifact.getVersion (AttachedArtifact.java:138)
at org.apache.maven.artifact.DefaultArtifact.validateIdentity (DefaultArtifact.java:149)
at org.apache.maven.artifact.DefaultArtifact.<init> (DefaultArtifact.java:124)
at org.apache.maven.project.artifact.AttachedArtifact.<init> (AttachedArtifact.java:49)
at org.apache.maven.project.DefaultMavenProjectHelper.attachArtifact (DefaultMavenProjectHelper.java:63)
at org.apache.maven.plugins.source.AbstractSourceJarMojo.packageSources (AbstractSourceJarMojo.java:324)
at org.apache.maven.plugins.source.AbstractSourceJarMojo.packageSources (AbstractSourceJarMojo.java:253)
at org.apache.maven.plugins.source.AbstractSourceJarMojo.execute (AbstractSourceJarMojo.java:216)
Affects: 3.6.0
Issue Links:
MNG-4731 NPE from AttachedArtifact.getVersion rather than meaningful error
("duplicates")
I don't have a project I can share to reproduce it but I can tell you what caused the validation failure to begin with. We have our own plugin that tries to override the version coming from the POM with one that it computes based on a number of factors. And it had these lines:
The code for DefaultArtifact.setVersion actually nulls out the version range that we set on the line before. The source plugin uses AttachedArtifact and AttachedArtifact basically will not work without a version range. The validations here check to make sure that either versionRange or version is non-null, but because this is called from the constructor version will ALWAYS be null so the only way to pass the validations is to have a versionRange.
We were able to fix our problem by switching from calling setVersion to calling selectVersion which does not null out the versionRange, but the problem with the validations throwing an NPE instead of an intelligent message is still there.
So to summarize a reproduction would be to simply call
Activity
jira-importer commentedon Apr 16, 2019
Michael Osipov commented
Please provide a reproducer.
jira-importer commentedon Apr 16, 2019
Dale King commented
I don't have a project I can share to reproduce it but I can tell you what caused the validation failure to begin with. We have our own plugin that tries to override the version coming from the POM with one that it computes based on a number of factors. And it had these lines:
project.getArtifact().setVersionRange(VersionRange.createFromVersion(version));project.getArtifact().setVersion(version);The code for DefaultArtifact.setVersion actually nulls out the version range that we set on the line before. The source plugin uses AttachedArtifact and AttachedArtifact basically will not work without a version range. The validations here check to make sure that either versionRange or version is non-null, but because this is called from the constructor version will ALWAYS be null so the only way to pass the validations is to have a versionRange.
We were able to fix our problem by switching from calling setVersion to calling selectVersion which does not null out the versionRange, but the problem with the validations throwing an NPE instead of an intelligent message is still there.
So to summarize a reproduction would be to simply call
project.getArtifact().setVersion(project.getArtifact().getVersion());in a plugin and also invoke the maven-source-plugin.