OpenRewrite: Easy Code Refactoring for Seamless Development
Turning project updates into a breeze: A guide to effortless refactoring with OpenRewrite
Have you ever wished for a coding companion that effortlessly updates your project? Enter OpenRewrite, a must-have toolkit for code transformation.
OpenRewrite is a powerful refactoring ecosystem that automates the often tedious task of code maintenance. Its standout feature lies in applying “recipes” to your source code. These recipes, written in Java, act as intelligent directives, guiding OpenRewrite through various transformations.
Whether upgrading dependencies, applying crucial security patches, transitioning between technologies (like migrating from Spring Boot 2 to Spring Boot 3), or simply cleaning up your code, OpenRewrite can help.
The usage is very straightforward:

The beauty of OpenRewrite extends beyond its built-in recipes, as it thrives on community contributions. Everybody can help by adding new recipes to solve shared problems.
In this tutorial, I’ll show you how to update your code in a few easy steps using the OpenRewrite plugin in a Maven-based project.
Let’s explore it!
OpenRewrite Demo
Now that you’ve understood how OpenRewrite works let’s dive into some real-world scenarios where it can come in handy:
- Dependency updates: A simple recipe can scan your project, identify outdated dependencies, and update to the latest versions, ensuring your project stays up-to-date. For example, consider migrating from Log4j 1.x to Log4j 2.x. The recipe will automatically update the version, and the package names.
- Security: By applying specific recipes, you can address vulnerabilities. For instance, if offers a Java security best practices recipe. It can also identify potential security issues, like keeping an AWS secret in plain text in the code.
- API deprecation: A tailored recipe can identify and replace deprecated calls, keeping your codebase aligned with the latest best practices. For example, it can migrate the deprecated
javax.xml.bindpackages tojakarta.xml.bind. - Technology migration: Whether migrating from JUnit asserts to AssertJ or adopting a new framework, recipes can guide you through the process and ensure a seamless transition. It can even be minor, like using the native Java
final varinstead of Lombok’svalfor Java 10+ and above.
Let’s see an example:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.13.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.migrate.jakarta.JavaxPersistenceToJakartaPersistence</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>This one will perform the following actions against the code:

You can run it with the mvn rewrite:run command.
Just review the changes made by OpenRewrite and commit the necessary code updates.
Exploring recipes without local testing
Exploring without a personal project to test the recipes on? No worries — OpenRewrite lets you experiment with publicly available projects. Take, for instance, the Spring pet-clinic project. Just hit the button:

You can perform a dry-run and see the results:


Writing your own recipes
As mentioned earlier, OpenRewrite is community-based, so anyone can contribute with a new recipe. The quickest way to get started is by using the rewrite-recipe-starter repository and clicking the “Use this template” button. Writing new recipes is out-of-scope of this tutorial, but you can find more information in the docs.
Conclusion
In this article, you learned how to use the OpenRewrite code refactoring plugin in Maven projects. You now know that you don’t have to manually perform monotonous tasks if you find the necessary recipe.
Not sure where to start? You can find some popular recipes here:

Personally, I’m a fan of this tool and wish I had discovered it sooner — it could have spared me some headaches! I trust this tutorial has proven beneficial for you as well.
Thanks for reading, and happy re-coding!






