Repository navigation
Conversation
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
left a comment
There was a problem hiding this comment.
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 ifmojoExecutionwere non-null. - The
&& mojoExecution != nullguard on thestartsWith("mojo")branch is correct —mojo/mojoExecutionexact-match returnsnull(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 != nullguards on bothpluginbranches are correct — they convert what was a guaranteed NPE into anullreturn.
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 Sorry, I meant 3.10.x, could you retarget PR #13505 to that branch ? |
…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
#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
…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
Fixes #8052
What and why
PluginParameterExpressionEvaluator(MavenSession)creates the evaluator without aMojoExecution, butevaluate()calledmojoExecution.getMojoDescriptor()for every expression. Any${...}therefore failed with aNullPointerException, 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.*andplugin.*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
PluginParameterExpressionEvaluatorTesthas two new tests that use the single argument constructor: one for the expressions which do not depend on a mojo execution, one for themojoandpluginexpressions. Both fail on master with theNullPointerExceptionof the issue and pass with the change, three runs in a row.maven-corepasses (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: