You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2026/10/14 21:32:49 UTC

(maven) branch maven-3.10.x updated: [Backport 3.10.x] fix: Evaluate expressions without a mojo execution (fix #8052)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch maven-3.10.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.10.x by this push:
     new 380143fcc4 [Backport 3.10.x] fix: Evaluate expressions without a mojo execution (fix #8052)
380143fcc4 is described below

commit 380143fcc4295cc5452f855df7fc11b118ab06fa
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Wed Oct 14 23:32:43 2026 +0200

    [Backport 3.10.x] fix: Evaluate expressions without a mojo execution (fix #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
---
 .../plugin/PluginParameterExpressionEvaluator.java |  9 +++---
 .../PluginParameterExpressionEvaluatorTest.java    | 36 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java
index b81f91bc95..b3697ea89e 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluator.java
@@ -176,7 +176,8 @@ public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationEx
             return expression.replace("$$", "$");
         }
 
-        MojoDescriptor mojoDescriptor = mojoExecution.getMojoDescriptor();
+        // the evaluator may be created without a mojo execution, see the single argument constructor
+        MojoDescriptor mojoDescriptor = mojoExecution != null ? mojoExecution.getMojoDescriptor() : null;
 
         if ("localRepository".equals(expression)) {
             value = session.getLocalRepository();
@@ -226,7 +227,7 @@ public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationEx
             value = session.getRepositorySession();
         } else if (expression.equals("mojo")) {
             value = mojoExecution;
-        } else if (expression.startsWith("mojo")) {
+        } else if (expression.startsWith("mojo") && mojoExecution != null) {
             try {
                 int pathSeparator = expression.indexOf('/');
 
@@ -243,8 +244,8 @@ public Object evaluate(String expr, Class<?> type) throws ExpressionEvaluationEx
                         "Error evaluating plugin parameter expression: " + expression, e);
             }
         } else if (expression.equals("plugin")) {
-            value = mojoDescriptor.getPluginDescriptor();
-        } else if (expression.startsWith("plugin")) {
+            value = mojoDescriptor != null ? mojoDescriptor.getPluginDescriptor() : null;
+        } else if (expression.startsWith("plugin") && mojoDescriptor != null) {
             try {
                 int pathSeparator = expression.indexOf('/');
 
diff --git a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java
index 9bee456813..82655b47c5 100644
--- a/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java
+++ b/maven-core/src/test/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorTest.java
@@ -407,6 +407,42 @@ private MavenProject createDefaultProject() {
         return new MavenProject(new Model());
     }
 
+    /**
+     * An evaluator created without a mojo execution still evaluates the expressions which do not depend on one.
+     */
+    @Test
+    void testEvaluationWithoutMojoExecution() throws Exception {
+        String sysprop = "PPEET_without_mojo_execution";
+        Properties executionProperties = new Properties();
+        executionProperties.setProperty(sysprop, "value");
+        MavenSession session = createSession(
+                factory.createLocalRepository(null, RepositorySystem.defaultUserLocalRepository), executionProperties);
+
+        ExpressionEvaluator ee = new PluginParameterExpressionEvaluator(session);
+
+        assertEquals("value", ee.evaluate("${" + sysprop + "}"));
+        assertEquals("prefix-value-suffix", ee.evaluate("prefix-${" + sysprop + "}-suffix"));
+        assertSame(session, ee.evaluate("${session}"));
+        assertNull(ee.evaluate("${PPEET_nonexisting_property}"));
+    }
+
+    /**
+     * Without a mojo execution there is no mojo and no plugin to evaluate an expression against, which
+     * yields no value rather than a failure.
+     */
+    @Test
+    void testMojoAndPluginExpressionsWithoutMojoExecution() throws Exception {
+        MavenSession session = createSession(
+                factory.createLocalRepository(null, RepositorySystem.defaultUserLocalRepository), new Properties());
+
+        ExpressionEvaluator ee = new PluginParameterExpressionEvaluator(session);
+
+        assertNull(ee.evaluate("${mojo}"));
+        assertNull(ee.evaluate("${mojo.goal}"));
+        assertNull(ee.evaluate("${plugin}"));
+        assertNull(ee.evaluate("${plugin.artifactId}"));
+    }
+
     private ExpressionEvaluator createExpressionEvaluator(
             MavenProject project, PluginDescriptor pluginDescriptor, Properties executionProperties) throws Exception {
         ArtifactRepository repo = factory.createLocalRepository(null, RepositorySystem.defaultUserLocalRepository);