This is an automated email from the ASF dual-hosted git repository.
slachiewicz 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 8a2d9cb6d4 Interpolate ${...} in the plugin POM coordinates used by
@InjectMojo (#13816)
8a2d9cb6d4 is described below
commit 8a2d9cb6d4025de1f492925e50aa1861e59dc5d3
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Mon Nov 9 18:10:46 2026 +0100
Interpolate ${...} in the plugin POM coordinates used by @InjectMojo
(#13816)
* Interpolate ${...} in the plugin POM coordinates used by @InjectMojo
A plugin built with a CI-friendly ${revision} version got the literal
expression as its version, so the mojo lookup by plugin key failed.
* Run the coordinates test isolated from tests that read basedir
* Interpolate the plugin POM coordinates with maven-utils StringInterpolator
* Name the plugin POM when its coordinates contain a recursive reference
* Export maven-utils to the maven-testing module
---------
Co-authored-by: Guillaume Nodet <[email protected]>
---
impl/maven-testing/pom.xml | 4 ++
.../apache/maven/testing/plugin/MojoExtension.java | 36 +++++++++++--
.../plugin/MojoExtensionCoordinatesTest.java | 60 ++++++++++++++++++++++
impl/maven-utils/src/main/java/module-info.java | 1 +
4 files changed, 98 insertions(+), 3 deletions(-)
diff --git a/impl/maven-testing/pom.xml b/impl/maven-testing/pom.xml
index 1814bb9f15..8a2ac01de0 100644
--- a/impl/maven-testing/pom.xml
+++ b/impl/maven-testing/pom.xml
@@ -76,6 +76,10 @@ under the License.
<groupId>org.apache.maven</groupId>
<artifactId>maven-support</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-utils</artifactId>
+ </dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-impl</artifactId>
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 d9aa3a7724..49416b5b3c 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
@@ -93,6 +93,8 @@
import org.apache.maven.testing.plugin.stubs.ProjectStub;
import org.apache.maven.testing.plugin.stubs.RepositorySystemSupplier;
import org.apache.maven.testing.plugin.stubs.SessionMock;
+import org.apache.maven.utils.StringInterpolator;
+import org.apache.maven.utils.StringInterpolatorException;
import
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
import
org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
@@ -171,6 +173,8 @@ public class MojoExtension extends MavenDIExtension
implements ParameterResolver
private static final String BASEDIR_KEY = "basedir";
+ private static final StringInterpolator INTERPOLATOR = new
StringInterpolator();
+
private static final String POM_KEY = "pom";
/**
@@ -694,10 +698,13 @@ protected String[] mojoCoordinates(String goal) throws
Exception {
return goal.split(":");
} else {
Path pluginPom = Paths.get(getPluginBasedir(), "pom.xml");
- Xpp3Dom pluginPomDom =
Xpp3DomBuilder.build(Files.newBufferedReader(pluginPom));
+ Xpp3Dom pluginPomDom;
+ try (Reader reader = Files.newBufferedReader(pluginPom)) {
+ pluginPomDom = Xpp3DomBuilder.build(reader);
+ }
String artifactId = pluginPomDom.getChild("artifactId").getValue();
- String groupId = resolveFromRootThenParent(pluginPomDom,
"groupId");
- String version = resolveFromRootThenParent(pluginPomDom,
"version");
+ String groupId = interpolate(pluginPom, pluginPomDom,
resolveFromRootThenParent(pluginPomDom, "groupId"));
+ String version = interpolate(pluginPom, pluginPomDom,
resolveFromRootThenParent(pluginPomDom, "version"));
return new String[] {groupId, artifactId, version, goal};
}
}
@@ -764,6 +771,29 @@ private static String resolveFromRootThenParent(Xpp3Dom
pluginPomDom, String ele
.orElseThrow(() -> new Exception("unable to determine " +
element));
}
+ /**
+ * Resolves {@code ${...}} references in a plugin POM value, such as a
CI-friendly
+ * {@code ${revision}} version, from the POM's {@code <properties>} and
then from the
+ * system properties.
+ */
+ private static String interpolate(Path pluginPom, Xpp3Dom pluginPomDom,
String value) throws Exception {
+ String interpolated;
+ try {
+ interpolated = INTERPOLATOR.interpolate(
+ value,
+ key -> child(pluginPomDom, "properties")
+ .flatMap(p -> child(p, key))
+ .map(Xpp3Dom::getValue)
+ .orElseGet(() -> System.getProperty(key)));
+ } catch (StringInterpolatorException e) {
+ throw new Exception("unable to resolve " + value + " in " +
pluginPom + ": " + e.getMessage(), e);
+ }
+ if (interpolated.contains("${")) {
+ throw new Exception("unable to resolve " + value + " in " +
pluginPom);
+ }
+ return interpolated;
+ }
+
/**
* Convenience method to obtain the value of a variable on a mojo that
might not have a getter.
* <br>
diff --git
a/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionCoordinatesTest.java
b/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionCoordinatesTest.java
new file mode 100644
index 0000000000..4a6466ef9d
--- /dev/null
+++
b/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionCoordinatesTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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 java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.api.parallel.Isolated;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+// swaps the global basedir system property that every @MojoTest reads
+@Isolated
+class MojoExtensionCoordinatesTest {
+
+ @TempDir
+ Path pluginBasedir;
+
+ @Test
+ void coordinatesInterpolateCiFriendlyVersion() throws Exception {
+ Files.writeString(
+ pluginBasedir.resolve("pom.xml"),
+ "<project>"
+ +
"<parent><groupId>org.example</groupId><artifactId>parent</artifactId>"
+ + "<version>${revision}</version></parent>"
+ + "<artifactId>my-plugin</artifactId>"
+ + "<properties><revision>1.2.3</revision></properties>"
+ + "</project>");
+ String previous = System.getProperty("basedir");
+ System.setProperty("basedir", pluginBasedir.toString());
+ try {
+ String[] coord = new MojoExtension().mojoCoordinates("goal");
+ assertArrayEquals(new String[] {"org.example", "my-plugin",
"1.2.3", "goal"}, coord);
+ } finally {
+ if (previous != null) {
+ System.setProperty("basedir", previous);
+ } else {
+ System.clearProperty("basedir");
+ }
+ }
+ }
+}
diff --git a/impl/maven-utils/src/main/java/module-info.java
b/impl/maven-utils/src/main/java/module-info.java
index 888392b4fe..6d587bb9b7 100644
--- a/impl/maven-utils/src/main/java/module-info.java
+++ b/impl/maven-utils/src/main/java/module-info.java
@@ -22,6 +22,7 @@
org.apache.maven.impl,
org.apache.maven.cling,
org.apache.maven.core,
+ org.apache.maven.testing,
maven.compat,
maven.embedder,
maven.model.builder;