How to Package Spring Boot as a WAR and Deploy to an External Tomcat
This guide shows how to change a Spring Boot project’s packaging to WAR, remove the embedded Tomcat, add the servlet‑api dependency, create a servlet initializer, build the WAR with Maven, and deploy it to an external Tomcat server for verification.
Overview
Spring Boot normally runs with an embedded Tomcat, but many production environments use a separately managed Tomcat instance. This article walks through converting a Spring Boot application into a deployable WAR file and deploying it to an external Tomcat container.
Change Packaging to WAR
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>Remove Embedded Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Remove embedded Tomcat plugin -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>Add servlet‑api Dependency
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>Create Servlet Initializer
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// Point to the original Application class that contains the main method
return builder.sources(Application.class);
}
}Build and Deploy
Run mvn clean package in the project root (where pom.xml resides). The build should finish with [INFO] BUILD SUCCESS and produce a .war file in the target directory.
Copy the generated WAR to Tomcat’s webapps directory and start Tomcat. Tomcat will automatically unpack and deploy the application.
Access the application via http://YOUR_IP:PORT/your‑project‑name. Optionally rename the WAR to ROOT.war to make it accessible at the root context ( http://YOUR_IP:PORT/).
Verification
The following screenshots show the successful deployment and access of the Spring Boot application on the external Tomcat server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
