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 6ad313714c Apply a parameter-level @InjectMojo POM to the test project 
(#13814)
6ad313714c is described below

commit 6ad313714c7d22bf819f096146ff71b7fb600065
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 9 12:03:52 2026 +0100

    Apply a parameter-level @InjectMojo POM to the test project (#13814)
    
    * Apply a parameter-level @InjectMojo POM to the test project
    
    The Project singleton is created in beforeEach from the method-level POM,
    so a POM given on the mojo parameter configured the mojo but not the 
project.
    
    * Keep the default project for a parameter-only @InjectMojo without a POM
    
    * Assert the artifactId in the parameter-only default project test
    
    * Write the inline test POMs as text blocks
    
    * Document that a parameter-level @InjectMojo POM also builds the test 
project
---
 .../apache/maven/testing/plugin/InjectMojo.java    |  3 +
 .../apache/maven/testing/plugin/MojoExtension.java | 35 +++++++++--
 .../plugin/MojoExtensionParameterPomTest.java      | 68 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 5 deletions(-)

diff --git 
a/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/InjectMojo.java
 
b/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/InjectMojo.java
index b2be929162..0293610e62 100644
--- 
a/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/InjectMojo.java
+++ 
b/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/InjectMojo.java
@@ -100,6 +100,9 @@
      *
      * <p>If not specified, the default project configuration will be used.</p>
      *
+     * <p>A POM given on a parameter's annotation also builds the test 
project, in place of the
+     * {@code pom.xml} of the base directory that a method-level annotation 
without a POM reads.</p>
+     *
      * @return the path to a custom POM file, or an empty string to use 
defaults
      */
     String pom() default "";
diff --git 
a/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/MojoExtension.java
 
b/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/MojoExtension.java
index bef012485b..d9aa3a7724 100644
--- 
a/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/MojoExtension.java
+++ 
b/impl/maven-testing/src/main/java/org/apache/maven/testing/plugin/MojoExtension.java
@@ -171,6 +171,8 @@ public class MojoExtension extends MavenDIExtension 
implements ParameterResolver
 
     private static final String BASEDIR_KEY = "basedir";
 
+    private static final String POM_KEY = "pom";
+
     /**
      * Gets the identifier for the current test method.
      * The format is "TestClassName-testMethodName".
@@ -237,7 +239,8 @@ public Object resolveParameter(ParameterContext 
parameterContext, ExtensionConte
             String goal;
             if (parameterInjectMojo != null) {
                 String pom = parameterInjectMojo.pom();
-                if (pom != null && !pom.isEmpty()) {
+                String appliedPom = 
extensionContext.getStore(MOJO_EXTENSION).get(POM_KEY, String.class);
+                if (pom != null && !pom.isEmpty() && !Objects.equals(pom, 
appliedPom)) {
                     try (Reader r = openPomUrl(holder, pom, new Path[1])) {
                         Model localModel = new MavenStaxReader().read(r);
                         model = new MavenMerger().merge(localModel, model, 
false, null);
@@ -384,8 +387,18 @@ public void beforeEach(ExtensionContext context) throws 
Exception {
 
         Path basedirPath = Paths.get(getBasedir());
 
-        InjectMojo mojo = 
AnnotationSupport.findAnnotation(context.getElement().get(), InjectMojo.class)
+        InjectMojo methodMojo = AnnotationSupport.findAnnotation(
+                        context.getElement().get(), InjectMojo.class)
                 .orElse(null);
+        InjectMojo parameterMojo = findParameterInjectMojo(context);
+        InjectMojo mojo = methodMojo != null ? methodMojo : parameterMojo;
+        // A POM given on a test method parameter configures the project too, 
so it is read
+        // here, where the Project singleton is created, and not only in 
resolveParameter().
+        String pom = mojo != null ? mojo.pom() : "";
+        if (pom.isEmpty() && parameterMojo != null) {
+            pom = parameterMojo.pom();
+        }
+        context.getStore(MOJO_EXTENSION).put(POM_KEY, pom);
         Model defaultModel = Model.newBuilder()
                 .groupId("myGroupId")
                 .artifactId("myArtifactId")
@@ -430,12 +443,12 @@ public void beforeEach(ExtensionContext context) throws 
Exception {
         Path[] modelPath = new Path[] {null};
         Model tmodel = null;
         if (mojo != null) {
-            String pom = mojo.pom();
-            if (pom != null && !pom.isEmpty()) {
+            if (!pom.isEmpty()) {
                 try (Reader r = openPomUrl(context.getRequiredTestClass(), 
pom, modelPath)) {
                     tmodel = new MavenStaxReader().read(r);
                 }
-            } else {
+            } else if (methodMojo != null) {
+                // only a method-level annotation falls back to the basedir 
POM; a parameter-only one keeps the default
                 Path pomPath = basedirPath.resolve("pom.xml");
                 if (Files.exists(pomPath)) {
                     try (Reader r = Files.newBufferedReader(pomPath)) {
@@ -639,6 +652,18 @@ static ModelXmlFactory newModelXmlFactory(Session session) 
{
         //        mojoExecutionScope.seed(MojoExecution.class, me);
     }
 
+    /**
+     * Returns the {@link InjectMojo} declared on a parameter of the current 
test method, if any.
+     */
+    private static InjectMojo findParameterInjectMojo(ExtensionContext 
context) {
+        return context.getTestMethod()
+                .flatMap(m -> Arrays.stream(m.getParameters())
+                        .map(p -> p.getAnnotation(InjectMojo.class))
+                        .filter(Objects::nonNull)
+                        .findFirst())
+                .orElse(null);
+    }
+
     private Reader openPomUrl(Class<?> holder, String pom, Path[] modelPath) 
throws IOException {
         if (pom.startsWith("file:")) {
             Path path = 
Paths.get(getBasedir()).resolve(pom.substring("file:".length()));
diff --git 
a/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionParameterPomTest.java
 
b/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionParameterPomTest.java
new file mode 100644
index 0000000000..658b53c3f4
--- /dev/null
+++ 
b/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionParameterPomTest.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.testing.plugin;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.di.Inject;
+import 
org.apache.maven.testing.plugin.ExpressionEvaluatorTest.ExpressionEvaluatorMojo;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+@MojoTest
+class MojoExtensionParameterPomTest {
+
+    private static final String COORDINATES = 
"test:test-plugin:0.0.1-SNAPSHOT:goal";
+
+    private static final String CUSTOM_POM = """
+            <project>
+              <groupId>customGroupId</groupId>
+              <artifactId>customArtifactId</artifactId>
+              <version>2.0</version>
+              <build>
+                <plugins>
+                  <plugin>
+                    <groupId>test</groupId>
+                    <artifactId>test-plugin</artifactId>
+                    <configuration>
+                      <param>fromParameterPom</param>
+                    </configuration>
+                  </plugin>
+                </plugins>
+              </build>
+            </project>
+            """;
+
+    @Inject
+    Project project;
+
+    @Test
+    @InjectMojo(goal = COORDINATES)
+    void parameterPomConfiguresBothMojoAndProject(
+            @InjectMojo(goal = COORDINATES, pom = CUSTOM_POM) 
ExpressionEvaluatorMojo mojo) throws Exception {
+        assertEquals("fromParameterPom", 
MojoExtension.getVariableValueFromObject(mojo, "param"));
+        assertEquals("customGroupId", project.getModel().getGroupId());
+    }
+
+    @Test
+    void parameterWithoutPomKeepsTheDefaultProject(@InjectMojo(goal = 
COORDINATES) ExpressionEvaluatorMojo mojo) {
+        // not the plugin's own basedir POM, which only a method-level 
@InjectMojo reads
+        assertEquals("myArtifactId", project.getModel().getArtifactId());
+    }
+}

Reply via email to