diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteBaseRunMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteBaseRunMojo.java index 600fa558..7eb78124 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteBaseRunMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteBaseRunMojo.java @@ -85,10 +85,19 @@ protected ResultsContainer listResults(ExecutionContext ctx) throws MojoExecutio } URLClassLoader recipeArtifactCoordinatesClassloader = getRecipeArtifactCoordinatesClassloader(); + URLClassLoader fileRecipeFileClassLoader = getRecipeArtifactFileClassloader(); + Environment env = null; if (recipeArtifactCoordinatesClassloader != null) { merge(getClass().getClassLoader(), recipeArtifactCoordinatesClassloader); + env = environment(recipeArtifactCoordinatesClassloader); + } + else if (fileRecipeFileClassLoader != null) { + merge(getClass().getClassLoader(), fileRecipeFileClassLoader); + env = environment(fileRecipeFileClassLoader); + } + else { + env = environment(); } - Environment env = environment(recipeArtifactCoordinatesClassloader); Recipe recipe = env.activateRecipes(getActiveRecipes()); if (recipe.getRecipeList().isEmpty()) { diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java index c26ed3f4..55218554 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java @@ -60,7 +60,12 @@ public abstract class AbstractRewriteMojo extends ConfigurableRewriteMojo { protected RepositorySystem repositorySystem; protected Environment environment() throws MojoExecutionException { - return environment(getRecipeArtifactCoordinatesClassloader()); + URLClassLoader classLoader = getRecipeArtifactCoordinatesClassloader(); + if (classLoader != null) { + return environment(classLoader); + } + classLoader = getRecipeArtifactFileClassloader(); + return environment(classLoader); } static class Config { @@ -190,4 +195,35 @@ protected URLClassLoader getRecipeArtifactCoordinatesClassloader() throws MojoEx AbstractRewriteMojo.class.getClassLoader() ); } + + @Nullable + protected URLClassLoader getRecipeArtifactFileClassloader() throws MojoExecutionException { + if (getRecipeArtifactFiles().isEmpty()) { + return null; + } + ArtifactResolver resolver = new ArtifactResolver(repositorySystem, mavenSession); + + URL[] urls = getRecipeArtifactFiles().stream() + .map(File::new) + .peek(file -> { + if (!file.exists()) { + throw new RuntimeException("Recipe Artifact file " + file.getPath() + " does not exist"); + } + }) + .map(File::toURI) + .map(uri -> { + try { + return uri.toURL(); + } catch (MalformedURLException e) { + throw new RuntimeException("Failed to resolve artifacts from rewrite.recipeArtifactFiles", e); + } + }) + .toArray(URL[]::new); + + return new URLClassLoader( + urls, + AbstractRewriteMojo.class.getClassLoader() + ); + } + } diff --git a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java index 0841eadc..b8996610 100644 --- a/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java @@ -198,6 +198,10 @@ protected boolean allProjectsMarked() { @Parameter(property = "rewrite.recipeArtifactCoordinates") private LinkedHashSet recipeArtifactCoordinates; + @Nullable + @Parameter(property = "rewrite.recipeArtifactFiles") + private LinkedHashSet recipeArtifactFiles; + @Nullable private volatile Set computedRecipes; @@ -207,6 +211,9 @@ protected boolean allProjectsMarked() { @Nullable private volatile Set computedRecipeArtifactCoordinates; + @Nullable + private volatile Set computedRecipeArtifactFiles; + protected Set getActiveRecipes() { if (computedRecipes == null) { synchronized (this) { @@ -292,6 +299,18 @@ protected Set getRecipeArtifactCoordinates() { return computedRecipeArtifactCoordinates; } + protected Set getRecipeArtifactFiles() { + if (computedRecipeArtifactFiles == null) { + synchronized (this) { + if (computedRecipeArtifactFiles == null) { + computedRecipeArtifactFiles = getCleanedSet(recipeArtifactFiles); + } + } + } + //noinspection ConstantConditions + return computedRecipeArtifactFiles; + } + private static Set getCleanedSet(@Nullable Set set) { if (set == null) { return Collections.emptySet();