avatarKirshi Yin

Summary

OpenRewrite is an automated code refactoring tool that simplifies project updates and maintenance through the use of community-contributed recipes.

Abstract

OpenRewrite is a versatile refactoring toolkit designed to streamline code maintenance by applying user-created "recipes" to automate tasks such as dependency updates, security patches, API deprecations, and technology migrations. It integrates with Maven projects and is community-driven, allowing developers to contribute new recipes for shared problems. The tool supports dry-runs on public projects for testing and offers extensive documentation for writing custom recipes. The article emphasizes the ease of use and potential for reducing manual refactoring efforts, positioning OpenRewrite as a valuable asset for developers looking to keep their codebases up-to-date and secure.

Opinions

  • The author expresses that OpenRewrite can alleviate the tedious aspects of code maintenance by automating updates and refactoring.
  • OpenRewrite's community-driven nature is highlighted as a strength, with the potential for collective problem-solving through recipe contributions.
  • The author personally endorses OpenRewrite, suggesting it could have saved them from past refactoring headaches.
  • The article suggests that OpenRewrite is not only a practical tool but also a cost-effective solution compared to other AI services.
  • The author recommends OpenRewrite as a beneficial tool for readers, indicating confidence in its ability to improve the development process.

OpenRewrite: Easy Code Refactoring for Seamless Development

Turning project updates into a breeze: A guide to effortless refactoring with OpenRewrite

Photo by Aditya Wardhana on Unsplash

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:

OpenRewrite usage

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.bind packages to jakarta.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 var instead of Lombok’s val for 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:

Recipe definition

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:

Running OpenRewrite with ModerneSaaS on https://docs.openrewrite.org/

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

Dry run results
Results in the petclinic repository

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:

Popular recipes from https://docs.openrewrite.org/running-recipes/popular-recipe-guides/

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!

Technology
Coding
Software Development
Programming
Software Engineering
Recommended from ReadMedium