Backend Development 13 min read

Understanding JAR vs WAR Packaging in Spring Boot and How to Build Them

This article explains why a Spring Boot project runs differently when packaged as a JAR versus a WAR, outlines the historical evolution of Java web containers, compares the characteristics of JAR and WAR files, and provides step‑by‑step Maven commands and pom.xml configurations for building both package types.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Understanding JAR vs WAR Packaging in Spring Boot and How to Build Them

The author starts by describing a puzzling situation: a Spring Boot project packaged as a JAR runs with an embedded Tomcat on the configured port, while the same project packaged as a WAR deployed to an external Tomcat defaults to port 8080 and includes the project name in the URL, because the embedded server is disabled when deployed to a container.

A brief history follows, recounting the origins of Java EE, the servlet and JSP standards, the creation of Tomcat as a servlet container, and the later emergence of embedded servers such as Jetty and Undertow that enable the "use JAR, not WAR" micro‑service approach.

JAR and WAR Differences

JAR files are generic Java archives that contain compiled classes and a Main-Class entry, allowing execution via java -jar . WAR files are specialized web archives that must include a WEB-INF directory, servlet classes, and optionally JSPs, and they are intended to be deployed to a servlet container.

JAR: suitable for standalone applications or libraries.

WAR: suitable for web applications that rely on an external container.

The article also lists format advantages of JAR files, such as digital signing, reduced download time, compression, and extensibility via the Java Extensions Framework.

Packaging a Spring Boot Project as a JAR

1. Create a new Spring Starter Project (packaging defaults to JAR).

2. Use the following pom.xml (excerpt):

<?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>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>
  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

3. Build the JAR with the Maven command:

mvn clean package

The resulting JAR appears in the target directory and can be started with java -jar demo-0.0.1-SNAPSHOT.jar .

Packaging the Same Project as a WAR

1. Add a ServletInitializer class (automatically generated when the project is created with packaging=war ).

2. Modify pom.xml to change the packaging type and add the necessary dependencies, for example:

<packaging>war</packaging>
...
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.apache.tomcat</groupId>
  <artifactId>tomcat-servlet-api</artifactId>
  <version>8.0.36</version>
  <scope>provided</scope>
</dependency>

3. Build the WAR with the same Maven command:

mvn clean package

The generated WAR can be copied to the webapps directory of an external Tomcat server and started by launching Tomcat.

When the project is initially created with packaging=war , the ServletInitializer class is generated automatically, simplifying the process.

Overall, the article provides a complete guide to switching between JAR and WAR packaging in Spring Boot, including historical context, technical differences, Maven configuration, and command‑line steps.

JavamavenSpring BootpackagingServletJarWAR
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.