Codementor Events

Java | Spring Boot - Library Upgrades Modernization

Published Apr 14, 2024

Have you ever wondered why build systems like Gradle and Maven exist? You might say that their purpose is to automate dependency pulling and create required artifacts. Others may use them to maintain good coding standards and testing coverage, breaking the build and pulling necessary reports. Those involved with enterprise applications may also use security scans on their dependencies.

All of these are valid use cases for building systems, but have you ever considered what to do when you have multiple libraries to update manually? It's up to us to choose the optimized, non-vulnerable, and up-to-date versions of these libraries. But who will do the tedious job of upgrading, especially when there are numerous security vulnerabilities in the project? Who will make code changes and ensure that deprecated methods or features are refactored to the latest changes supported by a library? Fortunately, the community is here to support us.

Allow me to introduce you to OpenRewrite by Moderne. OpenRewrite is an automated refactoring ecosystem for source code that helps developers eliminate technical debt within their repositories. It includes an auto-refactoring engine that runs pre-packaged, open-source refactoring recipes for common framework migrations, security fixes, and stylistic consistency tasks. This reduces coding effort from hours or days to minutes. Build tool plugins such as the OpenRewrite Gradle plugin and the OpenRewrite Maven plugin to help you run these recipes on one repository at a time.

Although OpenRewrite's original focus was on the Java language, the community is continuously expanding language and framework coverage. Thousands of individuals and teams are working together to make software seamless to update and continuously secure.

In this article, we will focus on Spring Boot, a widely accepted framework in Java to build Microservices. Specifically, we will address the use case of upgrading Spring Boot 2.x.x to Spring Boot 3.

There are three ways to use OpenRewrite recipes: Maven, Gradle, and Moderne CLI. Recipes are configurations that OpenRewrite can understand. We don't need to worry about creating a recipe for now as we will focus on using the existing recipes available in the community.

Out of two different ways to use the recipe, we will focus on the easiest one for the naive user to start with.

  1. Create a file named init.gradle in the root of your project.
  2. Paste the below content in the init.gradle
initscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2" }
    }
    dependencies { classpath("org.openrewrite:plugin:6.11.2") }
}
rootProject {
    plugins.apply(org.openrewrite.gradle.RewritePlugin)
    dependencies {
        rewrite("org.openrewrite.recipe:rewrite-spring:5.7.0")
    }
    rewrite {
        activeRecipe("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0")
    }
    afterEvaluate {
        if (repositories.isEmpty()) {
            repositories {
                mavenCentral()
            }
        }
    }
}

Below is the line that helps to identify the recipe which makes the magic of refactoring. There are so many different recipes available in the Open Rewrite catalog. You can explore and try it out yourself.

activeRecipe("org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_0")
  1. Run below gradle command to run the recipe.
gradle --init-script init.gradle rewriteRun

Great! You will witness some magic as your files are automatically refactored. However, please note that this will only happen if there are no errors related to your Gradle and Java Path. If you encounter any specific errors, don't hesitate to reach out to me for assistance!

You may notice that when you upgrade to a new version of Spring Boot, all the compatible and related libraries you're using, such as Kafka, Elastic Search, Redis, and others, will also be upgraded to the compatible version of Spring Boot. This saves you the trouble of checking compatibility metrics from the Spring Boot upgrade documentation. Additionally, many class files will be refactored automatically with the code changes. Have you considered updating your yml files to ensure compatibility? If not, you may see some yml or property files that have also been updated to conform to standard practices suggested in the library documentation. It's all done out of the box, which is pretty amazing.

I really hope that this article can assist you in optimizing those long upgrade processes that can be quite a headache.

Reference: OpenRewrite | Migrate to Spring Boot 3.0 | Recipe Catalog

Discover and read more posts from Malay
get started
post commentsBe the first to share your opinion
Show more replies