Deploying Spring Boot Applications

In this article of Spring Boot, we will discuss Deploying Spring Boot Applications. We will discuss Deploying Spring Boot Applications on external Tomcat or any other external application server.

 

Introduction

Spring Boot provides support for embedded servlet container which makes it easy to deploy our application. There is a common question how do I deploy Spring Boot application to an external Tomcat?.

To deploy Spring Boot Application on Tomcat or any other application server, follow these 4 steps 

  • Change packing from jar to war in pom.xml.
  • Exclude embedded container dependency from pom.xml
  • Enter web entry point by using SpringBootServletInitializer.
  • Deploy war on tomcat 

 

1. Change packaging from jar to war

For deploying our spring boot application on external Tomcat, change packaging from jar to war.

The final artifact created by Maven will now be war file.

....
<packaging>jar</packaging>
....

 

2. Exclude Embedded container dependencies

We do not want embedded container classes to conflict with the classes part of application servers.

To avoid this conflict, declare dependency as provided in pom.xml file

<dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-tomcat</artifactId>
     <scope>provided</scope>
  </dependency>
</dependencies>

Let’s understand what does above entry show

  • It shows that above dependency will not be part of the final artifact (war file).
  • The system should provide this dependency where we are deploying our application.
  • We excluded tomcat server, we can deploy final war on any application server causing no conflict.

This is one of the main benefits of Spring Boot. We still have the full flexibility to run our application using java-jar command or it can be also be deployed on to an application server.

 

 

3. Extend SpringBootServletInitializer

We need to change our Spring Boot main class by extending SpringBootServletInitializer and ensure to override configure method.

3.1 Standard Spring Boot Main Class
@SpringBootApplication
public class DemoApplication {
     public static void main(String[] args) {
         SpringApplication.run(DemoApplication.class, args);
	}
}
 
3.2  Spring Boot Main Class WAR deployment
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

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

    private static Class < DemoApplication > applicationClass = DemoApplication.class;
}

 

If you have over one class with the main method, you need to specify Spring Boot which is your main class to start.

Use <properties> tag in your pom.xml to specify your main class.

<properties>
   <start-class>com.javadevjournal.MyMainClassForSpringBoot</start-class>
</properties>

 

 

4. Deploy Spring Boot Application on Tomcat

As part of the last step, create the final artifact (war file) and deploy it on the tomcat.

Run mvn package command to create final war. Once this is done, copy the war file from $PROJECT_DIR/target/xxx.war to Tomcat and run your application. 

 

5. Complete Example

Let’s have a look at the complete example.We will use sample web application and adapt it for deploying on the standalone application server.Read our article Creating a Web Application with Spring Boot  for creating web application using Spring Boot

 

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

    <groupId>com.javadevjournal</groupId>
    <artifactId>wardeployment</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>wardeployment</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Declaring it as optional to avoid conflict with application server -->
        <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>
 
5.2 Setting up web configurations

Read Spring Boot Web Application Configuration to understand Spring Boot web application configurations.

 

5.2 Spring Boot Main Class
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class WardeploymentApplication extends SpringBootServletInitializer{

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
	   return applicationBuilder.sources(WardeploymentApplication.class);
	}

	public static void main(String[] args) {
	    SpringApplication.run(WardeploymentApplication.class, args);
	}
}
 
5.3 Build and Deployment

As part of the last step, run the mvn package and deploy WAR file on the tomcat.

 

Summary

In this post, we learned and discuss Deploying Spring Boot Applications. We learned steps required to Deploy Spring Boot Application on external Tomcat.