Packaging Spring Boot App As War File

Default application packaging used by spring boot applications is jar packaging. We can also generate a war file for our spring boot application easily.

Spring boot Jar packaging

Jar packaging generates a jar file, which contains an embedded servlet container. We can run this packaged jar to start the spring boot application.

The pom.xml file contains the application packaging information as shown below.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<!-- ... -->
	<packaging>jar</packaging>
	<!-- ... -->
</project>

Spring boot war file packaging

Sometimes, we may want to deploy a spring application as a war file. We can deploy this war file into servers like tomcat, Jboss, etc.

For this, we have to change the packaging property by setting it to war inside the pom.xml file.

<?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>
	<packaging>war</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.asb.example</groupId>
	<artifactId>spring-boot-war-packaging</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-war-packaging</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</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>

In the above pom.xml file, the tomcat starter dependency’s scope is set to the value provided. This indicates that during the packaging process, the application will not add an embedded tomcat server to the generated package.

The JDK or web-Server/Container also provides the required dependency during runtime.

Package the application by running maven build. We also should be able to see generated war file under /target directory of our spring application.

The below images shows generated war file after the maven build.

Spring boot war packaging exampl

Conclusion

In this article, we learned how to package our spring boot application into a war file.