Skip to main content

Convert JAR to WAR in Spring Boot

convert jar to war in spring boot

In this article we are going to discuss more about how to covert JAR to WAR in spring boot.
Before going in more details, first of all need to understand that spring boot default packing type is JAR. Therefore by default Spring boot packages application into JAR.
Suppose we want to create war file in place of jar in Spring Boot. So that we can deploy it on standalone server.
Yes, it is very simple in Spring Boot by performing some small changes.
Generated war can deploy on standalone tomcat and also run as a executable war file using java -jar command.
We need 3 steps for convert JAR to WAR in Spring Boot:

  1. Change packaging to war
  2. Marked the embedded tomcat as provided.
  3. Extends SpringBootServletInitializer and override the configure() method.

1- Change packing to war

Spring Boot default packaging type is JAR so first of all need to change packing to WAR.

pom.xml

<groupId>in.tecmentor</groupId>
<artifactId>spring-boot-war-demo</artifactId>
<version>1</version>
<packaging>war</packaging>        <!-- Change this from jar to war -->
<name>spring-boot-war-demo</name>

2- Marked the embedded tomcat as provided

If we are using spring-boot-starter-web dependency then Spring Boot automatically add the spring-boot-starter-tomcat dependencies.
In order to create war that can deploy on standalone tomcat, need to remove embedded tomcat dependency.
To remove embedded spring-boot-starter-tomcat dependency, mark as provided. As a result it will not be shipped with the WAR file.

pom.xml

<dependency>
 <groupid>org.springframework.boot</groupid>
 <artifactid>spring-boot-starter-tomcat</artifactid>
 <scope>provided</scope>
</dependency>

3- Extends SpringBootServletInitializer

Modify the @SpringBootApplication class to extends the SpringBootServletInitializer and override the configure() method.

SpringBootWarDemoApplication.java

package in.tecmentor;

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 SpringBootWarDemoApplication extends SpringBootServletInitializer {

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

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(SpringBootWarDemoApplication.class);
	}
}

Build and Deploy

Finally all required changes done. Now it’s time to build the application. As a result we will get .war file.
To build the application using the Maven build tool, use the following given below command:

>mvn clean
>mvn package

After project successful build, copy the WAR file for deployment.


Invoke the application

Once the application deployed on stand alone server, it’s time to invoke the exposed end points for verification purpose.
In order to invoke the end point, open the postman or browser, enter the URL and submit the request.

test convert jar to war in spring boot

Download source code

Download Source Code: spring-boot-war-demo.zip

Leave a Reply

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

Top