Plugin invoker requires Maven plugin version (#90)
This commit is contained in:
@@ -46,6 +46,8 @@ public class RewriteMavenPlugin
|
||||
|
||||
private boolean debug;
|
||||
|
||||
private String rewriteMavenPluginVersion;
|
||||
|
||||
@Override
|
||||
public RewriteMavenPluginBuilder.FinalizingBuilder withDependencies(String... dependencies) {
|
||||
this.dependencies = Arrays.asList(dependencies);
|
||||
@@ -113,29 +115,36 @@ public class RewriteMavenPlugin
|
||||
builder.withMemory(minMemory, maxMemory);
|
||||
}
|
||||
MavenInvocationResult result = execute(baseDir, debug, debugConfig, builder.build(), goal, recipes,
|
||||
dependencies);
|
||||
dependencies, rewriteMavenPluginVersion);
|
||||
|
||||
return new PluginInvocationResult(result.getExitCode() != 0 ? false : true, result.getCapturedLines());
|
||||
}
|
||||
|
||||
static MavenInvocationResult execute(Path baseDir, boolean debug, DebugConfig debugConfig, BuildConfig buildConfig,
|
||||
Goal openRewriteGoal, List<String> recipes1, List<String> dependencies1) {
|
||||
String openRewriteCommand = renderOpenRewriteCommand(recipes1, dependencies1, openRewriteGoal);
|
||||
Goal openRewriteGoal, List<String> recipes1, List<String> dependencies1, String rewriteMavenPluginVersion) {
|
||||
String openRewriteCommand = renderOpenRewriteCommand(recipes1, dependencies1, openRewriteGoal,
|
||||
rewriteMavenPluginVersion);
|
||||
List<String> goals = List.of("--fail-at-end", openRewriteCommand);
|
||||
return MavenInvoker.runGoals(baseDir, debugConfig, debug, buildConfig, goals);
|
||||
}
|
||||
|
||||
private static String renderOpenRewriteCommand(List<String> recipeNames, List<String> dependencies,
|
||||
Goal rewritePluginGoal) {
|
||||
Goal rewritePluginGoal, String rewriteMavenPluginVersion) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("org.openrewrite.maven:rewrite-maven-plugin:").append(rewritePluginGoal.getMaven()).append(" ");
|
||||
sb.append("org.openrewrite.maven:rewrite-maven-plugin:")
|
||||
.append(rewriteMavenPluginVersion)
|
||||
.append(":")
|
||||
.append(rewritePluginGoal.getMaven())
|
||||
.append(" ");
|
||||
String recipesList = recipeNames.stream().collect(Collectors.joining(","));
|
||||
sb.append("-Drewrite.activeRecipes=").append(recipesList);
|
||||
if (!dependencies.isEmpty()) {
|
||||
String dependenciesList = dependencies.stream().collect(Collectors.joining(","));
|
||||
sb.append(" ").append("-Drewrite.recipeArtifactCoordinates=").append(dependenciesList);
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
String cmd = sb.toString();
|
||||
return cmd;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -145,6 +154,12 @@ public class RewriteMavenPlugin
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RewriteMavenPluginBuilder.FinalizingBuilder withMavenPluginVersion(String mavenPluginVersion) {
|
||||
this.rewriteMavenPluginVersion = mavenPluginVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
public enum Goal {
|
||||
|
||||
RUN("run"), RUN_NO_FORK("runNoFork"), DRY_RUN("dryRun"), DRY_RUN_NO_FORK("dryRunNoFork"), DISCOVER("discover"),
|
||||
|
||||
@@ -26,11 +26,17 @@ public interface RewriteMavenPluginBuilder {
|
||||
|
||||
public interface Recipes {
|
||||
|
||||
FinalizingBuilder recipes(String... recipeNames);
|
||||
MavenPLuginVersionBuilder recipes(String... recipeNames);
|
||||
|
||||
}
|
||||
|
||||
interface FinalizingBuilder {
|
||||
interface MavenPLuginVersionBuilder {
|
||||
|
||||
FinalizingBuilder withMavenPluginVersion(String mavenPluginVersion);
|
||||
|
||||
}
|
||||
|
||||
interface FinalizingBuilder extends MavenPLuginVersionBuilder {
|
||||
|
||||
FinalizingBuilder withDependencies(String... dependencies);
|
||||
|
||||
|
||||
@@ -54,7 +54,10 @@ class RewriteMavenPluginTest {
|
||||
void simpleProjectSimpleConfig() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
|
||||
PluginInvocationResult result = RewriteMavenPlugin.run().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.run()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
|
||||
String out = result.capturedOutput();
|
||||
assertThat(result.success()).isTrue();
|
||||
@@ -66,7 +69,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("builder runNoFork")
|
||||
void simpleProjectSimpleConfigRunNoFork() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.runNoFork().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.runNoFork()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
String out = result.capturedOutput();
|
||||
assertThat(result.success()).isTrue();
|
||||
assertTasksExecuted(out, "runNoFork");
|
||||
@@ -77,7 +83,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("builder dryRun")
|
||||
void simpleProjectSimpleConfigDryRun() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.dryRun().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.dryRun()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
String out = result.capturedOutput();
|
||||
assertThat(result.success()).isTrue();
|
||||
assertTasksExecuted(out, tasksOf(DEFAULT_GOALS, "dryRun"));
|
||||
@@ -88,7 +97,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("builder dryRunNoFork")
|
||||
void simpleProjectSimpleConfigDryRunNoFork() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.dryRunNoFork().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.dryRunNoFork()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
String out = result.capturedOutput();
|
||||
assertThat(result.success()).isTrue();
|
||||
assertTasksExecuted(out, "dryRunNoFork");
|
||||
@@ -99,7 +111,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("builder discover")
|
||||
void simpleProjectSimpleConfigDiscover() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.discover().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.discover()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
String out = result.capturedOutput();
|
||||
assertThat(result.success()).isTrue();
|
||||
assertTasksExecuted(out, "discover");
|
||||
@@ -110,7 +125,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("builder cyclonedx")
|
||||
void simpleProjectSimpleConfigCyclonedx() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.cyclonedx().recipes(RECIPES).onDir(baseDir);
|
||||
PluginInvocationResult result = RewriteMavenPlugin.cyclonedx()
|
||||
.recipes(RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.onDir(baseDir);
|
||||
String out = result.capturedOutput();
|
||||
System.out.println(out);
|
||||
assertThat(result.success()).isTrue();
|
||||
@@ -125,6 +143,7 @@ class RewriteMavenPluginTest {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewriteMavenPlugin.run()
|
||||
.recipes(EXTENDED_RECIPES)
|
||||
.withMavenPluginVersion("5.32.1")
|
||||
.withDependencies(EXTENDED_REFCIPES_DEPS)
|
||||
.withDebugger(port, false)
|
||||
.withDebug()
|
||||
@@ -145,9 +164,10 @@ class RewriteMavenPluginTest {
|
||||
|
||||
int port = TestSocketUtils.findAvailableTcpPort();
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
String rewriteMavenPluginVersion = "5.32.1";
|
||||
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, true, DebugConfig.from(port, false),
|
||||
BuildConfig.builder().withMemory("1G", "4G").build(), RewriteMavenPlugin.Goal.DRY_RUN,
|
||||
Arrays.asList(RECIPES), List.of());
|
||||
Arrays.asList(RECIPES), List.of(), rewriteMavenPluginVersion);
|
||||
String out = result.getCapturedLines();
|
||||
assertDebugConfig(out, port, false);
|
||||
assertMemory(out, "1G", "4G");
|
||||
@@ -179,9 +199,10 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("execute discover")
|
||||
void executeDiscover() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
String rewriteMavenPluginVersion = "5.32.1";
|
||||
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
|
||||
BuildConfig.fromDefault(), RewriteMavenPlugin.Goal.DISCOVER, Arrays.asList(EXTENDED_RECIPES),
|
||||
List.of(EXTENDED_REFCIPES_DEPS));
|
||||
List.of(EXTENDED_REFCIPES_DEPS), rewriteMavenPluginVersion);
|
||||
String out = result.getCapturedLines();
|
||||
assertThat(result.getExitCode()).isEqualTo(0);
|
||||
assertTasksExecuted(out, "discover");
|
||||
@@ -192,17 +213,20 @@ class RewriteMavenPluginTest {
|
||||
@DisplayName("execute cylonedx")
|
||||
void executeCyclonedx() {
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
String rewriteMavenPluginVersion = "5.32.1";
|
||||
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
|
||||
BuildConfig.fromDefault(), RewriteMavenPlugin.Goal.CYCLONEDX, Arrays.asList(EXTENDED_RECIPES),
|
||||
List.of(EXTENDED_REFCIPES_DEPS));
|
||||
List.of(EXTENDED_REFCIPES_DEPS), rewriteMavenPluginVersion);
|
||||
String out = result.getCapturedLines();
|
||||
assertThat(result.getExitCode()).isEqualTo(0);
|
||||
assertTasksExecuted(out, "cyclonedx");
|
||||
}
|
||||
|
||||
private void verify(Path baseDir, RewriteMavenPlugin.Goal runNoFork, String expectedGoal) {
|
||||
String rewriteMavenPluginVersion = "5.32.1";
|
||||
MavenInvocationResult result = RewriteMavenPlugin.execute(baseDir, false, DebugConfig.disabled(),
|
||||
BuildConfig.fromDefault(), runNoFork, Arrays.asList(EXTENDED_RECIPES), List.of(EXTENDED_REFCIPES_DEPS));
|
||||
BuildConfig.fromDefault(), runNoFork, Arrays.asList(EXTENDED_RECIPES), List.of(EXTENDED_REFCIPES_DEPS),
|
||||
rewriteMavenPluginVersion);
|
||||
String out = result.getCapturedLines();
|
||||
assertThat(result.getExitCode()).isEqualTo(0);
|
||||
assertRecipesExecuted(out, EXTENDED_RECIPES);
|
||||
|
||||
@@ -42,6 +42,8 @@ public class RewritePlugin implements OpenRewritePluginBuilder.GradlePluginVersi
|
||||
|
||||
private String gradlePluginVersion;
|
||||
|
||||
private String mavenPluginVersion;
|
||||
|
||||
private List<String> recipes = new ArrayList<>();
|
||||
|
||||
private boolean debug;
|
||||
@@ -80,6 +82,12 @@ public class RewritePlugin implements OpenRewritePluginBuilder.GradlePluginVersi
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenRewritePluginBuilder.GradlePluginVersion mavenPluginVersion(String mavenPluginVersion) {
|
||||
this.mavenPluginVersion = mavenPluginVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OpenRewritePluginBuilder.FinalizingBuilder recipes(String... recipes) {
|
||||
this.recipes = Arrays.asList(recipes);
|
||||
@@ -151,7 +159,8 @@ public class RewritePlugin implements OpenRewritePluginBuilder.GradlePluginVersi
|
||||
default -> builder = RewriteMavenPlugin.run();
|
||||
}
|
||||
RewriteMavenPluginBuilder.FinalizingBuilder finalizingBuilder = builder
|
||||
.recipes(this.recipes.toArray(String[]::new));
|
||||
.recipes(this.recipes.toArray(String[]::new))
|
||||
.withMavenPluginVersion("5.32.1");
|
||||
if (minMemory != null) {
|
||||
finalizingBuilder = finalizingBuilder.withMemory(minMemory, maxMemory);
|
||||
}
|
||||
@@ -226,6 +235,8 @@ class OpenRewritePluginBuilder {
|
||||
|
||||
Recipes gradlePluginVersion(String pluginVersion);
|
||||
|
||||
GradlePluginVersion mavenPluginVersion(String s);
|
||||
|
||||
}
|
||||
|
||||
public interface Recipes {
|
||||
|
||||
@@ -118,6 +118,7 @@ public class RewritePluginTest {
|
||||
int port = TestSocketUtils.findAvailableTcpPort();
|
||||
Path baseDir = Path.of("./testcode/maven-projects/simple").toAbsolutePath().normalize();
|
||||
PluginInvocationResult result = RewritePlugin.run()
|
||||
.mavenPluginVersion("5.32.1")
|
||||
.gradlePluginVersion("6.10.0")
|
||||
.recipes(EXTENDED_RECIPES)
|
||||
.dependencies(EXTENDED_RECIPES_DEPS)
|
||||
|
||||
Reference in New Issue
Block a user