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

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>

Sometimes, we may want to deploy spring boot application as a war file. This packaged war file can be later deployed into servers like tomcat, Jboss, etc.

For this, we have to change packaging property by setting it to war inside 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 above pom.xml file, tomcat starter dependency’s scope is set to provided. This indicates that during packaging process, embedded tomcat server is not added to generated package. JDK or web-Server/Container provides the required dependency during runtime.

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

Below images shows generated war file after maven build.

Spring boot war packaging exampl

Conclusion

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

Happy coding!! 🙂 🙂

You may also intrested in