How to build executable JAR with Maven in Spring Boot


In this tutorial, we will learn how to build executable JAR with Maven in Spring Boot application. Spring Boot provides spring-boot-maven-plugin to create or build an executable JAR of your Spring Boot application.

Follow the below steps to build executable JAR:

Step 1: Go to your Spring Boot application and open the pom.xml file.

Step 2: Mention the packing type in the pom.xml file

<packaging>jar</packaging>

and add the below spring-boot-maven-plugin code just below the closing dependencies </dependencies> tag.

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

Step 3: Now run the mvn clean package command. If you are using STS/Eclipse IDE, then Right Click on your project » Run As » Maven build… » Goals: clean package » Run.

Step 4: Step 3 will create an executable JAR file of your Spring Boot application and put it within the target folder.

Step 5: Run the executable JAR, using the following Java command.

java -jar target/<your-jar-name>.jar

For your reference, I have attached my complete pom.xml file.

pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>org.websparrow</groupId>
	<artifactId>springboot-exe-jar</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-exe-jar</name>

	<!-- define the packaging type -->
	<packaging>jar</packaging>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<!-- Spring boot maven plugin to create executable JAR -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

References

  1. Creating an Executable Jar
  2. Spring Boot Maven Plugin

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.