diff --git a/astra-maven-plugin/src/main/java/org/alfasoftware/astra/RefactorMojo.java b/astra-maven-plugin/src/main/java/org/alfasoftware/astra/RefactorMojo.java index ce24237..ced3788 100644 --- a/astra-maven-plugin/src/main/java/org/alfasoftware/astra/RefactorMojo.java +++ b/astra-maven-plugin/src/main/java/org/alfasoftware/astra/RefactorMojo.java @@ -1,7 +1,10 @@ package org.alfasoftware.astra; +import static java.util.stream.Collectors.toList; + import java.io.File; import java.lang.reflect.InvocationTargetException; +import java.net.URL; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -10,13 +13,16 @@ import org.alfasoftware.astra.core.refactoring.UseCase; import org.alfasoftware.astra.core.utils.ASTOperation; import org.alfasoftware.astra.core.utils.AstraCore; +import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DependencyResolutionRequiredException; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.classworlds.realm.ClassRealm; /** * Runs an ASTRA UseCase refactor over the given module. @@ -29,7 +35,7 @@ public class RefactorMojo extends AbstractMojo { */ @Parameter(required = true, readonly = true, defaultValue = "${project}") protected MavenProject project; - + /** * The usecase fully-qualified class name */ @@ -56,19 +62,51 @@ public class RefactorMojo extends AbstractMojo { private String targetDirectory; + @Parameter(defaultValue = "${plugin}", readonly = true, required = true) + private PluginDescriptor pluginDescriptor; + + private void augmentClassLoaderWithTestDependencies() throws Exception { + ClassRealm pluginRealm = pluginDescriptor.getClassRealm(); + + List testDependencies = resolveTestDependencies(); + + for (File file : testDependencies) { + URL url = file.toURI().toURL(); + pluginRealm.addURL(url); + } + } + + private List resolveTestDependencies() { + return project + .getArtifacts() + .stream() + .filter(artifact -> "test".equals(artifact.getScope())) + .map(Artifact::getFile) + .collect(toList()); + } + + @Override public void execute() throws MojoExecutionException { + try { + // Maven class loader for plug-in will not contain module test dependencies + augmentClassLoaderWithTestDependencies(); + } catch (Exception e) { + throw new MojoExecutionException("Unable to augment class loader with test-scoped dependencies", e); + } + List testClasspathElements; try { - testClasspathElements = project.getTestClasspathElements(); + testClasspathElements = project.getTestClasspathElements(); } catch (DependencyResolutionRequiredException e) { throw new MojoExecutionException("Unable to resolve test class path for the project", e); } + // remove anything within this projects target directory as this will invalidate it testClasspathElements.removeIf(s -> s.startsWith(targetDirectory)); - + UseCase useCaseInstance = getUseCaseInstance(); AstraCore.run(sourceDirectory.getAbsolutePath(), new UseCase() { @@ -84,8 +122,9 @@ public Predicate getPrefilteringPredicate() { @Override public Set getAdditionalClassPathEntries() { - // This is a Maven plugin that can access the full classpath required for the source code Astra is running over - no additional help required. - return Set.of(); + // This is a Maven plugin that can access the full classpath required for the source code Astra is running over + // test classpath entries need to be added + return new HashSet<>(testClasspathElements); } }); @@ -101,7 +140,7 @@ private UseCase getUseCaseInstance() throws MojoExecutionException { } return (UseCase)useCaseClazz.getDeclaredConstructors()[0].newInstance(); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | SecurityException | ClassNotFoundException e) { + | SecurityException | ClassNotFoundException e) { throw new MojoExecutionException(String.format("Unable to instantiate usecase [%s]", usecase), e); } }