ZetCode

Maven executable JAR

last modified November 8, 2026

In this article we show how to create executable JAR files with Maven. The examples use Maven 3.10, Java 25, and the Maven Assembly plugin 3.8.0.

A JAR file can be started with java -jar app.jar only if its manifest names the main class in the Main-Class attribute, and if all classes the application needs can be found. The JAR built by the default package phase meets neither condition: it has no main class in the manifest and contains only our own classes.

There are two common solutions. A fat JAR (also uber JAR) contains our classes together with the classes of all dependencies, so it can be distributed as a single file. A thin JAR contains only our classes, and its manifest refers to dependency JARs placed next to it. We show both.

The example application

The application uses the Gson library, so it needs a dependency at runtime.

src/main/java/com/example/App.java
package com.example;

import com.google.gson.GsonBuilder;

import java.util.LinkedHashMap;

public class App {

    public static void main(String[] args) {
        var data = new LinkedHashMap<String, Object>();
        data.put("name", "Executable JAR example");
        data.put("version", App.class.getPackage().getImplementationVersion());
        data.put("java", Runtime.version().feature());

        var gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(data));
    }
}

The program prints a few values as pretty-printed JSON. The version is read from the Implementation-Version attribute of the manifest.

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>executable-jar</artifactId>
    <version>1.0.0</version>

    <properties>
        <maven.compiler.release>25</maven.compiler.release>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.15.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.App</mainClass>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </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>
</project>

The Assembly plugin builds the fat JAR in the package phase, after the JAR plugin has built the regular JAR.

<descriptorRef>jar-with-dependencies</descriptorRef>

An assembly descriptor defines what goes into the assembly. The built-in jar-with-dependencies descriptor unpacks our classes and the classes of all runtime dependencies into a single JAR.

<mainClass>com.example.App</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>

The archive element configures the manifest. The mainClass element creates the Main-Class attribute. The addDefaultImplementationEntries option adds the Implementation-Title and Implementation-Version attributes, taken from the project name and version.

<phase>package</phase>
<goals>
    <goal>single</goal>
</goals>

The single goal creates the assembly; the execution binds it to the package phase.

Building the JAR

The package phase now creates two files. The assembly is named after the project with the descriptor ID as a suffix. It is more than a hundred times bigger than the regular JAR, because it contains Gson and its dependencies.

