Spring Boot
Tomcat
WAR file
Deployment
Java

Spring Boot War deployed to Tomcat

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Spring Boot is widely appreciated for its ability to simplify the Java development process. While it allows for the creation of stand-alone applications with embedded server support, there are scenarios where deploying a traditional WAR file to an external web server, such as Apache Tomcat, is preferred. This article explores how to deploy a Spring Boot application to Tomcat by packaging it as a WAR file, detailing the necessary configurations and steps involved in the process.

Spring Boot and WAR Files

Spring Boot typically favors executable JARs, which are easy to run using embedded application servers. However, deploying a WAR (Web Application Archive) is essential when integrating with existing infrastructure, using distributed architectures, or adhering to enterprise guidelines that involve managing separate web servers such as Tomcat.

Getting Started with Spring Boot WAR Configuration

  1. Convert a Spring Boot JAR Application to WAR
    By default, Spring Boot applications are built as JARs. Transforming them into WAR involves some modifications:
    • Update pom.xml: Change the packaging type and add necessary dependencies.
xml
1     <packaging>war</packaging>
2
3     <dependencies>
4       <!-- Add Tomcat dependency only for compiling -->
5       <provided>
6         <groupId>org.springframework.boot</groupId>
7         <artifactId>spring-boot-starter-tomcat</artifactId>
8       </provided>
9       <!-- Other dependencies -->
10     </dependencies>
  • Create a Deployment Descriptor: Include a web.xml (optional but recommended) for defining servlets and context configurations.
  • Extend SpringBootServletInitializer in the Application Class:
java
1     import org.springframework.boot.builder.SpringApplicationBuilder;
2     import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
3
4     public class MyApplication extends SpringBootServletInitializer {
5         @Override
6         protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
7             return builder.sources(MyApplication.class);
8         }
9     }

Configuring Tomcat for Deployment

Tomcat needs to be correctly configured to accept WAR deployments:

  1. Tomcat Directory Structure: Ensure your Tomcat installation has the following structure, which includes directories like bin, conf, logs, and webapps.
  2. Deploy WAR to webapps Directory: Copy the WAR file to the webapps folder. Tomcat will automatically deploy and unpack the WAR once it starts.
  3. Configure Server Ports and Settings: Update the server.xml or conf/context.xml files to manage specific deployment configurations, such as exposed ports, security settings, or resource configurations.

Development Considerations and Best Practices

When deploying a Spring Boot WAR to Tomcat, keep in mind the following considerations:

  • Environment-Specific Configurations: Properties files and server settings should be segregated using Spring Profiles, enabling different configurations for development, testing, and production.
  • Logging Integration: Ensure that logging configurations are set up to reflect both standard output logging during local development and appropriate enterprise-standard logging in production.
  • Resource Utilization: Analyze and tune the resources allocated to the Tomcat server to prevent resource bottlenecks from affecting application performance.
  • Security: Regularly update Tomcat and other dependencies to mitigate security vulnerabilities. Employ HTTPS and consider configuring thread pools correctly to manage high load efficiently.

Example Directory Layout and Files

A simplified example structure of a Spring Boot WAR file and associated files:

 
1MyApp/
2├── pom.xml
3├── src/main/java/myapp/
4│   └── MyApplication.java
5├── src/main/webapp/WEB-INF/
6│   └── web.xml
7├── src/main/resources/
8│   ├── application.properties
9│   └── logging.properties

Key Points Comparison Table

CategorySpring Boot JARSpring Boot WAR
Deployment ServerEmbedded server (Tomcat, Jetty)External Tomcat server
Configuration Fileapplication.properties/yamlRequires web.xml for additional servlet configurations
PackagingStand-alone JARWAR
PortabilityHighly portableRelies on the web server
Use CasesMicroservices, serverless applications 🍃Traditional web applications, controlled environments

Conclusion

Deploying a Spring Boot application as a WAR file to a Tomcat server allows for integration into existing enterprise environments and provides additional control over server configurations. While it introduces additional complexity compared to Spring Boot JAR deployment, the trade-offs in terms of management, control, and scalability make it suitable for various enterprise scenarios. Ensure thorough configuration management and resource allocation to maximize application performance and security.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.