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

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


The following commit(s) were added to refs/heads/master by this push:
     new 27232de0ef Fix V4MojoConfigurator to assign expression values of the 
parameter type directly (#14011)
27232de0ef is described below

commit 27232de0ef1a676892715a5a744c25df9493e65b
Author: xh123637 <[email protected]>
AuthorDate: Wed Nov 18 14:03:56 2026 +0800

    Fix V4MojoConfigurator to assign expression values of the parameter type 
directly (#14011)
    
    * Fix V4MojoConfigurator to assign expression values of the parameter type 
directly
    
    When a leaf configuration node evaluates to an object that is already an
    instance of the mojo parameter type (for example ${project.mainArtifact}
    for a ProducedArtifact parameter), assign it directly instead of
    stringifying the value and re-instantiating the type, which fails with
    NoSuchMethodException for types without a no-arg constructor.
    
    Fixes #14007
    
    Signed-off-by: xh123637 <[email protected]>
    
    * Apply spotless formatting to V4MojoConfiguratorTest
    
    Signed-off-by: xh123637 <[email protected]>
    
    ---------
    
    Signed-off-by: xh123637 <[email protected]>
---
 .../maven/plugin/internal/V4MojoConfigurator.java  | 11 ++++
 .../plugin/internal/V4MojoConfiguratorTest.java    | 72 ++++++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git 
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java
 
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java
index 90e0c00fd0..b0f29a8075 100644
--- 
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java
+++ 
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/V4MojoConfigurator.java
@@ -200,6 +200,17 @@ private Object convert(XmlNode node, Type target) throws 
Exception {
             return values;
         }
         type = resolveImplementation(node, type);
+        if (node.children().isEmpty()) {
+            // A leaf node whose expression evaluates to an object of the 
parameter type
+            // (e.g. ${project.mainArtifact} for a ProducedArtifact parameter) 
is assigned
+            // directly instead of being stringified and re-instantiated, 
which fails for
+            // types without a no-arg constructor. Evaluate without coercion 
so plain
+            // text values still fall through to the conversion below.
+            Object evaluated = evaluateNode(node, null);
+            if (evaluated != null && type.isInstance(evaluated)) {
+                return evaluated;
+            }
+        }
         String value = evaluateNodeAsString(node);
         if (value != null && node.children().isEmpty()) {
             Method setter = findDefaultSetter(type);
diff --git 
a/impl/maven-core/src/test/java/org/apache/maven/plugin/internal/V4MojoConfiguratorTest.java
 
b/impl/maven-core/src/test/java/org/apache/maven/plugin/internal/V4MojoConfiguratorTest.java
index 9091fa03d5..5cfd42c1ad 100644
--- 
a/impl/maven-core/src/test/java/org/apache/maven/plugin/internal/V4MojoConfiguratorTest.java
+++ 
b/impl/maven-core/src/test/java/org/apache/maven/plugin/internal/V4MojoConfiguratorTest.java
@@ -39,6 +39,7 @@
 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
@@ -545,4 +546,75 @@ void testObjectTypedParameterWithChildrenConvertsToMap() 
throws Exception {
         assertEquals("val1", result.get("key1"));
         assertEquals("val2", result.get("key2"));
     }
+
+    // Object-valued expression for a parameter type without a no-arg 
constructor (issue #14007)
+    public interface ProducedArtifactLike {
+        String id();
+    }
+
+    public static final class ProducedArtifactImpl implements 
ProducedArtifactLike {
+        private final String id;
+
+        ProducedArtifactImpl(String id) {
+            this.id = id;
+        }
+
+        @Override
+        public String id() {
+            return id;
+        }
+    }
+
+    public static class ObjectExpressionMojo {
+        ProducedArtifactLike projectArtifact;
+    }
+
+    @Test
+    void testExpressionEvaluatingToParameterTypeInstanceIsAssignedDirectly() 
throws Exception {
+        ProducedArtifactLike artifact = new ProducedArtifactImpl("main");
+        MojoExpressionEvaluator objectEvaluator = 
mock(MojoExpressionEvaluator.class);
+        when(objectEvaluator.evaluate(anyString(), any())).thenAnswer(inv -> {
+            String val = inv.getArgument(0);
+            return "${project.mainArtifact}".equals(val) ? artifact : val;
+        });
+        V4MojoConfigurator objectConfigurator =
+                new V4MojoConfigurator(objectEvaluator, 
getClass().getClassLoader(), pluginDescriptor);
+
+        ObjectExpressionMojo mojo = new ObjectExpressionMojo();
+        XmlNode config = new XmlNodeImpl(
+                "configuration",
+                null,
+                Map.of(),
+                List.of(new XmlNodeImpl("projectArtifact", 
"${project.mainArtifact}")),
+                null);
+
+        objectConfigurator.configure(mojo, config);
+
+        assertSame(artifact, mojo.projectArtifact);
+    }
+
+    @Test
+    void 
testExpressionEvaluatingToParameterTypeInstanceViaDefaultValueIsAssignedDirectly()
 throws Exception {
+        ProducedArtifactLike artifact = new ProducedArtifactImpl("main");
+        MojoExpressionEvaluator objectEvaluator = 
mock(MojoExpressionEvaluator.class);
+        when(objectEvaluator.evaluate(anyString(), any())).thenAnswer(inv -> {
+            String val = inv.getArgument(0);
+            return "${project.mainArtifact}".equals(val) ? artifact : val;
+        });
+        V4MojoConfigurator objectConfigurator =
+                new V4MojoConfigurator(objectEvaluator, 
getClass().getClassLoader(), pluginDescriptor);
+
+        ObjectExpressionMojo mojo = new ObjectExpressionMojo();
+        XmlNode config = new XmlNodeImpl(
+                "configuration",
+                null,
+                Map.of(),
+                List.of(new XmlNodeImpl(
+                        "projectArtifact", null, Map.of("default-value", 
"${project.mainArtifact}"), List.of(), null)),
+                null);
+
+        objectConfigurator.configure(mojo, config);
+
+        assertSame(artifact, mojo.projectArtifact);
+    }
 }

Reply via email to