How to Create an Executable JAR with Maven?
The Maven is a powerful build automation tool primarily used for java program projects. And It simplifies that build process, dependency management and project configuration through a standard project object model which means POM file. This file is heart of the Maven Project and Build Automation. In this article we will explain about Create an Executable JAR with Maven. The JAR stands for Java ARchive.
A JAR file is one of the package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution. JAR files are archive files that include a Java-specific manifest file. Creating an executable JAR file with Maven can be done in several ways. Basically the executable JAR file contains a main class with a main method and includes all the dependencies required to run the application.
Create an Executable JAR with Maven
For creating Executable JAR we have some ways below listed them.
- Using the Maven Assembly Plugin.
- Using the Maven Shade Plugin.
- Using the Maven Jar Plugin.
- Using maven Commands.
Tools and Technologies
- Java Programming
- Java Version 17
- Maven 3.X.X
- JUnit
- Spring Tool Suite
Steps to Create an Executable JAR with Maven
Here we created a simple maven project by using spring initializr with required maven dependencies and we mention them in the below.
Step 1:
Create a Maven project by using Spring Tool Suite with basic dependencies.
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
</dependencies>
Step 2:
Once project created successfully, Now observe the project folder structure. The maven tool can provide standard structure to understand the project structure easily.

Step 3:
Now located the pom.xml file open It now observe the dependencies in that file. This file contains information like project configuration, dependency management and other project related information.
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mavenpro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mavenpro</name>
<description>Spring Reactive</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 4:
Now change the pom.xml file for creating executable JAR with maven below we provide the required XML configuration.
Using the Maven Assembly Plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now update the Maven Project after this run this project as Spring Boot App. Now refresh the project folder there is JAR is created.

Step 5:
In this step we use another approach to create an Executable JAR with Maven. Update the below configuration code in the pom.xml and update the project run this project.
Using the Maven Shade Plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.example.MainClass</mainClass>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Step 6:
In this approach we use jar plugin to create an Executable JAR with Maven and update the pom.xml with below provide configuration code and update the project after this run the project.
Using the Maven Jar Plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.MainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Step 7:
In this step we use maven command to Create an Executable JAR with Maven that is
mvn clean package
To Create an Executable JAR with Maven command we need redirect into project folder by using CD command then run the above mention command after this refresh the project folder then you can observe the jar files in the project folder.

