1. Introduction
In this article, we are going to present how to create a runnable JAR file out of a Spring Boot application. One of the best features of Spring Boot is embedded web containers which allow us to pack the whole application with dependencies into a single jar file ready to deploy and run.
2. Maven configuration
To create a fat jar
with Spring Boot we need an additional spring-boot-maven-plugin
in our pom.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/>
</parent>
<groupId>com.frontbackend.springboot</groupId>
<artifactId>getting-started</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>getting-started</name>
<properties>
<java.version>1.8</java.version>
</properties>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
With that Maven configuration, we can build our Spring Boot project using mvn clean install
. To run our application we simply executing generated jar file in the target directory.
java -jar target/getting-started-0.0.1-SNAPSHOT.jar
we should see Spring Boot starting output logs on the console:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.0)
2021-05-27 22:20:05.735 INFO 567 --- [ main] c.f.s.GettingStartedApplication : Starting GettingStartedApplication v0.0.1-SNAPSHOT using Java 1.8.0_231 on frontbackend with PID 567 (/home/tutorials/spring-boot/getting-started/target/getting-started-0.0.1-SNAPSHOT.jar started by marcinw in /home/tutorials/spring-boot/getting-started/target)
2021-05-27 22:20:05.737 INFO 567 --- [ main] c.f.s.GettingStartedApplication : No active profile set, falling back to default profiles: default
2021-05-27 22:20:06.768 INFO 567 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-05-27 22:20:06.781 INFO 567 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-05-27 22:20:06.781 INFO 567 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-05-27 22:20:06.839 INFO 567 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-05-27 22:20:06.839 INFO 567 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1033 ms
2021-05-27 22:20:07.232 INFO 567 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-27 22:20:07.246 INFO 567 --- [ main] c.f.s.GettingStartedApplication : Started GettingStartedApplication in 1.872 seconds (JVM running for 2.291)
2021-05-27 22:20:07.247 INFO 567 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state LivenessState changed to CORRECT
2021-05-27 22:20:07.249 INFO 567 --- [ main] o.s.b.a.ApplicationAvailabilityBean : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
To stop the application we need to find that process and terminates it:
> ps aux | grep java
frontbackend 567 11.6 0.8 12252152 289796 pts/14 Sl+ 22:20 0:09 java -jar getting-started-0.0.1-SNAPSHOT.jar
> sudo kill -9 567
3. Additional Maven configuration
If we are not inheriting spring-boot-starter-parent
in the pom.xml
file we could still create executable Spring Boot jar bu adding a main class in the Maven plugin:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.frontbackend.springboot.GettingStartedApplication</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
We could also point that class in properties section:
<properties>
<start-class>com.frontbackend.springboot.GettingStartedApplication</start-class>
</properties>
Note that if we are using correct annotations and in the pom.xml
file we inherited from spring-boot-starter-parent
we don't need to add main-class related values.
4. Conclusion
In this article, we presented a simple way to build Spring Boot in a runnable JAR file.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}