Topic 11: – 3 steps of Spring Boot Deployment (Bonus – Docker and Kubernetes deployments)

image 2

Play Store Application link – Spring Boot in 13 steps – App on Google Play


Step 1: Setting Up a Spring Boot Project

Using Spring Initializr

  1. Visit https://start.spring.io/.
  2. Configure the project:
    • Project: Maven
    • Language: Java
    • Spring Boot Version: 3.x.x
    • Dependencies: Spring Web, Spring Boot DevTools
  3. Click Generate to download the project ZIP and extract it.

Import into IDE

  • Import the project into your favorite IDE (IntelliJ, Eclipse, etc.) as a Maven project.

Step 2: Packaging Spring Boot Application as JAR/WAR

JAR Packaging (Default)

By default, Spring Boot creates a JAR file. To build your JAR, run the following Maven command:

./mvnw clean package

This will generate a .jar file in the target/ directory.

WAR Packaging

To deploy your Spring Boot application as a WAR (for example, to a Tomcat server), update the pom.xml file:

  1. Change the packaging type to war:
<packaging>war</packaging>

  1. Update the main class to extend SpringBootServletInitializer:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

  1. Build the WAR file:
./mvnw clean package


Step 3: Deploying Spring Boot on Tomcat Server

Deploying JAR to Tomcat

  1. For WAR files, copy the target/*.war file to the webapps/ directory of your Tomcat server.
  2. For JAR files, run it directly from the command line:
java -jar target/demo-0.0.1-SNAPSHOT.jar


Here’s a shorter, simpler version of the note:


Additional Note:
If you’re curious about how Docker and Kubernetes improve deployment, check out these posts:

  1. Spring Boot – Docker Deployment
  2. Spring Boot – Kubernetes Deployment

We’ll cover Docker and Kubernetes in more detail in separate sections later.

Here’s a quick comparison of the deployment methods:

  1. Manual Deployment: Done manually, slow, and hard to scale.
  2. Docker Deployment: Packages the app with its dependencies, making it portable and easier to deploy.
  3. Kubernetes Deployment: Automatically manages and scales Docker containers for better reliability and efficiency.

Conclusion

In this guide, we covered:

  1. Packaging Spring Boot applications as JAR/WAR files.
  2. Deploying on server (Tomcat)

Leave a Reply

Your email address will not be published. Required fields are marked *