Friends of OpenJDK Today

OpenRewrite: Migrate to Spring Boot 3.2

February 06, 2024

Author(s)

  • Avatar photo
    Mahendra Rao B

    Senior Technical Architect hailing from India with over 17 years of experience in the field. I hold a degree in Computer Science and specialize in Java, Spring, Microservices, and AWS ... Learn more

As a developer, we frequently face the challenges of migrating to newer versions of frameworks and refactoring code. However, we can effortlessly achieve these tasks with the assistance of OpenRewrite. OpenRewrite provides a stack of recipes specifically designed for migration purposes. By utilizing the appropriate recipes and integrating with the rewrite plugin, we can effectively migrate our code.

Two months ago, the Spring Boot 3.2 version released, bringing numerous changes to the framework. If you are involved in multiple projects and require migrating to the latest version, developers may find manually updating all the code repositories with the necessary upgrade changes a challenging task.

I successfully utilized UpgradeSpringBoot_3_2 Recipes to transfer all of my personal projects to the latest version of Spring Boot, 3.2.x. This migration significantly reduced the time required for the process.

Notable releases in this version include:

We can effortlessly transfer dependencies, modify build files, remove or alter them, update changes to deprecated or preferred APIs, and migrate configuration settings from the previous version of the application using OpenRewrite recipes. The Spring Boot 3.2 recipe also applies to this.

This article will focus on the recipes that we will utilize for migrating to Spring Boot 3.2.x with the following recipes:

  • org.openrewrite.java.migrate.UpgradeToJava21
  • org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2

The UpgradeSpringBoot_3_2 recipe combines multiple recipes that fulfill our requirements. Furthermore, we will also activate and execute the following recipes.

  1. Migrate to Spring Boot 3.1
  2. Update the versions of Maven or Gradle dependencies
  3. Update the version of the Maven parent project
  4. Update a Gradle plugin based on its id
  5. Migrating to Spring Security 6.2, Spring Boot Properties to 3.2, and Spring Cloud 2023
  6. Enabling Virtual Threads on Java21
  7. Switching to a more up-to-date qualifiedTypeName, replacing the older version

Usage of Recipes

I have used it for Maven projects, and I have employed this approach to accomplish it.

1. First add the following plugin to the pom.xml

<plugin>
 <groupId>org.openrewrite.maven</groupId>
 <artifactId>rewrite-maven-plugin</artifactId>
 <version>5.21.0</version>
 <configuration>
  <activeRecipes> ... </activeRecipes>
 </configuration>
</plugin>

2. Under the activeRecipes tag, please add the following UpgradeSpringBoot_3_2 recipe

<recipe>org.openrewrite.java.migrate.UpgradeToJava21</recipe>
<recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2</recipe>

3. And the above recipes can be activated by adding the following dependency

<dependencies>
<dependency>
  <groupId>org.openrewrite.recipe</groupId>
  <artifactId>rewrite-migrate-java</artifactId>
  <version>2.7.1</version>
 </dependency>
 <dependency>
  <groupId>org.openrewrite.recipe</groupId>
  <artifactId>rewrite-spring</artifactId>
  <version>5.3.0</version>
 </dependency>
</dependencies>

4. Finally configuration would be

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.openrewrite.maven</groupId>
        <artifactId>rewrite-maven-plugin</artifactId>
        <version>5.21.0</version>
        <configuration>
          <activeRecipes>
            <recipe>org.openrewrite.java.migrate.UpgradeToJava21</recipe>
            <recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2</recipe>
          </activeRecipes>
        </configuration>
        <dependencies>
         <dependency>
	     <groupId>org.openrewrite.recipe</groupId>
	     <artifactId>rewrite-migrate-java</artifactId>
	     <version>2.7.1</version>
	  </dependency>
          <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-spring</artifactId>
            <version>5.3.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

5. Perform the dryRun

Performing a rewrite:dryRun and verifying the generated file rewrite.patch under the target/rewrite folder is considered the recommended approach.

mvn rewrite:dryRun

https://github.com/bsmahi/migratespring/blob/master/target/rewrite/rewrite.patch

If you observe rewrite.patch file you can observe the difference in the each of the class and properties file and go through each one of them skeptically. Mainly

  • Changed Java version to 21 and Spring Boot Parent to 3.2.2 (latest)
  • Enabled VirtualThreads by adding the property: spring.threads.virtual.enabled=true
  • In SecurityConfig changed from fluent api to LambdaDSL (functional programming)
  • RequestMapping annotation has been simplified
  • Changed javax namespace to jakarta

6. Run the recipe using the below command

After gaining confidence in using the dryRun feature, you should proceed to execute the rewrite:run command

mvn rewrite:run

Conclusion

In summary, OpenRewrite recipes will reduce the time required for framework migration. Ultimately, numerous enterprise organizations need to consider cost optimization as a crucial factor.

As usual, the complete code available over on Github

Reference

https://docs.openrewrite.org/

Promoted Content

Step up your coding with the Continuous Feedback Udemy Course: Additional coupons are available

What do you know about the code changes that were just introduced into the codebase? When will you notice if something goes wrong?

Get Started Here!

Related Articles

View All

Author(s)

  • Avatar photo
    Mahendra Rao B

    Senior Technical Architect hailing from India with over 17 years of experience in the field. I hold a degree in Computer Science and specialize in Java, Spring, Microservices, and AWS ... Learn more

Comments (2)

Your email address will not be published. Required fields are marked *

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

Save my name, email, and website in this browser for the next time I comment.

Java Weekly, Issue 528 | Baeldung

[…] >> OpenRewrite: Migrate to Spring Boot 3.2 [foojay.io] […]

OpenRewrite :: สำหรับการ migrate ไปยัง Spring Boot 3.2

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard