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 a732960d56 Interpolate ${...} in the plugin POM coordinates used by
@InjectMojo (#13913)
a732960d56 is described below
commit a732960d560bb6a05f44ac499a0dbfe73ef1e309
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 9 13:16:58 2026 +0100
Interpolate ${...} in the plugin POM coordinates used by @InjectMojo
(#13913)
* Interpolate ${...} in the plugin POM coordinates used by @InjectMojo
Backport of #13816 to `maven-4.0.x`.
`mojoCoordinates` resolves `${...}` in the plugin `groupId` and `version`
using `org.apache.maven.api.services.Interpolator` (via
`DefaultInterpolator`),
from the plugin POM's `<properties>`, then from system properties, and fails
naming the expression and the POM when neither has it.
This is the 4.0.x equivalent of #13816 which uses
`maven-utils/StringInterpolator`
(not available on this branch); here we use the `Interpolator` SPI from
`maven-api-core` already available via the `maven-core` transitive
dependency.
* Add error-path test for unresolvable coordinate expressions
---
.../apache/maven/testing/plugin/MojoExtension.java | 35 +++++++++-
.../plugin/MojoExtensionCoordinatesTest.java | 81 ++++++++++++++++++++++
2 files changed, 113 insertions(+), 3 deletions(-)
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 7db9cf816e..2db08a1f3b 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
@@ -60,6 +60,8 @@
import org.apache.maven.api.services.ArtifactFactory;
import org.apache.maven.api.services.ArtifactInstaller;
import org.apache.maven.api.services.ArtifactManager;
+import org.apache.maven.api.services.Interpolator;
+import org.apache.maven.api.services.InterpolatorException;
import org.apache.maven.api.services.LocalRepositoryManager;
import org.apache.maven.api.services.ProjectBuilder;
import org.apache.maven.api.services.ProjectManager;
@@ -73,6 +75,7 @@
import org.apache.maven.di.Key;
import org.apache.maven.di.impl.DIException;
import org.apache.maven.impl.InternalSession;
+import org.apache.maven.impl.model.DefaultInterpolator;
import org.apache.maven.impl.model.DefaultModelPathTranslator;
import org.apache.maven.impl.model.DefaultPathTranslator;
import org.apache.maven.internal.impl.DefaultLog;
@@ -167,6 +170,8 @@ public class MojoExtension extends MavenDIExtension
implements ParameterResolver
private static final String BASEDIR_KEY = "basedir";
+ private static final Interpolator INTERPOLATOR = new DefaultInterpolator();
+
/**
* Gets the identifier for the current test method.
* The format is "TestClassName-testMethodName".
@@ -636,10 +641,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};
}
}
@@ -705,6 +713,27 @@ 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 (InterpolatorException e) {
+ throw new Exception("unable to resolve " + value + " in " +
pluginPom + ": " + e.getMessage(), e);
+ }
+ if (interpolated != null && 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..2d998b58ba
--- /dev/null
+++
b/impl/maven-testing/src/test/java/org/apache/maven/testing/plugin/MojoExtensionCoordinatesTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+// 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");
+ }
+ }
+ }
+
+ @Test
+ void coordinatesThrowsOnUnresolvableExpression() throws Exception {
+ Files.writeString(
+ pluginBasedir.resolve("pom.xml"),
+
"<project><parent><groupId>org.example</groupId><artifactId>parent</artifactId>"
+ + "<version>${unresolved}</version></parent>"
+ + "<artifactId>my-plugin</artifactId></project>");
+ String previous = System.getProperty("basedir");
+ System.setProperty("basedir", pluginBasedir.toString());
+ try {
+ assertThrows(Exception.class, () -> new
MojoExtension().mojoCoordinates("goal"));
+ } finally {
+ if (previous != null) {
+ System.setProperty("basedir", previous);
+ } else {
+ System.clearProperty("basedir");
+ }
+ }
+ }
+}