OpenRewrite: Migrate to Spring Boot 3.2

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:

Enabling Virtual Threads
Support for RestClient, JdbcClient, Apache Pulsar (alternative to Kafka), and Jetty12
Observability Enhancements
Initial Support for JVM Coordinated Restore at Checkpoint (CRaC)

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.

Migrate to Spring Boot 3.1
Update the versions of Maven or Gradle dependencies
Update the version of the Maven parent project
Update a Gradle plugin based on its id
Migrating to Spring Security 6.2, Spring Boot Properties to 3.2, and Spring Cloud 2023
Enabling Virtual Threads on Java21
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/

The post OpenRewrite: Migrate to Spring Boot 3.2 appeared first on foojay.