Create a deployable war with Spring Boot

January 04, 2020 No comments Spring Boot WAR Deploy Tomcat

1. Introduction

This article shows how to create a deployable .war file using Spring Boot. When it comes to deployment, Spring Boot is very flexible, it allows you to build executable jar with embedded web container as well as war file that can be deployed in various application servers.

2. Create a deployable war file

The first step to take in order to make deployable war file is to create SpringBootServletInitializer subclass and override its configure method. This subclass should be annotated with @SpringBootApplication just like in jar file approach.

@SpringBootApplication
public class CustomApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CustomApplication.class);
    }

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

The next thing we should do is to make some changes in pom.xml or build.gradle file. Packaging should be set to war:

  • for pom.xml:

    <packaging>war</packaging>
    
  • for build.gradle:

    apply plugin: 'war'
    

We also need to mark the embedded servlet container dependency as provided, to make sure that embedded servlet container will not be conflicted with servlet container on the server to which the war file will be deployed.

  • for pom.xml:

    <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <scope>provided</scope>
      </dependency>
    </dependencies>
    
  • for build.gradle:

    dependencies {
      providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    }
    

The fully formed pom.xml file prepared for creating war file, with minimum configuration, should have the following structure:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.frontbackend.springboot</groupId>
    <artifactId>build-war</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <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>
    </dependencies>
</project>

3. Conclusion

This article showcased how to build Spring Boot deployable war file instead of executable jar file. The tutorial should be useful for everyone who doesn't want to start their Spring Boot application with an embedded server but instead deploys war file on the different server.

Simple Maven application that creates war file with Spring is available on our GitHub repository with instructions how to run the app using docker.

{{ message }}

{{ 'Comments are closed.' | trans }}