Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -178,7 +181,13 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
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) {
Expand Down Expand Up @@ -318,6 +327,14 @@ private Mojo lookupMojo(
} 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("");
}
Expand All @@ -337,6 +354,10 @@ private Mojo lookupMojo(
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(":");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,20 @@ void basedirInjectedWithBasedirFromClasspathAnnotation(ParametersMojo mojo) {
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);
}
}