diff --git a/pom.xml b/pom.xml index 2d0ed3e6..3d559a20 100644 --- a/pom.xml +++ b/pom.xml @@ -366,22 +366,6 @@ - - - src/main/resources - true - - **/version.txt - - - - src/main/resources - false - - **/version.txt - - - src/test/resources diff --git a/src/main/java/org/openrewrite/maven/ConfigureMojo.java b/src/main/java/org/openrewrite/maven/ConfigureMojo.java deleted file mode 100644 index f0351c58..00000000 --- a/src/main/java/org/openrewrite/maven/ConfigureMojo.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.Execute; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.Result; -import org.openrewrite.SourceFile; -import org.openrewrite.internal.InMemoryLargeSourceSet; -import org.openrewrite.xml.tree.Xml; - -import javax.annotation.Nullable; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * Configure rewrite-maven-plugin (or any other plugin) in the project.
- * For example:
- * {@code ./mvnw rewrite:configure -DactiveRecipes=org.openrewrite.java.spring.boot2.SpringBoot1To2Migration -Ddependencies=org.openrewrite.recipe:rewrite-spring:4.17.0} - */ -@Mojo(name = "configure", threadSafe = true) -@Execute -@SuppressWarnings("unused") -public class ConfigureMojo extends AbstractRewriteMojo { - - @Parameter(property = "groupId", defaultValue = "org.openrewrite.maven") - protected String groupId; - - @Parameter(property = "artifactId", defaultValue = "rewrite-maven-plugin") - protected String artifactId; - - @Parameter(property = "configuration") - @Nullable - protected String configuration; - - @Parameter(property = "dependencies") - @Nullable - protected String dependencies; - - @Parameter(property = "executionPhase") - @Nullable - protected String executionPhase; - - @Parameter(property = "executionGoals") - @Nullable - protected String executionGoals; - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - Path baseDir = getBuildRoot(); - - ExecutionContext ctx = executionContext(); - Xml.Document maven = new MavenMojoProjectParser(getLog(), baseDir, pomCacheEnabled, pomCacheDirectory, runtime, skipMavenParsing, getExclusions(), getPlainTextMasks(), sizeThresholdMb, mavenSession, settingsDecrypter, runPerSubmodule).parseMaven(project, Collections.emptyList(), ctx); - Recipe recipe = new Recipe() { - @Override - public String getDisplayName() { - return ConfigureMojo.class.getName(); - } - - @Override - public String getDescription() { - return ConfigureMojo.class.getName() + " recipe."; - } - - @Override - public List getRecipeList() { - return Arrays.asList(new ChangePluginDependencies(groupId, artifactId, dependencies), new ChangePluginExecutions(groupId, artifactId, getExecutions())); - } - }; - - List poms = Collections.singletonList(maven); - List results = recipe.run(new InMemoryLargeSourceSet(poms), ctx).getChangeset().getAllResults(); - if (results.isEmpty()) { - getLog().warn("No changes made to plugin " + artifactId + " configuration"); - return; - } - Result result = results.get(0); - - assert result.getBefore() != null; - assert result.getAfter() != null; - Charset charset = result.getAfter().getCharset() == null ? StandardCharsets.UTF_8 : result.getAfter().getCharset(); - try (BufferedWriter sourceFileWriter = Files.newBufferedWriter( - baseDir.resolve(result.getBefore().getSourcePath()), charset)) { - sourceFileWriter.write(new String(result.getAfter().printAll().getBytes(charset), charset)); - } catch (IOException e) { - throw new RuntimeException(e); - } - getLog().info("Changed " + artifactId + " in " + project.getFile().getPath()); - } - - @Nullable - protected String getConfiguration() { - Set activeRecipes = getActiveRecipes(); - if (configuration == null && !activeRecipes.isEmpty()) { - configuration = "\n" + - activeRecipes.stream() - .map(it -> "" + it + "") - .collect(Collectors.joining("\n")) - + "\n"; - } - return configuration; - } - - @Nullable - protected String getExecutions() { - String executions = null; - if (executionPhase != null && executionGoals != null) { - executions = "\n" + - "" + executionPhase + "\n" - + "\n" - + Arrays.stream(executionGoals.split(",")) - .map(it -> "" + it + "") - .collect(Collectors.joining("\n")) - + "\n\n"; - } - return executions; - } -} diff --git a/src/main/java/org/openrewrite/maven/InitMojo.java b/src/main/java/org/openrewrite/maven/InitMojo.java deleted file mode 100644 index 321071c8..00000000 --- a/src/main/java/org/openrewrite/maven/InitMojo.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugins.annotations.Execute; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.Result; -import org.openrewrite.SourceFile; -import org.openrewrite.internal.InMemoryLargeSourceSet; - -import javax.annotation.Nullable; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.*; -import java.util.stream.Collectors; - -/** - * Add rewrite-maven-plugin (or any other plugin) to the project.
- * For example:
- * {@code ./mvnw rewrite:init -DactiveRecipes=org.openrewrite.java.spring.boot2.SpringBoot1To2Migration -Ddependencies=org.openrewrite.recipe:rewrite-spring:4.17.0} - */ -@Mojo(name = "init", threadSafe = true) -@Execute -@SuppressWarnings("unused") -public class InitMojo extends AbstractRewriteMojo { - - @Parameter(property = "groupId", defaultValue = "org.openrewrite.maven") - protected String groupId; - - @Parameter(property = "artifactId", defaultValue = "rewrite-maven-plugin") - protected String artifactId; - - @Parameter(property = "version") - @Nullable - protected String version; - - @Parameter(property = "configuration") - @Nullable - protected String configuration; - - @Parameter(property = "dependencies") - @Nullable - protected String dependencies; - - @Parameter(property = "executionPhase") - @Nullable - protected String executionPhase; - - @Parameter(property = "executionGoals") - @Nullable - protected String executionGoals; - - @Parameter(property = "rootOnly", defaultValue = "true") - protected boolean rootOnly; - - @Override - public void execute() throws MojoExecutionException { - Path baseDir = getBuildRoot(); - if (rootOnly && !project.getBasedir().toPath().equals(baseDir)) { - getLog().warn("Skipping non-root project " + project.getFile().getPath()); - return; - } - ExecutionContext ctx = executionContext(); - MavenParser mp = MavenParser.builder() - .mavenConfig(baseDir.resolve(".mvn/maven.config")) - .build(); - Recipe recipe = new Recipe() { - @Override - public String getDisplayName() { - return InitMojo.class.getName(); - } - - @Override - public String getDescription() { - return InitMojo.class.getName() + " recipe."; - } - - @Override - public List getRecipeList() { - return Arrays.asList( - new AddPlugin(groupId, artifactId, getVersion(), getConfiguration(), null, getExecutions()), - new ChangePluginDependencies(groupId, artifactId, dependencies) - ); - } - }; - - List poms = mp.parse(Collections.singleton(project.getFile().toPath()), baseDir, ctx).collect(Collectors.toList()); - List results = recipe.run(new InMemoryLargeSourceSet(poms), ctx).getChangeset().getAllResults(); - if (results.isEmpty()) { - getLog().warn("Plugin " + artifactId + " is already part of the build"); - return; - } - Result result = results.get(0); - - assert result.getBefore() != null; - assert result.getAfter() != null; - Charset charset = result.getAfter().getCharset() == null ? StandardCharsets.UTF_8 : result.getAfter().getCharset(); - try (BufferedWriter sourceFileWriter = Files.newBufferedWriter( - baseDir.resolve(result.getBefore().getSourcePath()), charset)) { - sourceFileWriter.write(new String(result.getAfter().printAll().getBytes(charset), charset)); - } catch (IOException e) { - throw new RuntimeException(e); - } - getLog().info("Added " + artifactId + " to " + project.getFile().getPath()); - } - - protected String getVersion() { - if (version == null) { - //noinspection ConstantConditions - return new Scanner(InitMojo.class.getResourceAsStream("/version.txt"), "UTF-8").next().trim(); - } - return version; - } - - @Nullable - protected String getConfiguration() { - Set activeRecipes = getActiveRecipes(); - if (configuration == null && !activeRecipes.isEmpty()) { - configuration = "\n\n" + - activeRecipes.stream() - .map(it -> "" + it + "") - .collect(Collectors.joining("\n")) - + "\n"; - } - return configuration; - } - - @Nullable - protected String getExecutions() { - String executions = null; - if (executionPhase != null && executionGoals != null) { - executions = "\n\n" + - "" + executionPhase + "\n" - + "\n" - + Arrays.stream(executionGoals.split(",")) - .map(it -> "" + it + "") - .collect(Collectors.joining("\n")) - + "\n\n\n"; - } - return executions; - } -} diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt deleted file mode 100644 index ad96e7cf..00000000 --- a/src/main/resources/version.txt +++ /dev/null @@ -1 +0,0 @@ -${project.version} diff --git a/src/test/java/org/openrewrite/maven/ConfigureMojoIT.java b/src/test/java/org/openrewrite/maven/ConfigureMojoIT.java deleted file mode 100644 index 153b6780..00000000 --- a/src/test/java/org/openrewrite/maven/ConfigureMojoIT.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import com.soebes.itf.jupiter.extension.*; -import com.soebes.itf.jupiter.maven.MavenExecutionResult; - -import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat; - -@MavenJupiterExtension -@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS) -@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING) -@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:configure") -@SystemProperties({ - @SystemProperty(value = "activeRecipes", content = "org.openrewrite.java.testing.junit5.ParameterizedRunnerToParameterized"), - @SystemProperty(value = "dependencies", content = "org.openrewrite.recipe:rewrite-spring:4.14.1") -}) -@SuppressWarnings("NewClassNamingConvention") -class ConfigureMojoIT { - - @MavenTest - void single_project(MavenExecutionResult result) { - assertThat(result) - .isSuccessful() - .out() - .info() - .anySatisfy(line -> assertThat(line).contains("Changed rewrite-maven-plugin in")); - } - -} diff --git a/src/test/java/org/openrewrite/maven/InitMojoIT.java b/src/test/java/org/openrewrite/maven/InitMojoIT.java deleted file mode 100644 index 2a69d16f..00000000 --- a/src/test/java/org/openrewrite/maven/InitMojoIT.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2021 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.maven; - -import com.soebes.itf.jupiter.extension.*; -import com.soebes.itf.jupiter.maven.MavenExecutionResult; - -import static com.soebes.itf.extension.assertj.MavenITAssertions.assertThat; - -@MavenJupiterExtension -@MavenOption(MavenCLIOptions.NO_TRANSFER_PROGRESS) -@MavenOption(MavenCLIExtra.MUTE_PLUGIN_VALIDATION_WARNING) -@MavenGoal("${project.groupId}:${project.artifactId}:${project.version}:init") -@SystemProperties({ - @SystemProperty(value = "activeRecipes", content = "org.openrewrite.java.testing.junit5.ParameterizedRunnerToParameterized"), - @SystemProperty(value = "dependencies", content = "org.openrewrite.recipe:rewrite-spring:4.14.1"), - @SystemProperty(value = "rootOnly", content = "false") -}) -@SuppressWarnings("NewClassNamingConvention") -class InitMojoIT { - - @MavenTest - void single_project(MavenExecutionResult result) { - assertThat(result) - .isSuccessful() - .out() - .info() - .anySatisfy(line -> assertThat(line).contains("Added rewrite-maven-plugin to")); - } - -} diff --git a/src/test/resources-its/org/openrewrite/maven/ConfigureMojoIT/single_project/pom.xml b/src/test/resources-its/org/openrewrite/maven/ConfigureMojoIT/single_project/pom.xml deleted file mode 100644 index 415cad54..00000000 --- a/src/test/resources-its/org/openrewrite/maven/ConfigureMojoIT/single_project/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - - org.openrewrite.maven - single_project - 1.0 - jar - ConfigureMojoIT#single_project - - - 1.8 - 1.8 - UTF-8 - - - - - - @project.groupId@ - @project.artifactId@ - @project.version@ - - - org.openrewrite.java.format.AutoFormat - - - - - - diff --git a/src/test/resources-its/org/openrewrite/maven/InitMojoIT/single_project/pom.xml b/src/test/resources-its/org/openrewrite/maven/InitMojoIT/single_project/pom.xml deleted file mode 100644 index 86f8667a..00000000 --- a/src/test/resources-its/org/openrewrite/maven/InitMojoIT/single_project/pom.xml +++ /dev/null @@ -1,17 +0,0 @@ - - 4.0.0 - - org.openrewrite.maven - single_project - 1.0 - jar - InitMojoIT#single_project - - - 1.8 - 1.8 - UTF-8 - -