This is an automated email from the ASF dual-hosted git repository.
sjaranowski pushed a commit to branch maven-plugin-testing-3.x
in repository https://gitbox.apache.org/repos/asf/maven-plugin-testing.git
The following commit(s) were added to refs/heads/maven-plugin-testing-3.x by
this push:
new 9f2100e Use default pom.xml when Basedir is set
9f2100e is described below
commit 9f2100ead7b4b906a4c76a73e584dbfe10e7aeb6
Author: Slawomir Jaranowski <[email protected]>
AuthorDate: Mon Oct 27 23:24:10 2025 +0100
Use default pom.xml when Basedir is set
When Basedir is explicit set we can try to find a pom.xml
we can not use a default pom.xml with default basedir which point to
project basedir
---
.../maven/api/plugin/testing/MojoExtension.java | 23 +++++++++++++++++++++-
.../maven/plugin/testing/ParametersMojoTest.java | 16 +++++++++++++++
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git
a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
index 8ffd04e..66699f8 100644
---
a/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
+++
b/maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java
@@ -28,6 +28,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@@ -139,6 +140,8 @@ public class MojoExtension extends PlexusExtension
implements ParameterResolver
// Namespace for storing/retrieving data related to MojoExtension
private static final ExtensionContext.Namespace MOJO_EXTENSION =
ExtensionContext.Namespace.create("MojoExtension");
+ public static final String BASEDIR_IS_SET_KEY = "basedirIsSet";
+
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext)
throws ParameterResolutionException {
@@ -178,7 +181,13 @@ public class MojoExtension extends PlexusExtension
implements ParameterResolver
public void beforeEach(ExtensionContext context) throws Exception {
String basedir =
AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
.map(Basedir::value)
- .orElseGet(PlexusExtension::getBasedir);
+ .orElse(null);
+
+ if (basedir == null) {
+ basedir = getBasedir();
+ } else {
+ context.getStore(MOJO_EXTENSION).put(BASEDIR_IS_SET_KEY,
Boolean.TRUE);
+ }
URL resource = context.getRequiredTestClass().getResource(basedir);
if (resource != null) {
@@ -318,6 +327,14 @@ public class MojoExtension extends PlexusExtension
implements ParameterResolver
} else if (!pom.isEmpty()) {
Path path = basedir.resolve(pom);
pomDom = Xpp3DomBuilder.build(new XmlStreamReader(path.toFile()));
+ } else if (isBasedirSet(extensionContext)) {
+ // only look for a pom.xml if basedir is explicitly set
+ Path path = basedir.resolve("pom.xml");
+ if (Files.exists(path)) {
+ pomDom = Xpp3DomBuilder.build(new
XmlStreamReader(path.toFile()));
+ } else {
+ pomDom = new Xpp3Dom("");
+ }
} else {
pomDom = new Xpp3Dom("");
}
@@ -337,6 +354,10 @@ public class MojoExtension extends PlexusExtension
implements ParameterResolver
return lookupMojo(extensionContext, coord, pluginConfiguration,
descriptor);
}
+ private boolean isBasedirSet(ExtensionContext extensionContext) {
+ return
extensionContext.getStore(MOJO_EXTENSION).getOrDefault(BASEDIR_IS_SET_KEY,
Boolean.class, Boolean.FALSE);
+ }
+
protected String[] mojoCoordinates(String goal, PluginDescriptor
pluginDescriptor) throws Exception {
if (goal.matches(".*:.*:.*:.*")) {
return goal.split(":");
diff --git
a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
index 0129bcc..fdc8f97 100644
---
a/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
+++
b/maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java
@@ -218,4 +218,20 @@ public class ParametersMojoTest {
assertDoesNotThrow(mojo::execute);
}
}
+
+ @Test
+ @Basedir("src/test/projects/basedir-set-by-annotation")
+ @InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
+ void basedirInjectedWithBasedirAnnotationDefaultPom(ParametersMojo mojo) {
+ assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
+ assertDoesNotThrow(mojo::execute);
+ }
+
+ @Test
+ @Basedir("/projects/basedir-set-by-annotation-classpath")
+ @InjectMojo(goal = "parameters")
+ void
basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo
mojo) {
+ assertEquals("i-have-a-basedir-set-by-annotation-classpath",
mojo.getPlain());
+ assertDoesNotThrow(mojo::execute);
+ }
}