How to convert a Spring Boot JAR Application to a WAR


On this page, we will learn how to convert a Spring Boot JAR Application to a WAR. By default Spring Boot creates a JAR file. Yes, you heard right, Spring Boot application package everything into a Java ARchive (JAR) because the default packaging type is jar specified in pom.xml or build.gradle depends on which build tool used in the application.

Follow the below steps to convert JAR to WAR in Spring Boot application.

1. Change Packaging

As we mentioned above default packaging type is jar, we need to change to war“.

1.1 Maven

<groupId>org.websparrow</groupId>
<artifactId>springboot-jar-war</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>  <!-- Change this from jar to war -->
<name>springboot-jar-war</name>

1.2 Gradle >= 4.0

plugins {
	id 'org.springframework.boot' version '2.1.7.RELEASE'
	id 'io.spring.dependency-management' version '1.0.8.RELEASE'
	id 'java'
	id 'war' // Add id to tell Gradle for create WAR file
}

1.3 Gradle <= 4.0

apply plugin: 'java'
apply plugin: 'war' // Tell Gradle for create WAR file
group = 'org.websparrow'
version = '0.0.1-SNAPSHOT'

P.S Tested with Spring Boot 2.1.7.RELEASE, Maven 3, Gradle 5.6 and JDK 8.

2. Remove Embedded Tomcat

Yes, we have to remove the embedded Tomcat server dependency. If you are using spring-boot-starter-web dependency for MVC, REST, etc, Spring Boot automatically adds the spring-boot-starter-tomcat to the dependencies list.

In order to remove this application, we will have to mark the tomcat starter as provided so that it will not be shipped with the WAR file.

2.1 Maven

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

2.2 Gradle

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // Exclude embedded Tomcat Server
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3. Extends SpringBootServletInitializer

Initializing Spring in the web application context, Spring starter class must extend the SpringBootServletInitializer class and overrides it’s configure(SpringApplicationBuilder application) method. It binds Servlet, Filter and ServletContextInitializer beans from the application context to the server.

JarToWarApp.java
package org.websparrow;

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

@SpringBootApplication
public class JarToWarApp extends SpringBootServletInitializer {

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

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

Note: SpringBootServletInitializer is only needed if you are building a war file and deploying it. If you prefer to run an embedded web server then you won’t need this at all.

4. Build and Deploy

Now, Spring Boot application is almost ready to deploy as traditional WAR.

4.1 Maven

To build with using the Maven build tool, use the following command:

>mvn clean
>mvn package

After a successful build, you can find the .war file at the location <project-directory>/target/<project-name-version.war>

4.2 Gradle >= 4.0

>gradle clean
>gradle bootWar

4.3 Gradle <= 4.0

>gradle clean
>gradle build

Find the .war file at the location <project-directory> » build » libs » <project-name-version.war>

Now copy the WAR file and paste it to your external Tomcat server » webapps directory and start the server.

References

  1. Spring Boot JAR to WAR
  2. How to install Maven on Windows
  3. Maven- Packaging executable wars
  4. Gradle- Packaging executable wars

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.