0
0
SpringbootHow-ToBeginner · 3 min read

How to Create WAR File in Spring Boot: Step-by-Step Guide

To create a WAR file in Spring Boot, change the packaging type to war in your pom.xml and extend SpringBootServletInitializer in your main application class. Then build the project using mvn clean package to generate the WAR file for deployment on external servlet containers.
📐

Syntax

To create a WAR file in Spring Boot, you need to adjust your pom.xml and your main application class as follows:

  • pom.xml: Set <packaging>war</packaging> to tell Maven to build a WAR file.
  • Main Application Class: Extend SpringBootServletInitializer and override configure method to support deployment in external servlet containers.
java
<project>
  ...
  <packaging>war</packaging>
  ...
</project>

@SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MyApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
💻

Example

This example shows a minimal Spring Boot project configured to build a WAR file. The pom.xml sets packaging to war, and the main class extends SpringBootServletInitializer. Running mvn clean package creates a WAR file in the target folder.

xml/java
<?xml version="1.0" encoding="UTF-8"?>
<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
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>war-demo</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

---

package com.example;

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(WarDemoApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(WarDemoApplication.class, args);
    }
}
Output
Building war-demo-1.0.0.war in target directory
⚠️

Common Pitfalls

Common mistakes when creating a WAR file in Spring Boot include:

  • Forgetting to set <packaging>war</packaging> in pom.xml, which results in a JAR file instead.
  • Not extending SpringBootServletInitializer in the main class, causing deployment failures on external servers.
  • Including the embedded Tomcat server dependency explicitly, which is unnecessary and can cause conflicts.
xml/java
/* Wrong: packaging is jar, no war file generated */
<packaging>jar</packaging>

/* Correct: packaging set to war */
<packaging>war</packaging>

/* Wrong: main class does not extend SpringBootServletInitializer */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

/* Correct: main class extends SpringBootServletInitializer */
@SpringBootApplication
public class App extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(App.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
📊

Quick Reference

  • Set <packaging>war</packaging> in pom.xml.
  • Extend SpringBootServletInitializer in your main application class.
  • Override configure method to specify the application sources.
  • Run mvn clean package to build the WAR file.
  • Deploy the generated WAR file to any external servlet container like Tomcat or Jetty.

Key Takeaways

Set packaging to war in pom.xml to build a WAR file.
Extend SpringBootServletInitializer in your main class for external server deployment.
Override configure method to specify the application class.
Use mvn clean package to generate the WAR file.
Deploy the WAR file to external servlet containers like Tomcat.