Spring Boot - Build Deployable Executable JAR or WAR file
After we develop and test a Spring Boot application, we need to build a package for deployment.
The deployable package can be a JAR or a WAR file, which acts as a package that contains all the application code and its dependencies.
- JAR stands for Java ARchieve
- WAR stands for Web application ARchieve
In general, we choose JAR if the application is a Java application and choose WAR if the application is a web application.
- JAR is used to aggregate many Java class files and associated metadata and resources like images, files, etc., into a distributable file format.
- WAR is used to aggregate one or more JAR files, Java Servlets, Java Classes, XMLs, files, static content, etc., into a distributable file format.
Packaging Type
Before building the application, the package type (jar or war) can be specified within the pom.xml
file, that determines the format of the package that gets generated by the build as shown below.
Spring Boot packages a project with all its dependencies inside the final artifact created, so we call it a "Fat JAR" or "Fat WAR" to make it completely self-contained, easily deployable on any machine/server with a JVM using standard command line tools.
<packaging>jar</packaging>
<packaging>war</packaging>
Maven Dependencies
Spring Boot provides capabilities for container-less deployments by simply adding the below configuration in pom.xml
file.
The generated JAR or WAR file can be simply run as a standalone application, directly using the command java -jar app-name.jar
from any command line tool.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.0</version>
</plugin>
</plugins>
Maven Build and Run
Build the application using maven command: mvn clean install
to generate the respective build file.
Then, we can run the using java command: java -jar <artifact-name>
from any command line tool.
- It runs the application similar to how it invokes on a IDE and shows all its logs on the command line tool.
- The application shuts down automatically once the command line terminal is closed. And if we need to have the application up and running even after closing the terminal, then use the command:
nohup java -jar <artifact-name>
Then, we can kill the Spring Boot application similar to other processes on a system, by simply ctrl+c
or kill <pid>
.
Additional Configuration
We don't need this configuration all the times, as the applications work perfectly fine even without this configuration.
However, there can be instances where we need to tell the Spring Boot about the main class in any of the below two ways.
If we are using spring-boot-starter-parent, we can simply add the below property in pom.xml.
<properties>
<start-class>org.example.boot.Application</start-class>
</properties>
Otherwise, specify that in the Maven plaugin as shown below.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.0</version>
<configuration>
<mainClass>org.example.boot.Application</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
Conclusion
Now, we know how to generate deployable JAR and WAR files.