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 62e130e899 Remove remaining Plexus dependencies from maven-testing
(#13928)
62e130e899 is described below
commit 62e130e8998dd947cbef5a14a9fe5c731cc9bad2
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Nov 10 18:26:53 2026 +0100
Remove remaining Plexus dependencies from maven-testing (#13928)
* Remove remaining Plexus dependencies from maven-testing
Make MojoExpressionEvaluator and MojoExpressionEvaluationException public
so that maven-testing (and other external consumers) can implement the
evaluator SPI directly without depending on plexus-component-configurator.
Changes:
- MojoExpressionEvaluator: make public, @since 4.2.0
- MojoExpressionEvaluationException: make public, @since 4.2.0
- MojoExpressionEvaluator.evaluate(): declare throws Exception (compatible
with ExpressionEvaluationException, avoids exception-hierarchy coupling)
- PlexusMojoExpressionEvaluatorAdapter: simplify now that evaluate() throws
Exception
- PluginParameterExpressionEvaluatorV4: also implements
MojoExpressionEvaluator
(ExpressionEvaluationException extends Exception, satisfies both
interfaces)
- V4MojoConfigurator: deprecate the Plexus TypeAwareExpressionEvaluator
bridge constructor (kept for compat, prefer MojoExpressionEvaluator)
- MojoExtension (maven-testing):
- Replace WrapEvaluator to implement MojoExpressionEvaluator instead of
TypeAwareExpressionEvaluator; pass it directly to V4MojoConfigurator
- Replace XmlStreamReader with InputStreamReader + StandardCharsets.UTF_8
- Replace Xpp3DomBuilder in mojoCoordinates() with MavenStaxReader
- Replace private Xpp3Dom helper methods with Model-based equivalents
- Replace ReflectionUtils.getFieldByNameIncludingSuperclasses() with
an equivalent private JDK-only helper
- Remove org.eclipse.sisu.plexus dependency from pom.xml
* Remove unused sisu.inject and guice direct deps from maven-testing
Both are already available at runtime transitively through maven-core,
and maven-testing has no direct Java imports from either artifact.
* Address review: public constructors on MojoExpressionEvaluationException,
document throws Exception rationale, include artifactId in error messages
* fix: apply spotless formatting to MojoExtension.setVariableValueToObject
* Address review: interpolate groupId in mojoCoordinates, add @since to
MojoExpressionEvaluator
---
.../PluginParameterExpressionEvaluatorV4.java | 3 +-
.../MojoExpressionEvaluationException.java | 12 +-
.../plugin/internal/MojoExpressionEvaluator.java | 13 +-
.../PlexusMojoExpressionEvaluatorAdapter.java | 8 +-
.../maven/plugin/internal/V4MojoConfigurator.java | 5 +-
impl/maven-testing/pom.xml | 13 --
.../apache/maven/testing/plugin/MojoExtension.java | 137 ++++++++++-----------
7 files changed, 90 insertions(+), 101 deletions(-)
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4.java
b/impl/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4.java
index 3264fce446..dbc2454368 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/plugin/PluginParameterExpressionEvaluatorV4.java
@@ -28,6 +28,7 @@
import org.apache.maven.api.Project;
import org.apache.maven.api.Session;
import org.apache.maven.impl.model.reflection.ReflectionValueExtractor;
+import org.apache.maven.plugin.internal.MojoExpressionEvaluator;
import
org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
import
org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
@@ -51,7 +52,7 @@
* @see org.apache.maven.api.settings.Settings
* @see MojoExecution
*/
-public class PluginParameterExpressionEvaluatorV4 implements
TypeAwareExpressionEvaluator {
+public class PluginParameterExpressionEvaluatorV4 implements
TypeAwareExpressionEvaluator, MojoExpressionEvaluator {
private final Session session;
private final MojoExecution mojoExecution;
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluationException.java
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluationException.java
index 8e3e78e040..7a63ec3f72 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluationException.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluationException.java
@@ -18,13 +18,17 @@
*/
package org.apache.maven.plugin.internal;
-/** Thrown when a mojo parameter expression cannot be evaluated. */
-class MojoExpressionEvaluationException extends Exception {
- MojoExpressionEvaluationException(String message) {
+/**
+ * Thrown when a mojo parameter expression cannot be evaluated.
+ *
+ * @since 4.2.0
+ */
+public class MojoExpressionEvaluationException extends Exception {
+ public MojoExpressionEvaluationException(String message) {
super(message);
}
- MojoExpressionEvaluationException(String message, Throwable cause) {
+ public MojoExpressionEvaluationException(String message, Throwable cause) {
super(message, cause);
}
}
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluator.java
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluator.java
index 7cc6078198..326c269e57 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluator.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/MojoExpressionEvaluator.java
@@ -23,18 +23,25 @@
/**
* Evaluates mojo parameter expressions and aligns paths to the project base
directory.
* Used by {@link V4MojoConfigurator} to resolve plugin parameter values.
+ *
+ * @since 4.2.0
*/
-interface MojoExpressionEvaluator {
+public interface MojoExpressionEvaluator {
/**
* Evaluates the given expression, optionally coercing the result to the
given type.
*
+ * <p>This method declares {@code throws Exception} rather than
+ * {@code throws MojoExpressionEvaluationException} intentionally: tying
the public SPI
+ * signature to a specific exception hierarchy would force implementors to
depend on
+ * {@code maven-core} internals. Callers should catch {@code Exception}
and wrap as needed.
+ *
* @param expr the expression to evaluate (e.g. {@code ${project.version}})
* @param type the desired return type, or {@code null} for no coercion
* @return the evaluated value, or {@code null}
- * @throws MojoExpressionEvaluationException if evaluation fails
+ * @throws Exception if evaluation fails
*/
- Object evaluate(String expr, Class<?> type) throws
MojoExpressionEvaluationException;
+ Object evaluate(String expr, Class<?> type) throws Exception;
/**
* Aligns a relative {@link File} to the project base directory.
diff --git
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/PlexusMojoExpressionEvaluatorAdapter.java
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/PlexusMojoExpressionEvaluatorAdapter.java
index c815ab4ce4..8ae95bc55a 100644
---
a/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/PlexusMojoExpressionEvaluatorAdapter.java
+++
b/impl/maven-core/src/main/java/org/apache/maven/plugin/internal/PlexusMojoExpressionEvaluatorAdapter.java
@@ -34,12 +34,8 @@ class PlexusMojoExpressionEvaluatorAdapter implements
MojoExpressionEvaluator {
}
@Override
- public Object evaluate(String expr, Class<?> type) throws
MojoExpressionEvaluationException {
- try {
- return delegate.evaluate(expr, type);
- } catch
(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException
e) {
- throw new MojoExpressionEvaluationException(e.getMessage(), e);
- }
+ public Object evaluate(String expr, Class<?> type) throws Exception {
+ return delegate.evaluate(expr, type);
}
@Override
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 9bf06b6788..90e0c00fd0 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
@@ -57,9 +57,10 @@ public V4MojoConfigurator(
/**
* Creates a configurator backed by a Plexus {@code
TypeAwareExpressionEvaluator}.
- * For use by callers outside the {@code internal} package that cannot
access
- * {@link PlexusMojoExpressionEvaluatorAdapter} directly (e.g. {@code
maven-testing}).
+ *
+ * @deprecated Use {@link #V4MojoConfigurator(MojoExpressionEvaluator,
ClassLoader, PluginDescriptor)} instead.
*/
+ @Deprecated
public V4MojoConfigurator(
org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator
evaluator,
ClassLoader classLoader,
diff --git a/impl/maven-testing/pom.xml b/impl/maven-testing/pom.xml
index 8a2ac01de0..075d25b990 100644
--- a/impl/maven-testing/pom.xml
+++ b/impl/maven-testing/pom.xml
@@ -128,23 +128,10 @@ under the License.
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-file</artifactId>
</dependency>
- <dependency>
- <groupId>org.eclipse.sisu</groupId>
- <artifactId>org.eclipse.sisu.inject</artifactId>
- </dependency>
- <dependency>
- <groupId>org.eclipse.sisu</groupId>
- <artifactId>org.eclipse.sisu.plexus</artifactId>
- </dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
- <dependency>
- <groupId>com.google.inject</groupId>
- <artifactId>guice</artifactId>
- <classifier>classes</classifier>
- </dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
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 49416b5b3c..8dde1e7802 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
@@ -24,12 +24,14 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.net.URL;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -43,7 +45,6 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
-import java.util.stream.Stream;
import org.apache.maven.api.MojoExecution;
import org.apache.maven.api.Project;
@@ -85,6 +86,7 @@
import org.apache.maven.model.v4.MavenStaxReader;
import org.apache.maven.plugin.PluginParameterExpressionEvaluatorV4;
import org.apache.maven.plugin.descriptor.io.PluginDescriptorStaxReader;
+import org.apache.maven.plugin.internal.MojoExpressionEvaluator;
import org.apache.maven.plugin.internal.V4MojoConfigurator;
import org.apache.maven.testing.di.MavenDIExtension;
import org.apache.maven.testing.plugin.stubs.MojoExecutionStub;
@@ -95,13 +97,6 @@
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;
-import org.codehaus.plexus.util.ReflectionUtils;
-import org.codehaus.plexus.util.xml.XmlStreamReader;
-import org.codehaus.plexus.util.xml.Xpp3Dom;
-import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.eclipse.aether.RepositorySystem;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -314,13 +309,10 @@ public Object resolveParameter(ParameterContext
parameterContext, ExtensionConte
Session session = getInjector().getInstance(Session.class);
Project project = getInjector().getInstance(Project.class);
MojoExecution mojoExecution =
getInjector().getInstance(MojoExecution.class);
- ExpressionEvaluator evaluator = new WrapEvaluator(
+ MojoExpressionEvaluator evaluator = new WrapEvaluator(
getInjector(), new
PluginParameterExpressionEvaluatorV4(session, project, mojoExecution));
- new V4MojoConfigurator(
- (TypeAwareExpressionEvaluator) evaluator,
- mojo.getClass().getClassLoader(),
- null)
+ new V4MojoConfigurator(evaluator,
mojo.getClass().getClassLoader(), null)
.configure(mojo, pluginConfiguration);
return mojo;
} catch (Exception e) {
@@ -479,7 +471,7 @@ public void beforeEach(ExtensionContext context) throws
Exception {
try (InputStream is = requireNonNull(
classLoader.getResourceAsStream(getPluginDescriptorLocation()),
"Unable to find plugin descriptor: " +
getPluginDescriptorLocation());
- Reader reader = new BufferedReader(new XmlStreamReader(is))) {
+ Reader reader = new BufferedReader(new InputStreamReader(is,
StandardCharsets.UTF_8))) {
// new InterpolationFilterReader(reader, map, "${", "}");
pluginDescriptor = new PluginDescriptorStaxReader().read(reader);
}
@@ -678,7 +670,7 @@ private Reader openPomUrl(Class<?> holder, String pom,
Path[] modelPath) throws
if (url == null) {
throw new IllegalStateException("Unable to find pom on
classpath: " + pom);
}
- return new XmlStreamReader(url.openStream());
+ return new InputStreamReader(url.openStream(),
StandardCharsets.UTF_8);
} else if (pom.stripLeading().startsWith("<")) {
// inline XML, with or without a prolog or a namespace on the root
element
return new StringReader(pom);
@@ -698,17 +690,39 @@ protected String[] mojoCoordinates(String goal) throws
Exception {
return goal.split(":");
} else {
Path pluginPom = Paths.get(getPluginBasedir(), "pom.xml");
- Xpp3Dom pluginPomDom;
+ org.apache.maven.api.model.Model pomModel;
try (Reader reader = Files.newBufferedReader(pluginPom)) {
- pluginPomDom = Xpp3DomBuilder.build(reader);
+ pomModel = new MavenStaxReader().read(reader);
}
- String artifactId = pluginPomDom.getChild("artifactId").getValue();
- String groupId = interpolate(pluginPom, pluginPomDom,
resolveFromRootThenParent(pluginPomDom, "groupId"));
- String version = interpolate(pluginPom, pluginPomDom,
resolveFromRootThenParent(pluginPomDom, "version"));
+ String artifactId = pomModel.getArtifactId();
+ String rawGroupId = resolveGroupId(pomModel);
+ String groupId = interpolateModel(pluginPom, pomModel, rawGroupId);
+ String rawVersion = resolveVersion(pomModel);
+ String version = interpolateModel(pluginPom, pomModel, rawVersion);
return new String[] {groupId, artifactId, version, goal};
}
}
+ private static String resolveGroupId(org.apache.maven.api.model.Model
model) throws Exception {
+ if (model.getGroupId() != null && !model.getGroupId().isEmpty()) {
+ return model.getGroupId();
+ }
+ if (model.getParent() != null && model.getParent().getGroupId() !=
null) {
+ return model.getParent().getGroupId();
+ }
+ throw new Exception("unable to determine groupId for artifact: " +
model.getArtifactId());
+ }
+
+ private static String resolveVersion(org.apache.maven.api.model.Model
model) throws Exception {
+ if (model.getVersion() != null && !model.getVersion().isEmpty()) {
+ return model.getVersion();
+ }
+ if (model.getParent() != null && model.getParent().getVersion() !=
null) {
+ return model.getParent().getVersion();
+ }
+ throw new Exception("unable to determine version for artifact: " +
model.getArtifactId());
+ }
+
private XmlNode finalizeConfig(XmlNode config, MojoDescriptor
mojoDescriptor) {
List<XmlNode> children = new ArrayList<>();
if (mojoDescriptor != null) {
@@ -737,54 +751,18 @@ private XmlNode finalizeConfig(XmlNode config,
MojoDescriptor mojoDescriptor) {
return XmlNode.newInstance("configuration", null, null, children,
null);
}
- private static Optional<Xpp3Dom> child(Xpp3Dom element, String name) {
- return Optional.ofNullable(element.getChild(name));
- }
-
- private static Stream<Xpp3Dom> children(Xpp3Dom element) {
- return Stream.of(element.getChildren());
- }
-
- public static XmlNode extractPluginConfiguration(String artifactId,
Xpp3Dom pomDom) throws Exception {
- Xpp3Dom pluginConfigurationElement = child(pomDom, "build")
- .flatMap(buildElement -> child(buildElement, "plugins"))
- .map(MojoExtension::children)
- .orElseGet(Stream::empty)
- .filter(e ->
e.getChild("artifactId").getValue().equals(artifactId))
- .findFirst()
- .flatMap(buildElement -> child(buildElement, "configuration"))
- .orElse(Xpp3DomBuilder.build(new
StringReader("<configuration/>")));
- return pluginConfigurationElement.getDom();
- }
-
- /**
- * sometimes the parent element might contain the correct value so
generalize that access
- *
- * TODO find out where this is probably done elsewhere
- */
- private static String resolveFromRootThenParent(Xpp3Dom pluginPomDom,
String element) throws Exception {
- return Optional.ofNullable(child(pluginPomDom, element)
- .orElseGet(() -> child(pluginPomDom, "parent")
- .flatMap(e -> child(e, element))
- .orElse(null)))
- .map(Xpp3Dom::getValue)
- .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 {
+ private static String interpolateModel(Path pluginPom,
org.apache.maven.api.model.Model model, String value)
+ throws Exception {
+ Map<String, String> properties = model.getProperties();
String interpolated;
try {
interpolated = INTERPOLATOR.interpolate(
- value,
- key -> child(pluginPomDom, "properties")
- .flatMap(p -> child(p, key))
- .map(Xpp3Dom::getValue)
- .orElseGet(() -> System.getProperty(key)));
+ value, key ->
Optional.ofNullable(properties.get(key)).orElseGet(() ->
System.getProperty(key)));
} catch (StringInterpolatorException e) {
throw new Exception("unable to resolve " + value + " in " +
pluginPom + ": " + e.getMessage(), e);
}
@@ -800,7 +778,10 @@ private static String interpolate(Path pluginPom, Xpp3Dom
pluginPomDom, String v
* NOTE: the caller is responsible for casting to what the desired type is.
*/
public static Object getVariableValueFromObject(Object object, String
variable) throws IllegalAccessException {
- Field field =
ReflectionUtils.getFieldByNameIncludingSuperclasses(variable,
object.getClass());
+ Field field = getFieldByNameIncludingSuperclasses(variable,
object.getClass());
+ requireNonNull(
+ field,
+ "Field " + variable + " not found in " +
object.getClass().getName());
field.setAccessible(true);
return field.get(object);
}
@@ -841,29 +822,41 @@ public static Map<String, Object>
getVariablesAndValuesFromObject(Class<?> clazz
*/
public static void setVariableValueToObject(Object object, String
variable, Object value)
throws IllegalAccessException {
- Field field =
ReflectionUtils.getFieldByNameIncludingSuperclasses(variable,
object.getClass());
- requireNonNull(field, "Field " + variable + " not found");
+ Field field = getFieldByNameIncludingSuperclasses(variable,
object.getClass());
+ requireNonNull(
+ field,
+ "Field " + variable + " not found in " +
object.getClass().getName());
field.setAccessible(true);
field.set(object, value);
}
- static class WrapEvaluator implements TypeAwareExpressionEvaluator {
+ /**
+ * Traverses the class hierarchy from {@code clazz} upward and returns the
first
+ * declared field matching {@code name}, or {@code null} if none is found.
+ */
+ private static Field getFieldByNameIncludingSuperclasses(String name,
Class<?> clazz) {
+ for (Class<?> type = clazz; type != null && type != Object.class; type
= type.getSuperclass()) {
+ try {
+ return type.getDeclaredField(name);
+ } catch (NoSuchFieldException ignored) {
+ // continue
+ }
+ }
+ return null;
+ }
+
+ static class WrapEvaluator implements MojoExpressionEvaluator {
private final Injector injector;
- private final TypeAwareExpressionEvaluator evaluator;
+ private final MojoExpressionEvaluator evaluator;
- WrapEvaluator(Injector injector, TypeAwareExpressionEvaluator
evaluator) {
+ WrapEvaluator(Injector injector, MojoExpressionEvaluator evaluator) {
this.injector = injector;
this.evaluator = evaluator;
}
@Override
- public Object evaluate(String expression) throws
ExpressionEvaluationException {
- return evaluate(expression, null);
- }
-
- @Override
- public Object evaluate(String expression, Class<?> type) throws
ExpressionEvaluationException {
+ public Object evaluate(String expression, Class<?> type) throws
Exception {
Object value = evaluator.evaluate(expression, type);
if (value == null) {
String expr = stripTokens(expression);