Packaging the Spring Boot Application
Learn to package your application as an executable JAR, understand the fat JAR concept, and deployment strategies
Introduction
In the section on using plugins in Maven, we introduced how to use maven-shade-plugin to package an executable JAR file. In Spring Boot applications, packaging is even simpler because Spring Boot comes with a built-in, even simpler spring-boot-maven-plugin plugin for packaging.
We only need to add the following configuration to our pom.xml:
Spring Boot Maven Plugin
<project...>
...
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>Without any configuration, this Spring Boot plugin will automatically locate the application's entry class.
Packaging the Application
We can package the application by executing the following Maven command:
$ mvn clean packageTaking springboot-exec-jar project as an example, after packaging, we can see two JAR files in the target directory:
$ ls
classes
generated-sources
maven-archiver
maven-status
springboot-exec-jar-1.0-SNAPSHOT.jar
springboot-exec-jar-1.0-SNAPSHOT.jar.originalUnderstanding the JAR Files
.jar.original
springboot-exec-jar-1.0-SNAPSHOT.jar.originalA JAR file built by the Maven standard packaging plugin, which only contains your own classes and does not include dependencies.
.jar (Executable)
springboot-exec-jar-1.0-SNAPSHOT.jarA JAR file created by the Spring Boot packaging plugin that includes all dependencies and can be run directly.
Running the Application:
$ java-jar springboot-exec-jar-1.0-SNAPSHOT.jarSimple Deployment
Deploying a Spring Boot application is very simple - no pre-installation of any server is required; you only need to upload the JAR file.
Excluding DevTools from Package
During the packaging process, since the packaged Spring Boot application will not be modified, spring-boot-devtools dependency will not be included by default.
Note for Earlier Versions
When using earlier versions of Spring Boot, some configuration is required to exclude spring-boot-devtools:
<plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludeDevtools>true</excludeDevtools></configuration></plugin>Custom JAR Filename
If you don't like using the default project name + version number as the filename, you can add a configuration to specify the filename:
<project...>
...
<build><finalName>awesome-app</finalName>
...
</build></project>The filename after packaging will be: awesome-app.jar
Summary
Spring Boot provides a Maven plugin for packaging all dependencies into a single JAR file.
This plugin is very easy to use and requires no configuration.
The resulting JAR file is self-contained and can be deployed without any additional server installation.