This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new db9097ec03 [Backport maven-4.0.x] Apply a parameter-level @InjectMojo
POM to the test project (#13916)
db9097ec03 is described below
commit db9097ec03d286a0f8004663dd324e81928f9dd9
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 9 15:13:24 2026 +0100
[Backport maven-4.0.x] Apply a parameter-level @InjectMojo POM to the test
project (#13916)
* 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
* spotless: fix formatting violations in MojoExtension.java
---------
Co-authored-by: Sylwester Lachiewicz <[email protected]>
---
.../apache/maven/testing/plugin/InjectMojo.java | 3 +
.../apache/maven/testing/plugin/MojoExtension.java | 47 ++++++++++----
.../testing/MojoExtensionParameterPomTest.java | 71 ++++++++++++++++++++++
3 files changed, 110 insertions(+), 11 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 2db08a1f3b..fb5a804088 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
@@ -170,6 +170,8 @@ public class MojoExtension extends MavenDIExtension
implements ParameterResolver
private static final String BASEDIR_KEY = "basedir";
+ private static final String POM_KEY = "pom";
+
private static final Interpolator INTERPOLATOR = new DefaultInterpolator();
/**
@@ -237,7 +239,8 @@ public Object resolveParameter(ParameterContext
parameterContext, ExtensionConte
String goal;
if (parameterInjectMojo != null) {
String pom = parameterInjectMojo[1];
- 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);
@@ -368,7 +371,17 @@ public void beforeEach(ExtensionContext context) throws
Exception {
Path basedirPath = Paths.get(getBasedir());
- String[] injectMojoAttrs =
findInjectMojoAttrs(context.getElement().get());
+ String[] methodInjectMojoAttrs =
+ findInjectMojoAttrs(context.getElement().get());
+ String[] parameterInjectMojoAttrs =
findParameterInjectMojoAttrs(context);
+ String[] injectMojoAttrs = methodInjectMojoAttrs != null ?
methodInjectMojoAttrs : parameterInjectMojoAttrs;
+ // 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 = injectMojoAttrs != null ? injectMojoAttrs[1] : "";
+ if (pom.isEmpty() && parameterInjectMojoAttrs != null) {
+ pom = parameterInjectMojoAttrs[1];
+ }
+ context.getStore(MOJO_EXTENSION).put(POM_KEY, pom);
Model defaultModel = Model.newBuilder()
.groupId("myGroupId")
.artifactId("myArtifactId")
@@ -413,12 +426,12 @@ public void beforeEach(ExtensionContext context) throws
Exception {
Path[] modelPath = new Path[] {null};
Model tmodel = null;
if (injectMojoAttrs != null) {
- String pom = injectMojoAttrs[1];
- if (pom != null && !pom.isEmpty()) {
+ if (!pom.isEmpty()) {
try (Reader r = openPomUrl(context.getRequiredTestClass(),
pom, modelPath)) {
tmodel = new MavenStaxReader().read(r);
}
- } else {
+ } else if (methodInjectMojoAttrs != 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)) {
@@ -428,15 +441,15 @@ public void beforeEach(ExtensionContext context) throws
Exception {
}
}
}
- Model model;
+ Model mergedModel;
if (tmodel == null) {
- model = defaultModel;
+ mergedModel = defaultModel;
} else {
- model = new MavenMerger().merge(tmodel, defaultModel, false, null);
+ mergedModel = new MavenMerger().merge(tmodel, defaultModel, false,
null);
}
- tmodel = new DefaultModelPathTranslator(new DefaultPathTranslator())
- .alignToBaseDirectory(tmodel, Paths.get(getBasedir()), null);
- context.getStore(MOJO_EXTENSION).put(Model.class, tmodel);
+ Model model = new DefaultModelPathTranslator(new
DefaultPathTranslator())
+ .alignToBaseDirectory(mergedModel, Paths.get(getBasedir()),
null);
+ context.getStore(MOJO_EXTENSION).put(Model.class, model);
// mojo execution
// Map<Object, Object> map =
getInjector().getContext().getContextData();
@@ -799,6 +812,18 @@ private static String findBasedirValue(AnnotatedElement
element) {
return d != null ? d.value() : null;
}
+ /**
+ * Returns the {@link InjectMojo} attributes declared on a parameter of
the current test method, if any.
+ */
+ private static String[] findParameterInjectMojoAttrs(ExtensionContext
context) {
+ return context.getTestMethod()
+ .flatMap(m -> Arrays.stream(m.getParameters())
+ .map(p -> findInjectMojoAttrs(p))
+ .filter(Objects::nonNull)
+ .findFirst())
+ .orElse(null);
+ }
+
@SuppressWarnings("deprecation")
private static String[] findInjectMojoAttrs(AnnotatedElement element) {
InjectMojo a = element.getAnnotation(InjectMojo.class);
diff --git
a/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionParameterPomTest.java
b/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionParameterPomTest.java
new file mode 100644
index 0000000000..e986c9e72c
--- /dev/null
+++
b/impl/maven-testing/src/test/java/org/apache/maven/api/plugin/testing/MojoExtensionParameterPomTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.api.plugin.testing;
+
+import org.apache.maven.api.Project;
+import org.apache.maven.api.di.Inject;
+import
org.apache.maven.api.plugin.testing.ExpressionEvaluatorTest.ExpressionEvaluatorMojo;
+import org.apache.maven.testing.plugin.InjectMojo;
+import org.apache.maven.testing.plugin.MojoExtension;
+import org.apache.maven.testing.plugin.MojoTest;
+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());
+ }
+}