How to Create an Executable JAR with Maven?
Last Updated :
17 Jun, 2024
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.
project folderStep 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
<?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.

Similar Reads
How to Execute a .class File in Java?
A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
1 min read
How to create TestNG XML file and Execute TestNG.XML File?
TestNG is a powerful testing framework for Java developers, inspired by JUnit and NUnit. It supports a wide range of testing needs, from unit testing to integration testing. One of the key features of TestNG is the ability to control the execution of tests using an XML file, known as the TestNG XML
4 min read
How to Create a .exe File From a Java Program?
In this article, we will learn how to create a .exe file from a Java program. Java is a platform-independent language, which works on the philosophy of "write Once, Run Anywhere". It simply means that if we write a Java program, we can run it on any device that has a JVM(Java Virtual Machine). Now i
5 min read
How to execute TestNG and Maven without testng.xml file?
To run TestNG tests with Maven, it is not mandatory to configure any testng.xml. Instead of this you just let Maven do the execution with maven-surefire-plugin of the Maven build lifecycle and for that you don't need to define any test suite, classes, or methods in testng.xml. The maven-surefire-plu
2 min read
How to Add JAR file to Classpath in Java?
JAR is an abbreviation of JAVA Archive. It is used for aggregating multiple files into a single one, and it is present in a ZIP format. It can also be used as an archiving tool but the main intention to use this file for development is that the Java applets and their components(.class files) can be
4 min read
How to create a user defined javap tool?
What is a javap tool? The javap tool is used to get the information of any class or interface. The javap command (also known as the Java Disassembler) disassembles one or more class files. Its output depends on the options used ("-c" or "-verbose" for byte code and byte code along with innards info,
4 min read
Creating and Executing a .jar File in Linux Terminal
JAR - Java Archive. It is like a zip file but for java classes. It combines all the .class files in Java into a single .jar file. It is used to download all java classes on HTTP in a single operation. These can be created using the "jar" CLI tool. It also has an optional META-INF which can include f
3 min read
Create a local repository with JGit
JGit is lightweight and it is a Java library implementing the Git version control system. It can allow the developers to perform the Git operators programmatically within Java applications. Making it a powerful tool for integrating Git functionalities into Java projects. PrerequisitesJava Developmen
2 min read
How to Implement a CNN with Deeplearning4j
Convolutional Neural Networks (CNNs) excel at automatically learning and extracting features from images, and this article provides a comprehensive guide on how to implement a CNN using Deeplearning4j (DL4J). Implementing CNN with Deeplearning4j involves setting up the environment, constructing the
6 min read
How to Create a Maven Project in IntelliJ IDEA?
Maven is a powerful project management tool based on POM (project object model). It is used for project build, dependency, and documentation. It simplifies the build process like ANT. But it is too much advanced than ANT. In the short term, we can tell maven is a tool that can build and manage any J
3 min read