Srikanth Technologies

Deploying Spring MVC Application To Tomcat

In this blog, I show how to deploy a Spring MVC application developed using Spring Boot on Tomcat Server.

Preparing Project For Deployment

I assume we have a Spring MVC application developed using Spring Boot ready to be deployed to Tomcat Server.

Before we deploy our application, we need to prepare our Spring MVC application so that it can run on Tomcat Server as a web application.

Follow the steps below to prepare Spring MVC application for deployment. My demo application is called as books.

Make changes to POM.XML

Change packaging to war in POM.XML file using <packaging> element. Provide final name to your project using <finalName> element. Here is final POM.XML file with changes highlighted. The rest of entries in POM.XML remain the same.

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
		<relativePath />  
	</parent>
	<groupId>com.st</groupId>
	<artifactId>books</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>books</name>
	<description>Books Tracker</description>
	<packaging>war</packaging>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
	</dependencies>

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

Make .jar files available.

All JAR files related to Maven dependencies are already included in the project. But if there are any JAR files that are not mentioned in POM.XML and handled by Maven then such files (like Oracle JDBC driver) are to be copied to webapp/WEB-INF/lib folder.

It is important to ensure all JAR file are made available at runtime, otherwise application might fail to start. Maven takes care of copying all required JAR files to webapp/WEB-INF/lib when it is creating WAR file. But JAR file like OJDBC6.JAR, which is Oracle JDBC driver, must be manually copied to webapp/WEB-INF/lib folder.

Make Spring Boot Application Runnable in Web Container

In order to run a Spring Boot Application from traditional WAR deployment, we need to extend SpringBootServletInitializer class in Application Class.

Spring Boot documentation has more information about SpringBootServletInitializer.

package com.st.books;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class BooksApplication  extends SpringBootServletInitializer {

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

}

Build WAR file

Once packaging is set to WAR, it is possible to create a WAR file for our project by using the following steps.

Start Tomcat Server

Once books.war file is copied to webapps folder in Tomcat server, just go to bin directory of Tomcat server installation and run startup.bat file.

Tomcat will automatically deploy application by creating books folder and copy all files from .war file into books folder. All .jar files from application are copied into /books/WEB-INF/lib folder in Tomcat's webapps folder, classes into /books/WEB-INF/classes folder etc.

It is possible to monitor the status of application by keeping an eye on console window of Tomcat Server. If no errors are found then application is deployed successfully.

Testing

The last step is to start browser and make a request to test whether application is deployed successfully.

In my system I used port number 8888 for tomcat (remember 8080 is default but on machines running Oracle, port 8080 is already occupied). So, here is the URL to accessindex.html that is placed in webapp folder in our application.

 http://localhost:8888/books/index.html    

The rest of the urls are to be accessed as we did when we ran this application as a standalone application with embedded tomcat. For example, if we need to get list of books then we have to give the following url, assuming list is the URL mapped to a request method in controller.

http://localhost:8888/books/list

It is possible to deploy WAR file to any other Servers like Glassfish, Web Logic Server with small changes in the deployment process.