Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - A Maven plugin fails and to open an issue, I would like to explain inside how the NullPointerException encountered can appear

While compiling an opensource project, I encounter a problem with a Maven plugin that fails.
And I would like to open an issue about its problem. To be helpful to those who will search to solve it, I would like to explain how it might happen.

Execution default of goal pl.project13.maven:git-commit-id-plugin:4.0.3:revision failed. (pl.project13.maven:git-commit-id-plugin:4.0.3:revision:default:initialize)

org.apache.maven.plugin.PluginExecutionException: Execution default of goal pl.project13.maven:git-commit-id-plugin:4.0.3:revision failed.
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:148)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:332)
    at org.eclipse.m2e.core.internal.embedder.MavenImpl.lambda$8(MavenImpl.java:1380)
    at [...]
Caused by: java.lang.NullPointerException
    at pl.project13.maven.git.GitCommitIdMojo.execute(GitCommitIdMojo.java:441)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    ... 30 more

For that, I've red the involved line that causes the NullPointerException on its Git repository.
The content of the line 441 of GitCommitIdMojo.java is :

List<MavenProject> sortedProjects =
   Optional.ofNullable(session.getProjectDependencyGraph())
       .map(graph -> graph.getSortedProjects())
       .orElseGet(() -> {
          log.warn("Maven's dependency graph is null. Assuming project is the only one executed.");
          return Collections.singletonList(session.getCurrentProject());
       });

But I'm not so experienced with Optional and other inline functions. And I wonder from where the NullPointerException is really coming. I have no way to test it in Live, so I can only guess.

  1. I've checked on the source file : session is a required attribute. It shouldn't be null.

  2. I believe that the Optional.ofNullable(...) protects the content inside parenthesis and that the NullPointerException isn't coming from here.

  3. But when entering the .orElseGet(...) part of the expression, it might fail on Collections.singletonList(session.getCurrentProject()) where session.getCurrentProject() would return null.

Am I right?
What other parts of this statement could lead to a NullPointerException under what conditions?

question from:https://stackoverflow.com/questions/65926853/a-maven-plugin-fails-and-to-open-an-issue-i-would-like-to-explain-inside-how-th

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're not looking at the right version of the code. The maven-error refers to 4.0.3 of git-commit-id-plugin, but the code you're displaying comes from the master branch, which is at 4.0.4-SNAPSHOT.

This is line 441 in version 4.0.3:

List sortedProjects = session.getProjectDependencyGraph().getSortedProjects();

The NullPointer can come from any of the three parts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...