Skip to content
Merged
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
@@ -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;
Expand All @@ -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.
Expand All @@ -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
*/
Expand All @@ -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<File> testDependencies = resolveTestDependencies();

for (File file : testDependencies) {
URL url = file.toURI().toURL();
pluginRealm.addURL(url);
}
}

private List<File> 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<String> 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() {

Expand All @@ -84,8 +122,9 @@ public Predicate<String> getPrefilteringPredicate() {

@Override
public Set<String> 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);

}
});
Expand All @@ -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);
}
}
Expand Down