2828import java .lang .reflect .InvocationTargetException ;
2929import java .lang .reflect .Method ;
3030import java .net .URL ;
31+ import java .nio .file .Files ;
3132import java .nio .file .Path ;
3233import java .nio .file .Paths ;
3334import 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 (":" );
0 commit comments