How to Package Spring Boot Projects as JAR and WAR
This article explains the differences between JAR and WAR packaging for Spring Boot applications, shows why embedded Tomcat behaves differently from external Tomcat, and provides step‑by‑step instructions with Maven commands and pom.xml modifications to build both JAR and WAR artifacts.
When a Spring Boot project is packaged as a JAR and run with java -jar , the embedded Tomcat starts and uses the port defined in the application's configuration; when the same project is packaged as a WAR and deployed to an external Tomcat, the external server's settings (default 8080) take precedence and the application context path is added.
The article gives a brief history of Java EE, Servlets, JSP, and the evolution toward lightweight, embedded servers such as Jetty and Undertow, leading to the modern "use JAR, not WAR" micro‑service approach.
Differences between JAR and WAR
1. A WAR is a web module containing a WEB-INF directory and can be directly deployed to a servlet container. A JAR usually contains only compiled classes and a Main-Class entry, allowing it to be executed with java -jar .
2. WAR packages are intended for full web applications and are deployed to containers; JAR packages are often used for reusable libraries or standalone Spring Boot applications.
3. JAR files are ZIP‑based archives that can also hold libraries, components, and plugins, and may be signed for security.
Packaging a Spring Boot project as a JAR
1) Create a new Spring Starter Project in IDE (packaging defaults to jar ).
2) Example pom.xml for a basic JAR project:
<?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 command:
mvn clean package
The resulting .jar appears in the target directory and can be run via java -jar demo-0.0.1-SNAPSHOT.jar .
Packaging the same project as a WAR
1) Add a ServletInitializer class (usually generated automatically when the packaging type is set to war ).
2) Change the pom.xml packaging from jar to war and adjust dependencies:
<project ...>
...
<packaging>war</packaging>
...
<dependencies>
<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>
<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>3) Build the WAR with the same Maven command:
mvn clean package
The generated .war can be placed in Tomcat's webapps directory and started by launching Tomcat.
When the project is created with packaging set to war from the beginning, the IDE automatically generates the ServletInitializer and the appropriate pom.xml structure.
Overall, the guide records the complete process of switching between JAR and WAR packaging for Spring Boot, including necessary Maven configuration changes, dependency adjustments, and command‑line build steps.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.