Converting a Spring Boot Executable Jar to a Deployable War File

By default Spring Boot applications build an executable jar file. This is ideal for development and testing however when it’s time to deploy to a production container such as VMware tc Server there are a few simple steps which must be performed.

There are two options.

Option 1: Start with a Deployable War

If using the Spring Initializer to start the project choose the War option for Packaging

Packaging: War

and deploy to VMware tc Server for testing.

Option 2: Converting to Deployable War (Gradle)

If the project was already started with an executable Jar file there are just a few steps which need to be performed

  1. Add the war plugin

    plugins {
        id 'org.springframework.boot' version '2.3.4.RELEASE'
        id 'io.spring.dependency-management' version '1.0.10.RELEASE'
        id 'java'
        id 'war' // Insert this line
    }
    
  2. Change the embedded tomcat to provided

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // Insert this line
        testImplementation('org.springframework.boot:spring-boot-starter-test') {
                exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
        }
    }
    
  3. Add ServletInitializer

    package com.example.demo;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(DemoApplication.class); // Replace DemoApplication with your main class
        }
    
    }
    

Option 2: Converting to Deployable War (Maven)

If the project was already started with an executable Jar file there are just a few steps which need to be performed

  1. Add the war plugin

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging> // Change this from jar to war
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    
  2. Change the embedded tomcat to provided

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    // Insert this block
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    
  3. Add ServletInitializer

    package com.example.demo;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
                return application.sources(DemoApplication.class); // Replace DemoApplication with your main class
        }
    
    }