Running Rewrite without build tool plugins
For less common situations when one of the build tool plugins isn't possible.
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:
Maven
Gradle
1
<properties>
2
<rewrite.version>7.16.0</rewrite.version>
3
</properties>
4
<dependencies>
5
<dependency>
6
<groupId>org.openrewrite</groupId>
7
<artifactId>rewrite-java</artifactId>
8
<version>${rewrite.version}</version>
9
</dependency>
10
<dependency>
11
<groupId>org.openrewrite</groupId>
12
<artifactId>rewrite-java-11</artifactId>
13
<version>${rewrite.version}</version>
14
<scope>runtime</scope>
15
</dependency>
16
</dependencies>
Copied!
1
dependencies {
2
implementation("org.openrewrite:rewrite-java:latest.release")
3
runtimeOnly("org.openrewrite:rewrite-java-11:latest.release")
4
}
Copied!
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.
1
package org.openrewrite.java;
2
3
import org.openrewrite.InMemoryExecutionContext;
4
import org.openrewrite.Recipe;
5
import org.openrewrite.Result;
6
import org.openrewrite.config.Environment;
7
import org.openrewrite.java.tree.J;
8
9
import java.io.IOException;
10
import java.nio.file.Files;
11
import java.nio.file.Path;
12
import java.nio.file.Paths;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
16
import static java.util.Collections.emptyList;
17
18
public class RunRewriteManually {
19
public static void main(String[] args) throws IOException {
20
// determine your project directory and provide a list of
21
// paths to jars that represent the project's classpath
22
Path projectDir = Paths.get(".");
23
List<Path> classpath = emptyList();
24
25
26
// put any rewrite recipe jars on this main method's runtime classpath
27
// and either construct the recipe directly or via an Environment
28
Environment environment = Environment.builder().scanRuntimeClasspath().build();
29
Recipe recipe = environment.activateRecipes("org.openrewrite.java.cleanup.CommonStaticAnalysis");
30
31
// create a JavaParser instance with your classpath
32
JavaParser javaParser = JavaParser.fromJavaVersion()
33
.classpath(classpath)
34
.build();
35
36
// walk the directory structure where your Java sources are located
37
// and create a list of them
38
List<Path> sourcePaths = Files.find(projectDir, 999, (p, bfa) ->
39
bfa.isRegularFile() && p.getFileName().toString().endsWith(".java"))
40
.collect(Collectors.toList());
41
42
// parser the source files into ASTs
43
List<J.CompilationUnit> cus = javaParser.parse(sourcePaths, projectDir,
44
new InMemoryExecutionContext(Throwable::printStackTrace));
45
46
// collect results
47
List<Result> results = recipe.run(cus);
48
49
for (Result result : results) {
50
// print diffs to the console
51
System.out.println(result.diff(projectDir));
52
53
// or overwrite the file on disk with changes.
54
// Files.writeString(result.getAfter().getSourcePath(),
55
// result.getAfter().printAll());
56
}
57
58
}
59
}
Copied!
Last modified 1mo ago
Export as PDF
Copy link