Saturday, April 20, 2024
HomeJavaIntroducing Spring Boot Migrator

Introducing Spring Boot Migrator


Spring Boot Migrator (SBM) is an experimental Spring venture first launched in March 2022. SBM permits builders to transform present, non Spring Boot functions, that are primarily based on applied sciences comparable to JAX-RS, EJB and JMS to Spring Boot or improve older Spring Boot functions to the most recent model.

SBM relies on OpenRewrite, a common objective instrument for supply code and configuration refactoring. OpenRewrite makes use of Recipes to alter present recordsdata for Kubernetes, Gradle, Maven, Java and others. The recipes enable, for instance, to improve an present software to a more recent model of Java. SBM makes use of OpenRewrite particularly for Spring Boot migrations.

After downloading the most recent model of SBM, the command-line interface (CLI) will be began with:


java -jar spring-boot-migrator.jar

After a number of seconds, the consumer is offered with an SBM-specific immediate: migrator:>.

The listing command shows the thirty recipes at present out there. The recipes, for instance, help upgrading Spring Boot to a brand new model, change XML Bean configuration to Java configuration and migrate numerous Java EE / Jakarta implementations to Spring Boot.

With the scan [directory] command, an software is analyzed and the relevant recipes are displayed. The next exhibits an instance, older JAX-RS software with out Spring Boot help analyzed by SBM, displaying the next outcomes:


scanning 'JAXRS'

Checked preconditions for '.../JAXRS'
[ok] Discovered pom.xml.
[ok] 'sbm.gitSupportEnabled' is 'true', adjustments might be dedicated to department [master] after every recipe.
[ok] Required Java model (17) was discovered.
[ok] Discovered required supply dir 'src/principal/java'.

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

Relevant recipes:

	= 'automated recipe'
  = 'partially automated recipe'
	= 'handbook recipe'

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

Primarily based on the supply code of the appliance, SBM shows the recipes matching the preconditions. When no recipe matches, the listing of relevant recipes will stay empty. On this case, one of many recipes listed above could also be utilized, for instance to transform the present code to a Spring Boot software:


migrator:> apply initialize-spring-boot-migration

This ends in a brand new Git commit with the outline SBM: utilized recipe ‘initialize-spring-boot-migration’.

Analyzing the commit exhibits the next adjustments within the pom.xml: the packaging was modified from WAR to JAR, the spring-boot-starter and spring-boot-starter-test dependencies and the spring-boot-maven-plugin have been added and the dependencyManagement part now comprises the spring-boot-dependencies of kind pom. Relying on the unique indentation of the pom.xml file, the indentation might change as nicely to mirror the recipe.

The supply code was altered as nicely and now comprises a SpringBootApp.java and a SpringBootAppTest.java:


@SpringBootApplication
public class SpringBootApp {

	public static void principal(String[] args) {
    	SpringApplication.run(SpringBootApp.class, args);
	}
}

@SpringBootTest
class SpringBootAppTest {

	@Take a look at
	void contextLoads() {
	}

}

Now the appliance is transformed to a Spring Boot software. The subsequent step is emigrate the JAX-RS supply code to Spring Boot with the command:


migrator:> apply migrate-jax-rs

The command ends in a brand new Git commit with the outline: SBM: utilized recipe ‘migrate-jax-rs’. Analyzing the commit exhibits the JAX-RS imports have been eliminated and changed with Spring imports and the JAX-RS @Path annotation on the category file was changed with the Spring Boot @RestController and @RequestMapping. The assorted strategies now have Spring Boot’s @RequestMapping, @RequestParam, @PathVariable as an alternative of the JAX-RS annotations comparable to @Get, @Put up, @Path, @Produces, @QueryParam and @PathParam. Lastly the return kind of the strategies is not of kind Response, however of kind ResponseEntity.

Operating the Spring Boot software after the migrations sadly failed because the maven-compiler-plugin outlined within the pom.xml file used an older model of Java. Manually altering the pom.xml to make use of the at present put in Java model fixes the issue, nevertheless it’s additionally attainable to automate the step with the Change Maven plugin configuration of OpenRewrite.

Analyzing the ensuing code and configuration confirmed some dependencies which have been not wanted by Spring Boot. The out of date dependencies could also be eliminated manually or this step could also be automated as nicely with SBM or OpenRewrite.

SBM at present helps Maven as OpenRewrite’s help for Gradle just isn’t but full. Extra info on SBM will be discovered within the Person Documentation and Developer Documentation.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments