Spring Rewrite Commons provides a set of components to parse a Java project to OpenRewrite LST and apply recipes outside a build tool plugin.
Maven
<dependency>
<groupId>org.springframwork.rewrite</groupId>
<artifactId>spring-rewrite-commons-launcher</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
Gradle
compile 'org.springframework.rewrite:spring-rewrite-commons-launcher:0.1.0-SNAPSHOT'
package com.acme.example;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.rewrite.RewriteProjectParser;
import org.springframework.rewrite.parser.RewriteProjectParsingResult;
import org.springframework.rewrite.resource.ProjectResourceSet;
import org.springframework.rewrite.resource.ProjectResourceSetFactory;
import org.springframework.rewrite.resource.ProjectResourceSetSerializer;
import org.springframework.rewrite.RewriteRecipeDiscovery;
import org.springframework.stereotype.Component;
import java.nio.file.Path;
@Component
public class MyMigrationApplication {
@Autowired (1)
private RewriteProjectParser parser;
@Autowired
private RewriteRecipeDiscovery discovery; (2)
@Autowired
private ProjectResourceSetFactory resourceSetFactory;
@Autowired
private ProjectResourceSetSerializer serializer;
public void migrateToBoot3_1() {
Path baseDir = Path.of("."); (2)
String recipeName = "org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_1";
Recipe boot3Upgrade = discovery.getByName(recipeName); (3)
RewriteProjectParsingResult result = parser.parse(baseDir); (4)
List<SourceFile> lst = result.sourceFiles(); (5)
ProjectResourceSet resourceSet = resourceSetFactory.create(baseDir, lst); (6)
resourceSet.apply(boot3Upgrade); (7)
serializer.writeChanges(resourceSet); (8)
}
}
-
All components are Spring beans and can be injected as such.
-
The path of the project that should be migrated.
-
RewriteRecipeDiscovery
is used to discover an OpenRewrite recipe by name. -
RewriteProjectParser
parses a given project to OpenRewrite LST. -
The result contains the list of
SourceFile
s (the LST). -
ProjectResourceSetFactory
can be used to create aProjectResourceSet
. -
The recipe is applied to the
ProjectResourceSet
which wraps the LST. -
ProjectResourceSetSerializer
is used to serialize the changes to disk.
Find the reference documentation here.
Pull requests are welcome. Note, that we expect everyone to follow the code of conduct.
Spring Rewrite Commons is Open Source software released under the Apache 2.0 license.