Spring Boot war Packaging Example

In Spring boot applications, default packaging is jar which is deployed in embedded servers. If you want to generate a war file for deployment in seperate application server instances such as Jboss, Weblogic or tomcat, then follow below instructions.

Step 1) Declare packaging type ‘war’

First logical step is to declare the packaging type ‘war’ in pom.xml file.

<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;
  ...
  <packaging>war</packaging>
  ...
</project>

It indicates the project’s artifact type. Please note that when no packaging is declared, Maven assumes the artifact is the default: jar.

Step 2) Set embedded server dependency scope to ‘provided’

We may want to have embedded server (e.g. tomcat) in development environment because of its usefulness in fast development lifecycle, but we certainly not want those server jars to be included in finally generated maven artifact or war file. To do so, set scope of embedded server dependency to ‘provided’.

Scope ‘provided’ indicates you expect the JDK or a container to provide the dependency at runtime. This scope is only available on the compilation and test classpath, and is not transitive.

Read more: Dependency Mechanism

War Packaging Demo

In this demo, we are using below pom.xml.

<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.howtodoinjava</groupId>
	<artifactId>springbootdemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<packaging>war</packaging>

	<name>springbootdemo</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>

	<properties>
		<java.version>1.8</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</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-hateoas</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
	</dependencies>

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

	<repositories>
		<repository>
			<id>repository.spring.release</id>
			<name>Spring GA Repository</name>
			<url>http://repo.spring.io/release</url>
		</repository>
	</repositories>
</project>

Now run maven build with goal clean install and it will genearte the project’s war file in target folder as below.

Spring Boot War Packaging Example
Spring Boot War Packaging Example

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

12 thoughts on “Spring Boot war Packaging Example”

  1. Hi Lokesh,

    Hope you are doing well!

    Any ideas on how to package an executable spring boot jar as war? I understand it will be a manual process configuration. The idea is to ship this in JBOSS as a war eventually.

    Thanks and Regards,
    Abhi

    Reply
  2. do you know if it is possible to “update” a regular spring mvc application in spring boot application? because in spring boot we eliminate web.xml and xml configuration in favor of conventions of spring boot

    Reply
  3. Hi,
    I created a boot project to create a war and trying to build that on jenkin and getting following error
    org.apache.maven.plugin.PluginResolutionException: Plugin org.springframework.boot:spring-boot-maven-plugin:2.0.0.RELEASE or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.springframework.boot:spring-boot-maven-plugin:jar:2.0.0.RELEASE.

    Please help me in resolving this issue

    Reply
  4. Hi, i lost my all the packages of spring boot like classes and controller but i have the snapshot.jar file with me so it is possible to retrieve the packages from it??

    Reply
  5. you will have to modify the main application class , It extends from SpringBootServletInitializer so that it can be deployed as a WAR.

    Ex-

    @SpringBootApplication
    public class Application extends SpringBootServletInitializer {

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

    Reply
  6. You didn’t tell anything about the project structure, for instance where to put the WebContent and its subfolders, web-inf/web.xml file etc in the spring boot 2.x configuration

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.