Upgrade to Spring Boot 4.0
To upgrade your project from Spring Boot 3.x to Spring Boot 4.0, you can use the OpenRewrite recipe org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0 from rewrite-spring.
This recipe will automatically update your Spring Boot dependencies to version 4.0 and apply the necessary code changes to ensure compatibility, including updating deprecated APIs, migrating configuration properties, and adapting to framework changes introduced in Spring Boot 4.0.
You can run the migration recipe using one of the following methods.
- Moderne CLI
- Maven Command Line
- Maven POM
- Gradle init script
- Gradle
The Moderne CLI allows you to run OpenRewrite recipes on your project without needing to modify your build files, against serialized Lossless Semantic Tree (LST) of your project for a considerable performance boost & across projects.
You will need to have configured the Moderne CLI on your machine before you can run the following command.
- If project serialized Lossless Semantic Tree is not yet available locally, then build the LST. This is only needed the first time, or after extensive changes:
mod build ~/workspace/
- If the recipe is not available locally yet, then you can install it once using:
mod config recipes jar install org.openrewrite.recipe:rewrite-spring:LATEST
- Run the recipe.
mod run ~/workspace/ --recipe org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0 -Drewrite.exportDatatables=true
You may add the plugin to your pom.xml file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
pom.xmlfile:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>LATEST</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-spring</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- Run the recipe.
mvn rewrite:run
Gradle init scripts are a good way to try out a recipe without modifying your build.gradle file.
- Create a file named
init.gradlein the root of your project.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:latest.release") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-spring:latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0")
setExportDatatables(true)
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
- Run the recipe.
gradle --init-script init.gradle rewriteRun
You can add the plugin to your build.gradle file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
build.gradlefile:
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.spring.boot4.UpgradeSpringBoot_4_0")
setExportDatatables(true)
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-spring:latest.release")
}
- Run
gradle rewriteRunto run the recipe.
Next steps
Customize the Spring Boot 4.0 upgrade recipe to fit your project's specific needs by extending it with additional transformations or configurations as required. This can be to bring along internal libraries or frameworks as you upgrade, or to expand the recipes beyond what is covered out of the box. As always: if you develop any recipes useful to others, consider contributing back to the community!