Introduction
Change is the only constant and this is very true for code. Code changes because of changes in requirements, changes in third-part library dependencies, frequent security vulnerabilities etc. Few changes need significant refactoring across the code base. For example, consider migrating a Spring Boot application from 3.5 to 4.0. This is a major change with significant effort. This potentially may cause regression if not done properly. To make larger code refactoring easier, we need deterministic and reliable tool. For this, I came across OpenRewrite as an appropriate tool. It’s an open source automated code refactoring tool. It supports various refactorings like library updates, security vulnerability fixing, clean code updates etc.
Example
To understand it better, let’s consider a simple Spring Boot application to migrate from 3.5 to 4.0.
Currently, it is on Spring Boot 3.5 here.
Let’s migrate it to Spring Boot 4.0 using OpenRewrite.
OpenRewrite uses a recipe to describe a particular refactoring task.
For example, for Spring Boot 4.0 migration there will be a single or multiple recipes which describe the migration.
In this case, it’s org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0.
Details of this recipe can be found here.
These recipes guide the refactoring process.
For this, we will run following maven command from the root of the project.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
--define rewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE \
--define rewrite.activeRecipes=org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0 \
--define rewrite.exportDatatables=true
Here,
`org.openrewrite.maven:rewrite-maven-plugin` <-- maven plugin for rewrite
`org.openrewrite.recipe:rewrite-spring` <-- library which contains Spring Boot 4 migration recipe
`org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0` <-- recipe containing Spring Boot 4 migration steps
This command pulls the required plugin and recipe dynamically and performs the refactoring.
Successful execution of the above command will show the following output in the terminal.

Refactored code can found here.
As we can see, OpenRewrite has updated the Spring Boot version and made related Spring module changes.

Here, it has made changes as per Jackson 3 migration. While making code changes, most importantly it hasn’t changed the formatting of the source file. This avoids unnecessary merge conflict errors.
Ways of using OpenRewrite
- We can dynamically pull the OpenRewrite maven plugin and run the refactoring recipes as shown in above example.
- Another way is to include the plugin details as part of root pom file of the project.
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>6.36.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.OrderImports</recipe>
</activeRecipes>
</configuration>
</plugin>
Here, we have defined the plugin along with recipes needed for the project refactoring.
Then, run using mvn rewrite:run.
Refer here for more details.
- Instead of running directly and changing the code base, we can also run OpenRewrite in dry-run mode to see the changes that will be made by the recipe.
mvn rewrite:dryRun
Proposed changes can be found in target/rewrite/rewrite.patch.

OpenRewrite has a catalog of recipes serving various refactoring needs. Details can be found here.
Similar to maven, OpenRewrite is supported for Gradle environment as well.
Under the Hood
Core of OpenRewrite’s refactoring logic is LST (Lossless Semantic Tree) data structure. LST stores every bit of information about source code including formatting (tabs, whitespaces etc.) details. Thus, OpenRewrite maintains the deterministic nature of code refactoring.
Code refactoring using OpenRewrite is a 3-step process:
- Create a LST from the source code and store it in-memory
- Apply the recipe changes on the LST stored in-memory
- Convert the in-memory LST back into text and overwrite the code base
Being deterministic and accurate in refactoring, OpenRewrite is a reliable tool for enterprise software refactoring needs. So, the next time you need to perform Spring Boot migration, Spring Security migration, Java version migration look for OpenRewrite recipes and let it do the heavy lifting for you.
