Create a Deployable WAR File with Spring Boot

When building RESTful Web Services with Spring Boot we can either package our Spring Boot App as an executable JAR file and run it with built-in Tomcat or we can create a deployable WAR file and then deploy it into a stand along Tomcat or Jetty. With this blog post, I am going to share with you how to edit your Spring Boot app, so that when built with Maven it generates a deployable WAR file.

1) Extend the SpringBootServletInitializer

To make your Spring Boot Web App work as a deployable WAR file we will need to:

  • Open Java class that contains the public static void main(String[] args) and make it extend the SpringBootServletInitializer class
  • Call the application.sources(DeployableWarApplication.class) method as in the example below. Where the DeployableWarApplication.class is the class which contains the public static void main(String[] args) method.
package com.appsdeveloperblog.DeployableWar;

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

 public static void main(String[] args) {
  SpringApplication.run(DeployableWarApplication.class, args);
 }
 
 @Override
 protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  return application.sources(DeployableWarApplication.class);
 }

}

2) Set Project Packaging to a WAR

Open the POM.xml file of your project and update the <packaging> XML element to have the value of WAR rather than JAR.

<packaging>war</packaging>

3) Add spring-boot-starter-tomcat to POM.XML

Next step is to add a new spring-boot-started-tomcat dependency to a pom.xml file of your project. Have a look at the example dependencies from my pom.xml file.

Please note that the <scope> XML element of spring-boot-starter-tomcat contains the value provided.

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

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

And this is it! Now you should be able to clean and build the project with the below commands.

First, clean the project:

maven clean

and now build your project to generate a deployable WAR file:

mvn install

after the successful build of your project check the target/ folder. It should now contain the deployable WAR file.

I hope this short blog post is of some value to you. Check out the other blog posts I have on Building RESTful Web Services with Spring Boot. Also, if you prefer to learn by watching a step by step video lessons, check the below list of video courses and may be one of them will help you take your skills to a whole new level.