Introducing Spring Boot Migrator

Spring Boot Migrator (SBM) is an experimental Spring project that was first released in March 2022. SBM allows developers to do existing ones spring boots Migrate applications built on technologies such as JAX-RS, EJB, and JMS to Spring Boot or upgrade older Spring Boot applications to the latest version.

SBM is based on OpenRewrite, a general purpose tool for source code and configuration refactoring. OpenRewrite used recipes to modify existing files for Kubernetes, Gradle, Maven, Java and others. For example, the recipes allow an existing application to be upgraded to a newer Java version. SBM uses OpenRewrite specifically for Spring Boot migrations.

To Downloading The latest version of SBM, the command line interface (CLI) can be started with:

java -jar spring-boot-migrator.jar

After a few seconds, the user is presented with an SBM-specific prompt: migrator:>.

That list The command displays the thirty recipes currently available. For example, the recipes support upgrading Spring Boot to a new version, changing XML bean configuration to Java configuration, and migrating various Java EE/Jakarta implementations to Spring Boot.

With the scan [directory] command, an application is analyzed and the applicable recipes are displayed. The following shows an example, older JAX-RS Application without Spring Boot support analyzed by SBM showing the following results:

scanning 'JAXRS'

Checked preconditions for '.../JAXRS'
[ok] Found pom.xml.
[ok] 'sbm.gitSupportEnabled' is 'true', changes will be committed to branch [master] after each recipe.
[ok] Required Java version (17) was found.
[ok] Found required source dir 'src/main/java'.

Maven    	100% │███████████████████████████│ 2/2 (0:00:09 / 0:00:00)

Applicable recipes:

	= 'automated recipe'
  = 'partially automated recipe'
	= 'manual recipe'

  - initialize-spring-boot-migration []
 	-> Initialize an application as Spring Boot application.
  - migrate-jax-rs []
 	-> Any class has import starting with javax.ws.rs
  - cn-spring-cloud-config-server []
 	-> Externalize properties to Spring Cloud Config Server

Based on the application’s source code, SBM displays the recipes that meet the requirements. If no recipe matches, the list of matching recipes remains empty. In this case, one of the recipes listed above can be applied, for example to convert the existing code into a Spring Boot application:

migrator:> apply initialize-spring-boot-migration

This results in a new git commit with the description SBM: Applied Recipe ‘Initialize-Spring-Boot-Migration’.

Analysis of the commit shows the following changes in the pom.xml: Packaging changed from WAR to JAR which spring-boot-starter and spring-boot-starter-test dependencies and the spring-boot-maven-plugin were added and the dependencyManagement Section now includes the spring boot dependencies of type pom. Depending on the original indentation of the pom.xml File may also change indentation to reflect recipe.

The source code has also been changed and now contains a SpringBootApp.java and a SpringBootAppTest.java:

@SpringBootApplication
public class SpringBootApp {

	public static void main(String[] args) {
    	SpringApplication.run(SpringBootApp.class, args);
	}
}
@SpringBootTest
class SpringBootAppTest {

	@Test
	void contextLoads() {
	}

}

Now the application is transformed into a Spring Boot application. The next step is to migrate the JAX-RS source code to Spring Boot with the command:

migrator:> apply migrate-jax-rs

The command results in a new git commit with the description: SBM: applied recipe ‘migrate-jax-rs’. Analysis of the commit shows that the JAX-RS imports have been removed and replaced with Spring imports and JAX-RS @Path Replaced the annotation in the class file with Spring Boot @RestController and @RequestMapping. The various methods now have Spring Boot’s @RequestMapping, @RequestParam, @PathVariable instead of the JAX-RS annotations such as @Get, @Post, @Path, @Produces, @QueryParam and @PathParam. Finally, the return type of the methods is no longer of type Responsebut of the type ResponseEntity.

Unfortunately, running the Spring Boot application after the migrations failed because the maven-compiler-plugin defined in the pom.xml File used an older version of Java. Manually changing the pom.xml Using the currently installed version of Java fixes the issue, but it’s also possible to automate the step with the Change Maven plugin configuration by OpenRewrite.

Analysis of the resulting code and configuration revealed some dependencies that were no longer needed by Spring Boot. The outdated dependencies can be removed manually or this step can also be automated with SBM or OpenRewrite.

SBM currently supports Maven due to the lack of OpenRewrite support for Gradle Completely. For more information on SBM, see the user documentation and developer documentation.

Comments are closed.