Skip to content

Commit 9f2100e

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 ad6036d commit 9f2100e

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) {
@@ -318,6 +327,14 @@ private Mojo lookupMojo(
318327
} else if (!pom.isEmpty()) {
319328
Path path = basedir.resolve(pom);
320329
pomDom = Xpp3DomBuilder.build(new XmlStreamReader(path.toFile()));
330+
} else if (isBasedirSet(extensionContext)) {
331+
// only look for a pom.xml if basedir is explicitly set
332+
Path path = basedir.resolve("pom.xml");
333+
if (Files.exists(path)) {
334+
pomDom = Xpp3DomBuilder.build(new XmlStreamReader(path.toFile()));
335+
} else {
336+
pomDom = new Xpp3Dom("");
337+
}
321338
} else {
322339
pomDom = new Xpp3Dom("");
323340
}
@@ -337,6 +354,10 @@ private Mojo lookupMojo(
337354
return lookupMojo(extensionContext, coord, pluginConfiguration, descriptor);
338355
}
339356

357+
private boolean isBasedirSet(ExtensionContext extensionContext) {
358+
return extensionContext.getStore(MOJO_EXTENSION).getOrDefault(BASEDIR_IS_SET_KEY, Boolean.class, Boolean.FALSE);
359+
}
360+
340361
protected String[] mojoCoordinates(String goal, PluginDescriptor pluginDescriptor) throws Exception {
341362
if (goal.matches(".*:.*:.*:.*")) {
342363
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
@@ -218,4 +218,20 @@ void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
218218
assertDoesNotThrow(mojo::execute);
219219
}
220220
}
221+
222+
@Test
223+
@Basedir("src/test/projects/basedir-set-by-annotation")
224+
@InjectMojo(goal = "test:test-plugin:0.0.1-SNAPSHOT:parameters")
225+
void basedirInjectedWithBasedirAnnotationDefaultPom(ParametersMojo mojo) {
226+
assertEquals("i-have-a-basedir-set-by-annotation", mojo.getPlain());
227+
assertDoesNotThrow(mojo::execute);
228+
}
229+
230+
@Test
231+
@Basedir("/projects/basedir-set-by-annotation-classpath")
232+
@InjectMojo(goal = "parameters")
233+
void basedirInjectedWithBasedirFromClasspathAnnotationDefaultPom(ParametersMojo mojo) {
234+
assertEquals("i-have-a-basedir-set-by-annotation-classpath", mojo.getPlain());
235+
assertDoesNotThrow(mojo::execute);
236+
}
221237
}

0 commit comments

Comments
 (0)