Unleash coding transformations with OpenRewrite—a versatile tool for seamless code upgrades and refactoring. Your codebase evolution, your control. 🚀💻 #OpenRewrite

Transformative Code Evolution with OpenRewrite: Upgrading Spring Boot, JUnit, and Java

OpenRewrite Jan 9, 2024

Introduction to OpenRewrite

OpenRewrite is a powerful tool for large-scale automated source code refactoring. Whether you're dealing with framework migrations, vulnerability patches, or API migrations, OpenRewrite can streamline the process. Initially focused on the Java language, OpenRewrite has evolved into a semantic code search and transformation ecosystem for Java and other source code.

What Does OpenRewrite Do?

OpenRewrite operates by making changes to the abstract syntax tree (AST) representing your source code and then printing the modified trees back into source code. This allows you to review the changes before committing them. The modifications to the AST are performed through visitors that are aggregated into recipes. OpenRewrite comes with a platform of prepackaged refactoring recipes for common tasks related to framework migration and stylistic consistency. Additionally, you can define custom recipes to achieve a wide range of source code transformations.

Key Features of OpenRewrite

  • Language Upgrades: Migrate from older versions of a language to newer versions (e.g., Java 8 to Java 11).
  • Framework Upgrades: Adapt to newer versions of frameworks such as Spring Boot, Hibernate, etc.
  • Dependency Migration: Upgrade from one library version to another, handling breaking changes.
  • Security Patching: Replace vulnerable methods or libraries with secure alternatives.
  • Custom Transformations: Define transformations specific to your business logic or infrastructure.

Getting Started with OpenRewrite

OpenRewrite Recipes

The main feature of OpenRewrite is its ability to automatically refactor source code by applying recipes to the project. OpenRewrite includes a variety of built-in recipes for common transformations, and the community often contributes recipes for widespread tasks. Each recipe is written in Java code and included in the build process using the OpenRewrite Maven or Gradle plugin.

Maven Dependency

To use OpenRewrite with Maven, add the rewrite-maven-plugin plugin to your pom.xml:

<plugins>
    <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.18.0</version>
        <configuration>
            <activeRecipes>
                <!-- Define the recipes -->
            </activeRecipes>
        </configuration>
        <dependencies>
            <!-- Dependencies for recipes -->
        </dependencies>
    </plugin>
</plugins>

The <activeRecipes> section specifies which OpenRewrite recipes should be active during plugin execution. Additional dependencies can be defined as needed.

2.3. Checkout Spring PetClinic

Before diving into examples, let's clone the relevant branch of the Spring PetClinic locally:

git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
git switch -c 2.5.x 1.5.x

3. Upgrading Spring Boot

Upgrading a Spring Boot project, especially from one major version to another, can be complex. OpenRewrite simplifies this process. Let's upgrade the PetClinic project from Spring Boot 1.5 to 2.7.

3.1. Maven Dependency

Add the UpgradeSpringBoot_2_7 recipe to the <activeRecipes> section:

<activeRecipes>
    <recipe>org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7</recipe>
</activeRecipes>

Also, declare the rewrite-spring dependency:

<dependency>
    <groupId>org.openrewrite.recipe</groupId>
    <artifactId>rewrite-spring</artifactId>
    <version>5.0.11</version>
</dependency>

3.2. Run Spring Boot Upgrade Recipe

Execute the migration:

mvn rewrite:run

Witness Maven in action as it lists active recipes, applies changes to the pom.xml and source files, and seamlessly upgrades Spring Boot versions.

4. Upgrading to JUnit 5

JUnit, a Java testing standard, transitions from version 4 to 5 with OpenRewrite support.

4.1. Maven Dependency

Activate the JUnit5BestPractices recipe in the pom.xml:

<activeRecipes>
    <recipe>org.openrewrite.java.testing.junit5.JUnit5BestPractices</recipe>
</activeRecipes>

Include the rewrite-testing-frameworks dependency:

<dependency>
    <groupId>org.openrewrite.recipe</groupId>
    <artifactId>rewrite-testing-frameworks</artifactId>
    <version>2.0.12</version>
</dependency>

4.2. Run JUnit Upgrade Recipe

Run the migration:

mvn rewrite:run

Observe the transformation as JUnit 4 artifacts gracefully evolve into JUnit 5 sophistication. OpenRewrite excels in updating annotations, modifying assertion method calls, and seamlessly adapting your tests.

5. Upgrading to Java 11

Upgrade your source code from Java 8 to Java 11 using OpenRewrite.

5.1. Maven Dependency

Add the Java8toJava11 recipe to the <activeRecipes> section:

<activeRecipes>
    <recipe>org.openrewrite.java.migrate.Java8toJava11</recipe>
</activeRecipes>

Also, declare the rewrite-migrate-java dependency:

<dependency>
    <groupId>org.openrewrite.recipe</groupId>
    <artifactId>rewrite-migrate-java</artifactId>
    <version>2.1.1</version>
</dependency>

5.2. Run Java Upgrade Recipe

Initiate the Java upgrade:

mvn rewrite:run

Watch in awe as your project transitions from Java 8 to Java 11. The Java8toJava11 recipe leaves its mark, upgrading the java.version property in your pom.xml and ushering in the era of Java 11 features.

Conclusion

In this expedition through the OpenRewrite universe, we've witnessed its transformative capabilities. Seamlessly upgrading Spring Boot, transitioning to JUnit 5, and embracing the power of Java 11 – OpenRewrite has proven its mettle. Armed with this knowledge, navigate your code evolution journey with confidence and precision.

Explore OpenRewrite Documentation 🚀💻

For questions, feedback, or to contribute, join the OpenRewrite community on GitHub. Your contributions are not just valued; they are instrumental in shaping the future of code transformation for developers worldwide.

Tags