How do you deploy a Spring Boot web application as a JAR or WAR file?
Spring Boot applications can be deployed as executable JAR files with an embedded server or as WAR files for deployment on an external servlet container. The choice depends on the deployment environment and infrastructure requirements.
Key Points
JAR deployment uses embedded servers such as Tomcat, Jetty, or Undertow and is the preferred approach in modern microservices architectures.
WAR deployment is used when applications need to run inside external application servers such as Tomcat, WebLogic, or JBoss.
Spring Boot supports both deployment models with minimal configuration changes.
Example
Consider an Order Management application.
JAR Deployment
The application contains an embedded Tomcat server.
Running the JAR automatically starts the web server.
WAR Deployment
The application is packaged as a WAR file.
The WAR is deployed to an existing external Tomcat server.
JAR Deployment Process
1. Configure packaging in pom.xml
<packaging>jar</packaging>
2. Build the application
mvn clean package
3. Run the generated JAR
java -jar target/order-service.jar
Spring Boot automatically starts the embedded server and deploys the application.
Advantages of JAR Deployment
Simplified deployment.
No external server installation required.
Ideal for Docker and Kubernetes environments.
Faster startup and easier scaling.
WAR Deployment Process
1. Configure packaging in pom.xml
<packaging>war</packaging>
2. Extend SpringBootServletInitializer
@SpringBootApplication
public class Application
extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder application) {
return application.sources(
Application.class);
}
}
Prefer executable JAR files for new Spring Boot projects.
Use WAR packaging only when organization standards require external application servers.
Use Docker containers for JAR deployments in cloud environments.
Interview Tip
A concise interview answer is: Spring Boot applications can be deployed as executable JAR files or traditional WAR files. JAR deployment uses an embedded server and is the preferred choice for modern microservices and cloud environments, while WAR deployment is used when applications need to run inside external servlet containers such as Tomcat or JBoss.