Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Spring Boot provides an easy and consistent way to package applications for deployment. The main goal is to create a self-contained, executable JAR or WAR that includes everything necessary to run the application, including embedded server, application code, and other dependencies.
Let's understand the packaging in Spring Boot:
There are mainly two types of packaging:
JAR (Java ARchive): This is the most common packaging in Spring Boot applications. It produces a single, executable JAR that contains all the required dependencies. It's suitable for microservice architectures and cloud deployments.
WAR (Web ARchive): Used when you want to deploy your application to an external servlet container like Tomcat, JBoss, etc.
By default, Spring Boot packages your application as an executable JAR. The generated JAR contains an embedded Tomcat, Jetty, or Undertow servlet container, which means you can run it from the command line without needing an external server.
For JAR packaging, use the following in your pom.xml
:
<packaging>jar</packaging>
To build and run:
mvn clean package java -jar target/your-artifactId-version.jar
If you want to deploy your application to an external servlet container, you can package it as a WAR.
First, change the packaging type in your pom.xml
:
<packaging>war</packaging>
Then, you'll need to mark the embedded servlet container as provided (so it won't be included in the WAR):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
After packaging the application with mvn clean package
, you can deploy the resulting WAR to any servlet container.
The spring-boot-maven-plugin
facilitates packaging and running Spring Boot applications. It repackages your JAR/WAR to be executable and provides the spring-boot:run
goal to run the application.
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
By including this plugin, you can run:
mvn spring-boot:run
This will start your application using the embedded server.
Spring Boot 2.3.0 introduced support for packaging applications into layers, allowing for optimizations when building Docker images. This is beneficial in scenarios where the application's dependencies change less frequently than its own code.
To enable layering, add the following configuration to the spring-boot-maven-plugin
:
<configuration> <layers> <enabled>true</enabled> </layers> </configuration>
Packaging in Spring Boot is streamlined to simplify deployment scenarios. With the flexibility to package applications as either JARs or WARs and the additional support for layers in Docker environments, Spring Boot addresses various deployment needs.
Executable JAR vs. WAR in Spring Boot:
pom.xml
or build.gradle
file:<!-- For JAR packaging --> <packaging>jar</packaging> <!-- For WAR packaging --> <packaging>war</packaging>
Customizing the packaging process in Spring Boot:
pom.xml
or build.gradle
file:<!-- Maven example --> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>unwanted-file.txt</exclude> </excludes> </resource> </resources> </build>
Building and packaging with Maven in Spring Boot:
mvn package
command to build and package the application.mvn clean package
Containerization of Spring Boot applications (Docker):
Dockerfile
to describe the container image:FROM openjdk:11 COPY target/myapp.jar /app.jar ENTRYPOINT ["java", "-jar", "/app.jar"]
Packaging Spring Boot applications with Gradle:
build.gradle
file:// For JAR packaging apply plugin: 'java' // For WAR packaging apply plugin: 'war'
Creating a self-contained executable with Spring Boot:
spring-boot-maven-plugin
or spring-boot-gradle-plugin
:<!-- Maven example --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>