SATURN  RING  STATION

At a distant space view post, we chat about codes.

Converting a Spring Boot JAR Application to a WAR extended tutorial

Version: Spring Boot 1.3.5

First, let your spring app main class extends  SpringBootServletInitializer. Override it’s configure method as below.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

@SpringBootApplication
public class YourAppNameApplication extends SpringBootServletInitializer{

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
		return application.sources(YourAppNameApplication.class);
	}

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

 


Second, go to your pom.xml maven file. Change the content in the tag named packaging from jar to war.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

</project>

One more thing to change in pom.xml. If you’re following the official tutorial on Spring Boot website, you found out that embedded tomcat artifact spring-boot-starter-tomcat is no longer list in the pom.xml file individually. It’s now shipped with the spring-boot-starter-web. My quick workaround is excluding the spring-boot-starter-tomcat within the spring-boot-starter-web package. Put it back as an individual dependency; give a new scope tag as provided.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-tomcat</artifactId>
			</exclusion>
		</exclusions>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-tomcat</artifactId>
	<scope>provided</scope>
</dependency>

 


Now the war file is ready to be built. Do it the way you like. I provide the maven command line way here. Open your shell and navigate to your app’s root directory, where pom.xml lives.

$ mvn clean install

If it shows BUILD SUCCESS, find your war file in the target folder.

Peace.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Converting a Spring Boot JAR Application to a WAR extended tutorial