Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for artifact recipes as file #800

Closed
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 @@ -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()) {
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
);
}

}
19 changes: 19 additions & 0 deletions src/main/java/org/openrewrite/maven/ConfigurableRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ protected boolean allProjectsMarked() {
@Parameter(property = "rewrite.recipeArtifactCoordinates")
private LinkedHashSet<String> recipeArtifactCoordinates;

@Nullable
@Parameter(property = "rewrite.recipeArtifactFiles")
private LinkedHashSet<String> recipeArtifactFiles;

@Nullable
private volatile Set<String> computedRecipes;

Expand All @@ -207,6 +211,9 @@ protected boolean allProjectsMarked() {
@Nullable
private volatile Set<String> computedRecipeArtifactCoordinates;

@Nullable
private volatile Set<String> computedRecipeArtifactFiles;

protected Set<String> getActiveRecipes() {
if (computedRecipes == null) {
synchronized (this) {
Expand Down Expand Up @@ -292,6 +299,18 @@ protected Set<String> getRecipeArtifactCoordinates() {
return computedRecipeArtifactCoordinates;
}

protected Set<String> getRecipeArtifactFiles() {
if (computedRecipeArtifactFiles == null) {
synchronized (this) {
if (computedRecipeArtifactFiles == null) {
computedRecipeArtifactFiles = getCleanedSet(recipeArtifactFiles);
}
}
}
//noinspection ConstantConditions
return computedRecipeArtifactFiles;
}

private static Set<String> getCleanedSet(@Nullable Set<String> set) {
if (set == null) {
return Collections.emptySet();
Expand Down