Spring boot allows to create executable jar files using maven by configuring in the pom.xml file. The maven package command builds the spring boot application and creates executable and non-executable jar files in the target folder. The executable jar can be launched with a java command without the need for the spring boot framework or maven.
The steps below will show you how to use maven to create an executable jar file and a non-executable jar file in spring boot application.
Step 1: Configure packaging as jar
The packaging tag in the pom.xml file informs Maven how to create the spring boot application. The types of packaging are jar, war, ear, ejb, pom and maven plugin. The packaging should be set up as a jar. The default packaging value is jar.
Step 2: Configure maven plugin
The spring boot maven plugin should be added to the plugins list in the pom.xml file. The spring boot maven plugin contains the dependent jars that generate the executable and non-executable jars for the spring boot application.
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin>
Step 3: Configure jar file name
The artifactId and version configuration will be used to generate the name of the executable jar file. The spring boot application name is the default value for the artifactId. The name of the executable jar file will be used as the artifactId. In the jar file name, the version is placed after the artifactId. A spring boot application’s default version is SNAPSHOT.
The executable jar file name is SpringBootExecutableJar-0.0.1-SNAPSHOT.jar. The non-executable jar file name is SpringBootExecutableJar-0.0.1-SNAPSHOT.jar.original.
Step 4: Creating executable jar using maven
The maven package command may be used to generate the spring boot application executable jar. The maven package command compiles the source code, runs all of the test cases, and then creates the non-executable jar file. By include all of the required jar files, Maven will create the executable jar from the non-executable jar.
The executable and non-executable jars will be created in the target folder of the spring boot project.
mvn package or mvn clean package
Step 5: Creating executable jar using STS
You may create executable and non-executable jars using the Spring Tool Suite UI. The following steps will walk you through the process of creating jars in the STS UI.
Select the spring boot project name Right Click on the project name Click "Run As -> Maven Build" in the popup A new Popup window will open enter "package" in the Goals input box Click "Run" button in the bottom right side of the popup window
Step 6: Check in target folder
The executable jar file SpringBootExecutableJar-0.0.1-SNAPSHOT.jar and the non-executable jar file SpringBootExecutableJar-0.0.1-SNAPSHOT.jar.original are created in the target folder in the spring boot project directory.
Step 7: Executing jar file
The java command can be used to execute the jar files produced by the spring boot application. To launch the executable jar, open a terminal or command prompt and type the following java command.
project_directory:> java -jar target/SpringBootExecutableJar-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.5.5) 2021-10-05 12:13:18.836 INFO 98379 --- [ main] c.y.SpringBootExecutableJarApplication : Starting SpringBootExecutableJarApplication v0.0.1-SNAPSHOT using Java 11.0.9 on yawin with PID 98379 (/Users/yawin/STS/workspace/SpringBootExecutableJar/target/SpringBootExecutableJar-0.0.1-SNAPSHOT.jar started by knatarajan2 in /Users/yawin/STS/workspace/SpringBootExecutableJar) 2021-10-05 12:13:18.838 INFO 98379 --- [ main] c.y.SpringBootExecutableJarApplication : No active profile set, falling back to default profiles: default 2021-10-05 12:13:19.326 INFO 98379 --- [ main] c.y.SpringBootExecutableJarApplication : Started SpringBootExecutableJarApplication in 2.998 seconds (JVM running for 3.503) Spring boot project is executing from executable jar