HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Spring Boot 2 / Spring Boot war Packaging Example

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?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Abhi

    August 17, 2020

    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

  2. Eric Martinez

    May 30, 2020

    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

    • Lokesh Gupta

      May 30, 2020

      Yes, it is always possible. Try these tips.

  3. Jully Vijayvargiya

    April 1, 2020

    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

    • Lokesh Gupta

      April 2, 2020

      Jars may have been corrupt. Try this script.

  4. Raiyan

    January 21, 2020

    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??

  5. vijay

    November 17, 2019

    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);
    }
    }

  6. mio

    March 28, 2019

    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

  7. Sepehr

    March 13, 2019

    I think this point missed from this article.
    To deploy our application on a web container we must initialize the Servlet context which is required. In this regard out App class must extend SpringBootServletInitializer.

    for mor information refer to below docs:
    https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/web/servlet/support/SpringBootServletInitializer.html

  8. sam

    June 26, 2018

    Hi,
    why when I convert the jar package to war, I can’t reach the wsdl?

Comments are closed on this article!

Search Tutorials

Spring Boot 2 Tutorial

  • Spring Boot – Introduction
  • Spring Boot – Starter parent
  • Spring Boot – Starter templates
  • Spring Boot – Multi-module project
  • Spring Boot – Annotations
  • Spring Boot – Auto configuration
  • Spring Boot – AOP
  • Spring Boot – Logging
  • Spring Boot – DevTools
  • Spring Boot – WAR Packaging
  • Spring Boot – REST API
  • Spring Boot – CRUD
  • Spring Boot – OAuth2
  • Spring Boot – Testing
  • Spring Boot – RestTemplate
  • Spring Boot – Thymeleaf
  • Spring Boot – Hibernate
  • Spring Boot – DataSource
  • Spring Boot – Error Handling
  • Spring Boot – Caching
  • Spring Boot – Retry
  • Spring Boot – BasicAuth
  • Spring Boot – H2 Database
  • Spring Boot – Ehcache 3.x
  • Spring Boot – Gson
  • Spring Boot – RMI
  • Spring Boot – Send Email
  • Spring Boot – Interview Questions

Spring Boot Tutorial

  • Spring Boot – CommandLineRunner
  • Spring Boot – Configure Jetty
  • Spring Boot – Tomcat Default Port
  • Spring Boot – Context Root
  • Spring Boot – SSL [https]
  • Spring Boot – Get all loaded beans
  • Spring Boot – PropertyEditor
  • Spring Boot – @EnableScheduling
  • Spring Boot – Jersey
  • Spring Boot – SOAP Webservice
  • Spring Boot – SOAP Client
  • Spring Boot – JMSTemplate
  • Spring Boot – REST APIs
  • Spring Boot – JSP View
  • Spring Boot – Actuator endpoints
  • Spring Boot – Role Based Security
  • Spring Boot – RSS / ATOM Feed
  • Spring Boot – Ehcache 2.x

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)