How to Deploy Spring Boot Web Applications as Jar and War Files?

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);
    }
}

3. Mark embedded Tomcat as provided:

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

4. Build the WAR file:

mvn clean package

5. Deploy the generated WAR file into:

• Apache Tomcat • WebLogic • JBoss • WebSphere

Comparison:

JAR Deployment: • Embedded server included. • Simpler deployment. • Preferred for microservices. • Ideal for cloud-native applications.

WAR Deployment: • Requires external servlet container. • Suitable for traditional enterprise environments. • Allows multiple applications on the same server.

Real-World Example:

Microservices Architecture: • User Service → JAR • Order Service → JAR • Payment Service → JAR

Traditional Enterprise Portal: • HR Application → WAR • Payroll Application → WAR

Best Practices: • 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.