Convert JAR to WAR in Spring Boot
March 3, 2019 | Spring boot complete tutorial with example | No Comments
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:
- Add spring-boot-starter-tomcat dependency into pom.xml file.
- change packaging to war instead of jar in pom.xml file.
- Go to your Spring Boot main class and extends SpringBootServletInitializer abstract class.
- Override the SpringApplicationBuilder method of
SpringBootServletInitializer abstract class . - 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
Profiles in Spring boot application