$ mvn package
[INFO] --- jar:3.5.1:jar (default-jar) @ executable-jar ---
[INFO] Building jar: /home/user/app/target/executable-jar-1.0.0.jar
[INFO] --- assembly:3.8.0:single (make-assembly) @ executable-jar ---
[INFO] Building jar: /home/user/app/target/executable-jar-1.0.0-jar-with-dependencies.jar
[INFO] BUILD SUCCESS
...
$ du -h --apparent-size target/*.jar
325K	target/executable-jar-1.0.0-jar-with-dependencies.jar
2.8K	target/executable-jar-1.0.0.jar

Running the JAR

The regular JAR cannot be run; it has no main class. The fat JAR runs without any classpath options. The version comes from the manifest.

$ java -jar target/executable-jar-1.0.0.jar
no main manifest attribute, in target/executable-jar-1.0.0.jar
$ java -jar target/executable-jar-1.0.0-jar-with-dependencies.jar
{
  "name": "Executable JAR example",
  "version": "1.0.0",
  "java": 25
}

Inside the JAR

The manifest contains the attributes configured in the POM. The listing shows our App class next to the Gson classes.

All entries have the same date, February 1, 1980. Since Maven 3.10, builds are reproducible by default: the project.build.outputTimestamp property has a default value, and the archivers use it instead of the current time. Building the same sources twice produces identical JAR files. To store a real date, set the property in the POM, for example <project.build.outputTimestamp>2026-11-08T00:00:00Z</project.build.outputTimestamp>.

$ unzip -p target/executable-jar-1.0.0-jar-with-dependencies.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven Archiver 3.6.5
Java-Version: 25
Build-Jdk-Spec: 25
Implementation-Title: executable-jar
Implementation-Version: 1.0.0
Main-Class: com.example.App

$ unzip -l target/executable-jar-1.0.0-jar-with-dependencies.jar | grep -E ' Name$|example/App.class|gson/Gson.class'
  Length      Date    Time    Name
    19274  1980-02-01 00:00   com/google/gson/Gson.class
     1586  1980-02-01 00:00   com/example/App.class

The assembly ID

The appendAssemblyId option (the assembly.appendAssemblyId user property) controls the -jar-with-dependencies suffix. When it is false, the fat JAR gets the name of the regular JAR and replaces it as the main artifact of the project. The plugin warns about it, because mvn install then installs the fat JAR, and projects that depend on ours get all classes of our dependencies a second time. The finalName parameter sets the base name of the assembly file.

$ mvn package -Dassembly.appendAssemblyId=false
[WARNING] Artifact: com.example:executable-jar:jar:1.0.0 references the same file as the assembly destination file. Moving it to a temporary location for inclusion.
[WARNING] Configuration option 'appendAssemblyId' is set to false.
[WARNING] Replacing pre-existing project main-artifact file: /home/user/app/target/archive-tmp/executable-jar-1.0.0.jar
[INFO] BUILD SUCCESS
...
$ ls target/*.jar
target/executable-jar-1.0.0.jar

Multiple main classes

A project can produce several executable JARs, each with a different main class. We need a custom descriptor for each, because the descriptor ID becomes part of the file name.

src/main/assembly/server.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.2.0 https://maven.apache.org/xsd/assembly-2.2.0.xsd">
    <id>server</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

The descriptor creates a JAR with the ID server. The includeBaseDirectory option is off, so the files are placed at the root of the JAR, not in a directory named after the project. The dependencySet takes the runtime dependencies including the project's own JAR (useProjectArtifact) and unpacks them. The client.xml descriptor is identical except for the ID client.

src/main/java/com/example/ServerApp.java
package com.example;

public class ServerApp {

    public static void main(String[] args) throws InterruptedException {
        System.out.println("Starting server on port 8080");
        Thread.sleep(500);
        System.out.println("Server stopped");
    }
}

The server application only prints two messages.

src/main/java/com/example/ClientApp.java
package com.example;

import com.google.gson.Gson;

import java.util.Map;

public class ClientApp {

    public static void main(String[] args) {
        var request = Map.of("action", "ping");
        System.out.println("Sending " + new Gson().toJson(request) + " to localhost:8080");
    }
}

The client application uses Gson to create a JSON request.

pom.xml (assembly plugin)
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.8.0</version>
    <executions>
        <execution>
            <id>server-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.ServerApp</mainClass>
                    </manifest>
                </archive>
                <descriptors>
                    <descriptor>src/main/assembly/server.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
        <execution>
            <id>client-jar</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.ClientApp</mainClass>
                    </manifest>
                </archive>
                <descriptors>
                    <descriptor>src/main/assembly/client.xml</descriptor>
                </descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

Two executions of the single goal use different descriptors and different main classes. Both JARs contain the same classes; they differ only in the Main-Class attribute.

$ mvn package
[INFO] --- assembly:3.8.0:single (server-jar) @ multi ---
[INFO] Building jar: /home/user/multi/target/multi-1.0.0-server.jar
[INFO] --- assembly:3.8.0:single (client-jar) @ multi ---
[INFO] Building jar: /home/user/multi/target/multi-1.0.0-client.jar
[INFO] BUILD SUCCESS
...
$ java -jar target/multi-1.0.0-server.jar
Starting server on port 8080
Server stopped
$ java -jar target/multi-1.0.0-client.jar
Sending {"action":"ping"} to localhost:8080

A thin JAR with a lib directory

A fat JAR is convenient, but unpacking all dependencies into one archive has drawbacks. Files with the same path in different JARs, such as META-INF/services entries or license files, overwrite each other, and signed JARs lose their signatures. A thin JAR avoids this: the JAR plugin writes the dependency JARs into the Class-Path attribute of the manifest, and the Dependency plugin copies them into a lib directory.

pom.xml (build section)
<build>
    <plugins>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.App</mainClass>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.11.0</version>
            <executions>
                <execution>
                    <id>copy-libs</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

The addClasspath option makes the JAR plugin list the runtime dependencies in the manifest; classpathPrefix puts them into the lib directory. The copy-dependencies goal copies the same dependencies to target/lib. The application is distributed as the JAR together with the lib directory.

$ mvn package
[INFO] --- jar:3.5.1:jar (default-jar) @ thin ---
[INFO] --- dependency:3.11.0:copy-dependencies (copy-libs) @ thin ---
[INFO] BUILD SUCCESS
...
$ ls target/lib
error_prone_annotations-2.50.0.jar
gson-2.15.0.jar
$ unzip -p target/thin-1.0.0.jar META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: Maven JAR Plugin 3.5.1
Java-Version: 25
Build-Jdk-Spec: 25
Class-Path: lib/gson-2.15.0.jar lib/error_prone_annotations-2.50.0.jar
Implementation-Title: thin
Implementation-Version: 1.0.0
Main-Class: com.example.App

$ java -jar target/thin-1.0.0.jar
{
  "name": "Executable JAR example",
  "version": "1.0.0",
  "java": 25
}

The Shade plugin

The Maven Shade plugin is another popular way to build fat JARs. Like the Assembly plugin, it unpacks the dependencies into one JAR, but it has resource transformers that merge files such as service provider configurations instead of overwriting them, and it can relocate packages to avoid conflicts with other copies of the same library. For applications with many dependencies, Shade is usually the better choice; the Assembly plugin is more general and also builds ZIP and TAR distributions.

Source

Apache Maven Assembly Plugin

In this article we have shown how to create executable JAR files with the Maven Assembly plugin and with the JAR and Dependency plugins.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Java tutorials.