Create executable JAR with Maven
Table of Contents
This short post show step-by-step how to create executable JAR. Thanks to Maven and Shade plugin it is really easy to achieve this.
Example project
Our project structure looks like this:
> tree
.
├── pom.xml (Maven configuration)
├── readme.md
├── src
│ └── main
│ └── java
│ └── codes
│ └── hubertwo
│ └── maven
│ └── executablejar
│ └── App.java (Application entrypoint)
└── target
└── maven-executablejar-1.0-SNAPSHOT.jar (Executable JAR)
The project contains single pom.xml
file and App.java
file with entrypoint to our application. At the end of this
post we will have executable JAR in target
folder.
What do we need
To create executable JAR we will need entry point (class with main method) and proper Maven configuration.
Entrypoint - class with main method
The entrypoint is just simple Java main
method.
public class App {
public static void main(String[] args) {
System.out.println("Hello from app!");
}
}
Executable JAR - configure Maven to create package
To create executable JAR we will use Maven Shade plugin. Let’s open ‘pom.xml’ and add plugin definition. Once we do that we can create executable JAR.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- Entrypoint for executable JAR -->
<mainClass>codes.hubertwo.maven.executablejar.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
As you may see the plugin will be executed in package
phase. Second thing to notice is
the ManifestResourceTransformer
where we define location of the entrypoint to our application.
Build executable JAR
It’s time to package our code. To do so we will use mvn package
command.
mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -------< codes.hubertwo.maven.executablejar:maven-executablejar >-------
[INFO] Building maven-executablejar 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
...
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-executablejar ---
[INFO] Building jar: target/maven-executablejar-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:3.2.4:shade (default) @ maven-executablejar ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing target/maven-executablejar-1.0-SNAPSHOT.jar with target/maven-executablejar-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Take a look on shade plugin logs. It replaced the JAR built by JAR plugin with modified one.
Run JAR
Now when we created the JAR let’s run it.
hubert@mac maven-executable-jar % java -jar target/maven-executablejar-1.0-SNAPSHOT.jar
Hello from app!
Looks like everything works as expected.
TL;DR - Maven configuration
You can find the link to repository with example code at the bottom of this page. However, if you are just looking for ready to use solution, you can find full configuration below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- Entrypoint for executable JAR -->
<mainClass>codes.hubertwo.maven.executablejar.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Summary
In this short post we learned how to create executable JAR with Maven and then run the JAR from console.
Source code
All code samples and Maven project described in this post is available on GitHub
Create executable JAR with Maven.
If you found this post useful do not forget to leave a ⭐️ on GitHub :) Thanks!