diff --git a/SUMMARY.md b/SUMMARY.md index c57c446c29..749fb6f88b 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -5,7 +5,6 @@ * [Quickstart: Setting up your project and running recipes](running-recipes/getting-started.md) * [Running Rewrite on a Gradle project without modifying the build](running-recipes/running-rewrite-on-a-gradle-project-without-modifying-the-build.md) * [Running Rewrite on a Maven project without modifying the build](running-recipes/running-rewrite-on-a-maven-project-without-modifying-the-build.md) - * [Running Rewrite without build tool plugins](running-recipes/running-rewrite-without-build-tool-plugins.md) * [Running Rewrite on a multi-module Maven project](running-recipes/multi-module-maven.md) * [Popular recipe guides](running-recipes/popular-recipe-guides/README.md) * [Common static analysis issue remediation](running-recipes/popular-recipe-guides/common-static-analysis-issue-remediation.md) @@ -1433,7 +1432,7 @@ * [Ensure the GKE metadata server is enabled](reference/recipes/terraform/gcp/ensurethegkemetadataserverisenabled.md) * [Search](reference/recipes/terraform/search/README.md) * [Find Terraform resource](reference/recipes/terraform/search/findresource.md) - * [Core](reference/recipes/core-README.md) + * [Core](reference/recipes/core-readme.md) * [Delete files](reference/recipes/deletesourcefiles.md) * [Find LST provenance](reference/recipes/findlstprovenance.md) * [Find colliding source files](reference/recipes/findcollidingsourcefiles.md) diff --git a/running-recipes/running-recipes.md b/running-recipes/running-recipes.md index 5aa4a07b1f..3666d6771c 100644 --- a/running-recipes/running-recipes.md +++ b/running-recipes/running-recipes.md @@ -3,9 +3,8 @@ * [Quickstart: Setting up your project and running recipes](/running-recipes/getting-started.md) * [Running Rewrite on a Gradle project without modifying the build](/running-recipes/running-rewrite-on-a-gradle-project-without-modifying-the-build.md) * [Running Rewrite on a Maven project without modifying the build](/running-recipes/running-rewrite-on-a-maven-project-without-modifying-the-build.md) -* [Running Rewrite without build tool plugins](/running-recipes/running-rewrite-without-build-tool-plugins.md) * [Running Rewrite on a multi-module Maven project](/running-recipes/multi-module-maven.md) -* [Popular recipe guides](/running-recipes/popular-recipe-guides/) +* [Popular recipe guides](/running-recipes/popular-recipe-guides/README.md) * [Common static analysis issue remediation](/running-recipes/popular-recipe-guides/common-static-analysis-issue-remediation.md) * [Automatically fix Checkstyle violations](/running-recipes/popular-recipe-guides/automatically-fix-checkstyle-violations.md) * [Migrate to Java 17](/running-recipes/popular-recipe-guides/migrate-to-java-17.md) diff --git a/running-recipes/running-rewrite-without-build-tool-plugins.md b/running-recipes/running-rewrite-without-build-tool-plugins.md deleted file mode 100644 index beb81af994..0000000000 --- a/running-recipes/running-rewrite-without-build-tool-plugins.md +++ /dev/null @@ -1,118 +0,0 @@ ---- -description: For less common situations when one of the build tool plugins isn't possible. ---- - -# Running Rewrite without build tool plugins - -It's simple to run Rewrite from a regular Java main method outside of the build tool plugins. The hardest part is determining the classpath that is relevant for each set of Java files. In the case of Maven and Gradle projects, locating each source set and the classpath that applies to them is non-trivial enough that it's much easier to use the build tool plugins instead. - -This tutorial is useful primarily for projects that have a simpler structure then, e.g. a project with a simple source directory and a lib directory with all the JARs for its classpath already downloaded. - -In this example, we are going to show running common static analysis issues cleanup over Java code, This recipe that exists in the `rewrite-java` package. For parsing other languages or running other recipes, the process is very similar just with other dependencies (e.g. `rewrite-yaml` for working with YAML). - -At a minimum for this example, you need the following dependencies: - -{% tabs %} -{% tab title="Maven" %} -```xml - - - - org.openrewrite.recipe - rewrite-recipe-bom - 2.4.1 - pom - import - - - - - - org.openrewrite - rewrite-java - - - org.openrewrite - rewrite-java-11 - runtime - - -``` -{% endtab %} - -{% tab title="Gradle" %} -```kotlin -dependencies { - implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.4.1")) - implementation("org.openrewrite:rewrite-java") - runtimeOnly("org.openrewrite:rewrite-java-11") -} -``` -{% endtab %} -{% endtabs %} - -And here is a main method that is mostly complete. You just need to change the `projectDir` and `classpath` variables at the top to point to the root of your Java source files and determine your classpath. - -```java -package org.openrewrite.java; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.InMemoryExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.Result; -import org.openrewrite.config.Environment; -import org.openrewrite.java.tree.J; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.List; -import java.util.stream.Collectors; - -import static java.util.Collections.emptyList; - -public class RunRewriteManually { - public static void main(String[] args) throws IOException { - // determine your project directory and provide a list of - // paths to jars that represent the project's classpath - Path projectDir = Paths.get("."); - List classpath = emptyList(); - - - // put any rewrite recipe jars on this main method's runtime classpath - // and either construct the recipe directly or via an Environment - Environment environment = Environment.builder().scanRuntimeClasspath().build(); - Recipe recipe = environment.activateRecipes("org.openrewrite.java.cleanup.CommonStaticAnalysis"); - - // create a JavaParser instance with your classpath - JavaParser javaParser = JavaParser.fromJavaVersion() - .classpath(classpath) - .build(); - - // walk the directory structure where your Java sources are located - // and create a list of them - List sourcePaths = Files.find(projectDir, 999, (p, bfa) -> - bfa.isRegularFile() && p.getFileName().toString().endsWith(".java")) - .collect(Collectors.toList()); - - ExecutionContext ctx = new InMemoryExecutionContext(Throwable::printStackTrace); - - // parser the source files into LSTs - List cus = javaParser.parse(sourcePaths, projectDir, ctx); - - // collect results - List results = recipe.run(cus, ctx).getResults(); - - for (Result result : results) { - // print diffs to the console - System.out.println(result.diff(projectDir)); - - // or overwrite the file on disk with changes. - // Files.writeString(result.getAfter().getSourcePath(), - // result.getAfter().printAll()); - } - - } -} -```