Introducing Spring Boot Migrator

Spring Boot Migrator (SBM) is an experimental Spring project first released in March 2022. SBM allows developers to convert existing non-Spring Boot applications, based on technologies such as JAX-RS, EJB and JMS to Spring Boot or to upgrade older Spring applications. Start apps with the latest version.

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

After 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:>.

The list The command displays the thirty recipes currently available. Recipes, for example, 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. Here is an example of an 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 source code of the application, SBM displays the recipes corresponding to the preconditions. When no recipe matches, the list of applicable recipes will remain 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, the spring-boot-starter and spring-boot-starter-test dependencies and spring-boot-maven-plugin have been added and dependencyManagement now contains pom-like spring-boot dependencies. According to the original indentation of the pom.xml file, the indentation may also change to reflect the recipe.

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

@SpringBootApplication
public class SpringBootApp {

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

	@Test
	void contextLoads() {
	}

}

The application is now converted to 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 generates a new Git commit with the description: SBM: applied recipe ‘migrate-jax-rs’. Commit analysis shows that JAX-RS imports have been removed and replaced with Spring and JAX-RS imports @Path annotation on class file changed to Spring Boot @RestController and @RequestMapping. The various methods now have Spring Boot’s @RequestMapping, @RequestParam, @PathVariable instead of 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 kind ResponseEntity.

Running the Spring Boot application after migrations unfortunately failed because the maven-compiler-plugin defined in the pom.xml The file was using an older version of Java. Manually change the pom.xml using the currently installed Java version fixes the problem, but it’s also possible to automate the step with OpenRewrite’s Change Maven plugin configuration.

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

SBM currently supports Maven as OpenRewrite support for Gradle is not yet complete. You can find more information about SBM in the user documentation and developer documentation.

Comments are closed.