Spring Boot war Packaging Example

In Spring boot applications, the default packaging is jar which the application is deployed in embedded servers. If you want to generate a war file for deployment in separate application server instances such as Jboss, Weblogic or Tomcat, follow the instructions below.

1. Declare packaging type ‘war’

The first logical step is to declare the packaging type ‘war’ in pom.xml file.


It indicates the project’s artifact type. Please note that when no packaging is declared, Maven assumes the artifact is the default: jar.

2. Set Embedded Server Dependency Scope to ‘provided

We may want to have an embedded server (e.g. Tomcat) in the development environment because of its usefulness in the fast development lifecycle, but we certainly do not want those server jars to be included in the finally generated maven artifact or war file. To do so, set the scope of embedded server dependency to ‘provided‘.


Scope ‘provided’ indicates you expect the JDK or a container to provide the dependency at runtime. This scope is only available on the compilation and test classpath, and is not transitive.

3. Configure SpringBootServletInitializer

To deploy our application on a web container, we must initialize the Servlet context, which is required. In this regard, our App class must extend SpringBootServletInitializer.

Note that a WebApplicationInitializer is only needed if we build a war file and deploy it. If you prefer to run an embedded web server, you won’t need this.

public class WarInitializerApplication extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WarInitializerApplication.class);


Now, if we package our application as a WAR, we’ll be able to deploy it on any web container in a traditional way, which will also execute the logic we added in the configure() method.

4. War Packaging Demo

In this demo, we are using below pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd;
      <name>Spring GA Repository</name>

Now run the maven build with goal package and it will generate the project’s war file in the target folder as below.

mvn package
Spring Boot War Packaging Example
Spring Boot War Packaging Example

Drop me your questions in the comments section.

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode