Skip to content

Commit b91b932

Browse files
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
1 parent 302aee7 commit b91b932

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

maven-plugin-testing-harness/src/main/java/org/apache/maven/api/plugin/testing/MojoExtension.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.lang.reflect.InvocationTargetException;
2929
import java.lang.reflect.Method;
3030
import java.net.URL;
31+
import java.nio.file.Files;
3132
import java.nio.file.Path;
3233
import java.nio.file.Paths;
3334
import java.util.ArrayList;
@@ -139,6 +140,8 @@ public class MojoExtension extends PlexusExtension implements ParameterResolver
139140
// Namespace for storing/retrieving data related to MojoExtension
140141
private static final ExtensionContext.Namespace MOJO_EXTENSION = ExtensionContext.Namespace.create("MojoExtension");
141142

143+
public static final String BASEDIR_IS_SET_KEY = "basedirIsSet";
144+
142145
@Override
143146
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
144147
throws ParameterResolutionException {
@@ -178,7 +181,13 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
178181
public void beforeEach(ExtensionContext context) throws Exception {
179182
String basedir = AnnotationSupport.findAnnotation(context.getElement().get(), Basedir.class)
180183
.map(Basedir::value)
181-
.orElseGet(PlexusExtension::getBasedir);
184+
.orElse(null);
185+
186+
if (basedir == null) {
187+
basedir = getBasedir();
188+
} else {
189+
context.getStore(MOJO_EXTENSION).put(BASEDIR_IS_SET_KEY, Boolean.TRUE);
190+
}
182191

183192
URL resource = context.getRequiredTestClass().getResource(basedir);
184193
if (resource != null) {
@@ -315,6 +324,14 @@ private Mojo lookupMojo(
315324
} else if (!pom.isEmpty()) {
316325
Path path = basedir.resolve(pom);
317326
pomDom = Xpp3DomBuilder.build(new XmlStreamReader(path.toFile()));
327+
} else if (isBasedirSet(extensionContext)) {
328+
// only look for a pom.xml if basedir is explicitly set
329+
Path path = basedir.resolve("pom.xml");
330+
if (Files.exists(path)) {
331+
pomDom = Xpp3DomBuilder.build(new XmlStreamReader(path.toFile()));
332+
} else {
333+
pomDom = new Xpp3Dom("");
334+
}
318335
} else {
319336
pomDom = new Xpp3Dom("");
320337
}
@@ -334,6 +351,10 @@ private Mojo lookupMojo(
334351
return lookupMojo(extensionContext, coord, pluginConfiguration, descriptor);
335352
}
336353

354+
private boolean isBasedirSet(ExtensionContext extensionContext) {
355+
return extensionContext.getStore(MOJO_EXTENSION).getOrDefault(BASEDIR_IS_SET_KEY, Boolean.class, Boolean.FALSE);
356+
}
357+
337358
protected String[] mojoCoordinates(String goal, PluginDescriptor pluginDescriptor) throws Exception {
338359
if (goal.matches(".*:.*:.*:.*")) {
339360
return goal.split(":");

maven-plugin-testing-harness/src/test/java/org/apache/maven/plugin/testing/ParametersMojoTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,20 @@ void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
124124
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
125125
assertDoesNotThrow(mojo::execute);
126126
}
127+
128+
@Test
129+
@Basedir("src/test/projects/basedir-set-by-annotation")
130+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
131+
void basedirInjectedWithBasedirAnnotationDefaultPom(ParametersMojo mojo) {
132+
assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
133+
assertDoesNotThrow(mojo::execute);
134+
}
135+
136+
@Test
137+
@Basedir("/projects/basedir-set-by-annotation-classpath")
138+
@InjectMojo(goal = "parameters")
139+
void basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo mojo) {
140+
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
141+
assertDoesNotThrow(mojo::execute);
142+
}
127143
}

0 commit comments

Comments
 (0)