Convert JAR to WAR in Spring Boot

Home / Convert JAR to WAR in Spring Boot

We know that when we create a Spring Boot project it will make a JAR file after compilation. But some developers prefers WAR over JAR for deploying their application on tomcat server. so for converting JAR into WAR in Spring Boot application we have to follow some rules and the rules are:

  1. Add spring-boot-starter-tomcat dependency into pom.xml file.
  2. change packaging to war instead of jar in pom.xml file.
  3. Go to your Spring Boot main class and extends SpringBootServletInitializer abstract class.
  4. Override the SpringApplicationBuilder method of
    SpringBootServletInitializer abstract class .
  5. Now just clean and build your project you will get your war file inside target folder of your project.

1- Add below dependency to pom.xml

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

2- Change packaging to war instead of jar in pom.xml file

<packaging>war</packaging> 

3- Go to main class and extends SpringBootServletInitializer abstract class.

@SpringBootApplication
public class SpringBootWarExampleApplication extends SpringBootServletInitializer{

4- Override the SpringApplicationBuilder method of
SpringBootServletInitializer abstract class

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
return app.sources(SpringBootWarExampleApplication.class);
}

Now Your Main Class will look like this

package com.vasu.SpringBootWarExample;
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 SpringBootWarExampleApplication extends SpringBootServletInitializer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder app) {
return app.sources(SpringBootWarExampleApplication.class);
}

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

5- Now just clean and build your project you will get your war file inside target folder of your project.

You may also like

Change Default Banner Spring Boot

How to read property values in spring boot application

How to use H2 database in spring boot application

Spring Boot With MySql

Profiles in Spring boot application

spring batch example

Help Others, Please Share

About Author

Leave a Reply

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