This tutorial is going to cover how to package a Spring Boot application into WAR file so that we can deploy the application in any application server such as Tomcat, JBoss, Web Sphere and so on.

1. Package a Spring Boot Application into War File

To package a Spring Boot application into WAR file or  to convert a Spring Boot JAR application to a WAR, we will need to change two things:

  • Modify the build file (pom.xml or build.gradle) to instruct the build tool such as Maven or Gradle to create deployable war file.
  • Modify the main application to extend from the SpringBootServletInitializer abstract class in order to make use of Spring Framework’s Servlet 3.0 support and allows us to configure the application when it’s launched by the servlet container.

1.1. Modify the build file

1.1.1. The pom.xml File (for Maven based project)

We will need to modify the pom.xml file to

  • Change the <packaging> tag from jar to war as the following. If there is no packaging tag, we will need to place one.
  • Add the spring-boot-starter-tomcat dependency to our pom.xml and set the scope to provided

1.1.2. The build.gradle File (for Gradle based project)

We will need to modify the build.gradle file to

  • Apply the war plugin to the project
  • Add the spring-boot-starter-tomcat dependency

Let’s see the following example:

1.2. Modify the main application to extend from the SpringBootServletInitializer abstract class

Typically, we will need to update our application’s main class to extend SpringBootServletInitializer as the following example:

2. Package the Application

2.1. Maven based project

For Maven based project, we can issue the following command to package a Spring Boot application into WAR file:

or with Maven Wrapper

2.1. Gradle based project

For Gradle based project, we can issue the following command to package a Spring Boot application into WAR file:

or with Gradle Wrapper

After the packaging process finishes, we can deploy the war file into any container.

3. Conclusion

The tutorial has just illustrated how to package a Spring Boot application into war file for both Maven based project and Gradle based project. This tutorial can be used to convert a Spring Boot JAR application to a WAR which then can be deployed to any application server.

Here are other related tutorials for your references:

Spring Boot – How To Set Active Profiles

How to Configure Logging in Spring Boot

How To Run A Spring Boot Application

Install Spring Boot Command Line Interface on Linux

JUnit 5 and Spring Boot Example

0 0 vote
Article Rating