TExcelTAD
Home › Java Builds: Maven & Gradle › Fat JARs with Shade, Running with exec

Fat JARs with Shade, Running with exec

mvn package produces a JAR with your classes only. To run it you need its dependencies on the classpath too, which is fine inside an IDE or a container image built with copy-dependencies, but awkward when you want to hand someone one file. Two plugins cover the everyday needs: exec runs a main class with the project's classpath, and shade builds an executable "uber" JAR that contains the dependencies.

The plain JAR is not runnable

bash · try it yourself
mkdir -p tool/src/main/java/demo && cd tool
cat > src/main/java/demo/Tool.java <<'SRC'
package demo;

import org.apache.commons.lang3.StringUtils;

public class Tool {
    public static void main(String[] args) {
        String who = args.length > 0 ? args[0] : "world";
        System.out.println(StringUtils.center(" hello " + who + " ", 30, '*'));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>tool</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
  </dependencies>
</project>
POM
mvn -q package
java -jar target/tool-1.0.jar
java -cp target/tool-1.0.jar demo.Tool 2>&1 | head -1
no main manifest attribute, in target/tool-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

The JAR has no Main-Class in its manifest, and even with the class named explicitly, Commons Lang is missing at run time.

exec: run with the project's classpath

exec-maven-plugin (from the MojoHaus project, group org.codehaus.mojo) has two goals: exec:java runs a main class inside Maven's JVM with the runtime classpath, exec:exec starts any external program. Configure the main class once in the POM:

bash · try it yourself
mkdir -p tool/src/main/java/demo && cd tool
cat > src/main/java/demo/Tool.java <<'SRC'
package demo;

import org.apache.commons.lang3.StringUtils;

public class Tool {
    public static void main(String[] args) {
        String who = args.length > 0 ? args[0] : "world";
        System.out.println(StringUtils.center(" hello " + who + " ", 30, '*'));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>tool</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.6.4</version>
        <configuration>
          <mainClass>demo.Tool</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
POM
mvn -q compile exec:java
mvn -q compile exec:java -Dexec.args="maven"
mvn compile exec:java | grep -- '--- '
******** hello world *********
******** hello maven *********
[INFO] --- resources:3.4.0:resources (default-resources) @ tool ---
[INFO] --- compiler:3.15.0:compile (default-compile) @ tool ---
[INFO] --- exec:3.6.4:java (default-cli) @ tool ---

Note compile exec:java: exec doesn't compile anything, so put the phase in front of the goal. -Dexec.args passes program arguments. exec is for developers and scripts; it isn't how you ship.

shade: an executable uber JAR

The shade plugin runs at package, after jar:jar. It unpacks your JAR and every runtime dependency into one new JAR, which replaces the original (the original is kept as original-tool-1.0.jar). A transformer writes the Main-Class manifest entry:

bash · try it yourself
mkdir -p tool/src/main/java/demo && cd tool
cat > src/main/java/demo/Tool.java <<'SRC'
package demo;

import org.apache.commons.lang3.StringUtils;

public class Tool {
    public static void main(String[] args) {
        String who = args.length > 0 ? args[0] : "world";
        System.out.println(StringUtils.center(" hello " + who + " ", 30, '*'));
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>tool</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.6.2</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals><goal>shade</goal></goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>demo.Tool</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>
POM
mvn package | sed -n '/--- shade/,/^\[INFO\] ---/p' | grep -v -- '^\[INFO\] ---'
ls target/*.jar
java -jar target/tool-1.0.jar shade
echo "classes: original $(unzip -l target/original-tool-1.0.jar | grep -c '\.class
  

), shaded $(unzip -l target/tool-1.0.jar | grep -c '\.class
  

)"
[INFO] Dependency-reduced POM written at: /home/learner/tool/dependency-reduced-pom.xml
[WARNING] commons-lang3-3.21.0.jar, tool-1.0.jar define 1 overlapping resource:
[WARNING]   - META-INF/MANIFEST.MF
[WARNING] maven-shade-plugin has detected that some files are
[WARNING] present in two or more JARs. When this happens, only one
[WARNING] single version of the file is copied to the uber jar.
[WARNING] Usually this is not harmful and you can skip these warnings,
[WARNING] otherwise try to manually exclude artifacts based on
[WARNING] mvn dependency:tree -Ddetail=true and the above output.
[WARNING] See https://maven.apache.org/plugins/maven-shade-plugin/
[INFO] Replacing original artifact with shaded artifact.
target/original-tool-1.0.jar
target/tool-1.0.jar
******** hello shade *********
classes: original 1, shaded 441

Read the plugin's output:

  • Shade doesn't list the artifacts it merges at the default log level; the class count at the end shows the effect: 1 class of ours, 440 from Commons Lang. Only runtime-classpath dependencies go in; test and provided ones never do.
  • The overlapping resources warning is normal: both JARs have a META-INF/MANIFEST.MF, and only one copy can survive (the transformer writes a new one anyway). The same mechanism silently drops files that matter, such as META-INF/services/* registrations when two libraries provide the same service; the ServicesResourceTransformer merges those instead of keeping one.
  • Dependency-reduced POM written: shade writes a POM without the dependencies it swallowed, so a consumer of the uber JAR doesn't get them twice.

Relocation: shading for real

The word "shade" comes from relocation: rewriting the package names of bundled classes, so your bundled copy of a library can't clash with another version on the user's classpath:

xml
<configuration>
  <relocations>
    <relocation>
      <pattern>org.apache.commons.lang3</pattern>
      <shadedPattern>demo.shaded.lang3</shadedPattern>
    </relocation>
  </relocations>
</configuration>

Shade rewrites both the bundled classes and every reference to them in your bytecode. Libraries that are used inside other people's applications (Maven plugins, JDBC drivers, agents) relocate their dependencies; applications rarely need to.

Tip: An uber JAR isn't the only way to ship. Container images usually layer the dependencies separately from your code (copy-dependencies or Jib), which makes rebuilds faster; Spring Boot has its own nested-JAR format. Use shade for command-line tools and simple deployments.

Test yourself

Why does java -jar target/app-1.0.jar fail on a JAR built by plain mvn package?

  1. The JAR is corrupt
  2. The manifest has no Main-Class, and dependencies aren't inside the JAR
  3. Maven JARs need Maven to run
  4. Tests were skipped

What does mvn compile exec:java do?

  1. Builds an uber JAR
  2. Compiles, then runs the configured main class inside Maven's JVM with the project's runtime classpath
  3. Deploys the app
  4. Runs the tests

What is relocation in the shade plugin?

  1. Moving the JAR to another folder
  2. Rewriting package names of bundled classes (and references to them) to avoid clashes
  3. Uploading the JAR
  4. Sorting the JAR entries