Spring Boot: Deployable WAR File

When building RESTful web services with Spring Boot, we have two options for deployment. We can package our Spring Boot app as an executable JAR file and run it with the built-in Tomcat, or we can create a deployable WAR file and then deploy it into a standalone Tomcat or Jetty. In this blog post, I will share how to modify your Spring Boot app so that it generates a deployable WAR file when built with Maven. You can then use this WAR file to deploy your application to a Tomcat or Jetty instance.

If you’re interested in learning how to download and install Tomcat and how to deploy your WAR file to a Tomcat instance running on your local machine or on a remote Linux server, check out my other video lessons on “RESTful Web Services with Spring Boot, Spring MVC, JPA, and MySQL“.

Creating a Deployable WAR File

1) Extend the SpringBootServletInitializer

To make your Spring Boot web app work as a deployable WAR file, you will need to:

  • Open the Java class that contains the public static void main(String[] args) method and make it extend the SpringBootServletInitializer class.
  • Call the application.sources(DeployableWarApplication.class) method, where DeployableWarApplication.class is the class that contains the public static void main(String[] args) method, as shown in the example below.
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 instead of jar.

<packaging>war</packaging>

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

The next step is to add the spring-boot-starter-tomcat dependency to the pom.xml file of your project. Here is an example of the dependency added to a pom.xml file:

Please note that the <scope> XML element of spring-boot-starter-tomcat has 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 that’s it! You should now be able to clean and build the project using the following commands.

First, clean the project:

maven clean

Now build your project to generate a deployable WAR file:

mvn install

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

I hope this short blog post has been helpful. Check out my other blog posts on Building RESTful Web Services with Spring Boot. Additionally, if you prefer to learn by watching step-by-step video lessons, take a look at the list of courses below. Perhaps one of them will help you take your skills to a whole new level.

Frequently asked questions

  • What is a WAR file?
    WAR stands for Web Application Archive. It’s a file format used to package and distribute a web application.
  • Why would I want to create a WAR file?
    WAR files can be easily deployed to a web server or a cloud platform like AWS or Google Cloud Platform. They simplify the process of deploying and managing web applications.
  • What is the SpringBootServletInitializer class?
    The SpringBootServletInitializer class is used to initialize a Spring Boot application when it’s deployed as a WAR file to a web server.
  • What is the spring-boot-starter-tomcat dependency?
    The spring-boot-starter-tomcat dependency is used to include the Tomcat web server in your Spring Boot application.

Leave a Reply

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