Skip to content

Evaluate expressions without a mojo execution (fix #8052) - #13497

Merged
gnodet merged 1 commit into
apache:masterfrom
arturobernalg:fix/maven-8052-evaluator-without-mojo-execution
Oct 14, 2026
Merged

gnodet merged 1 commit into
apache:masterfrom
arturobernalg:fix/maven-8052-evaluator-without-mojo-execution

Conversation

@arturobernalg

@arturobernalg arturobernalg commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Fixes #8052

What and why

PluginParameterExpressionEvaluator(MavenSession) creates the evaluator without a MojoExecution, but evaluate() called mojoExecution.getMojoDescriptor() for every expression. Any ${...} therefore failed with a NullPointerException, as reported in the issue by a tool that uses the evaluator outside a mojo execution.

How

The mojo descriptor is only read when there is a mojo execution. The mojo.* and plugin.* expressions need one and have no value without it, so they evaluate to nothing, like any unknown expression, and fall back to the properties lookup. All other expressions (session, project, basedir, user, system and project properties, nested expressions) are evaluated as before.

Tests

PluginParameterExpressionEvaluatorTest has two new tests that use the single argument constructor: one for the expressions which do not depend on a mojo execution, one for the mojo and plugin expressions. Both fail on master with the NullPointerException of the issue and pass with the change, three runs in a row. maven-core passes (732 tests) and spotless is clean. I did not run the integration tests.


To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
check one of the following:

PluginParameterExpressionEvaluator(MavenSession) passes no MojoExecution, but evaluate() dereferenced it for every expression, so any ${...} failed with a NullPointerException. Only look at the mojo execution for the mojo and plugin expressions, which have no value without one, and evaluate everything else as before.

Fixes apache#8052

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clean, minimal null-safety fix that correctly guards all mojoExecution/mojoDescriptor dereferences in evaluate().

What this does: The single-argument PluginParameterExpressionEvaluator(session) constructor has always existed but the evaluate() method unconditionally called mojoExecution.getMojoDescriptor() at line 166, causing an NPE whenever the evaluator was used without a mojo execution context. The fix adds the three guards that were clearly missing.

Correctness:

  • The null check at the top (mojoDescriptor = mojoExecution != null ? mojoExecution.getMojoDescriptor() : null) is correct — it mirrors exactly what the old code would have done if mojoExecution were non-null.
  • The && mojoExecution != null guard on the startsWith("mojo") branch is correct — mojo/mojoExecution exact-match returns null (the value itself), which is the right semantic; the sub-property path (mojo.goal) can't be resolved without an execution, so falling through to properties lookup (returning null) is the right behaviour.
  • The mojoDescriptor != null guards on both plugin branches are correct — they convert what was a guaranteed NPE into a null return.

Tests: Both new tests are well-targeted and would NPE on master at the old line 166. testEvaluationWithoutMojoExecution confirms session/system-property expressions still resolve, and testMojoAndPluginExpressionsWithoutMojoExecution confirms mojo/plugin expressions yield null rather than throwing.

Backport note: The bug exists on maven-3.9.x and maven-4.0.x as well — backport labels should be added.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@arturobernalg

Copy link
Copy Markdown
Member Author

Thanks for the review. I checked that the bug also reproduces on both branches, with the same NullPointerException, and opened the backports: #13504 for maven-4.0.x and #13505 for maven-3.9.x. Could you add the backport labels?

@gnodet

gnodet commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Thanks for the review. I checked that the bug also reproduces on both branches, with the same NullPointerException, and opened the backports: #13504 for maven-4.0.x and #13505 for maven-3.9.x. Could you add the backport labels?

@arturobernalg Sorry, I meant 3.10.x, could you retarget PR #13505 to that branch ?

arturobernalg added a commit to arturobernalg/maven that referenced this pull request Oct 14, 2026
…pache#8052)

PluginParameterExpressionEvaluator(MavenSession) passes no MojoExecution, but evaluate() dereferenced it for every expression, so any ${...} failed with a NullPointerException. Only look at the mojo execution for the mojo and plugin expressions, which have no value without one, and evaluate everything else as before.

Backport of the change to master, see apache#13497.

Fixes apache#8052
@gnodet gnodet added bug Something isn't working java Pull requests that update Java code labels Oct 14, 2026
@gnodet gnodet added this to the 4.1.0 milestone Oct 14, 2026
@gnodet
gnodet merged commit dd81550 into apache:master Oct 14, 2026
26 checks passed
gnodet pushed a commit that referenced this pull request Oct 14, 2026
 #8052)

Backport of #13497 to `maven-4.0.x`.

- `PluginParameterExpressionEvaluator(MavenSession)` failed with NullPointerException when evaluate() was called without a MojoExecution
- Fix: guard the mojo descriptor read behind a null-check; `mojo.*` and `plugin.*` expressions return null/empty when no execution is present
- Two new tests in PluginParameterExpressionEvaluatorTest cover the no-execution constructor; both fail on maven-4.0.x before the fix and pass after
gnodet pushed a commit that referenced this pull request Oct 14, 2026
…ix #8052)

Backport of #13497 to `maven-3.10.x`.

- `PluginParameterExpressionEvaluator(MavenSession)` failed with NullPointerException when evaluate() was called without a MojoExecution
- Fix: guard the mojo descriptor read behind a null-check; `mojo.*` and `plugin.*` expressions return null/empty when no execution is present
- Two new tests in PluginParameterExpressionEvaluatorTest cover the no-execution constructor; both fail on maven-3.10.x before the fix and pass after
- Code of the 3.10.x branch differs slightly from master; the fix was applied by hand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[MNG-6982] PluginParameterExpressionEvaluator fails with NPE if created without MojoExecution

3 participants