Fat Jar or Uber Jar and Shading dependencies

Fat Jar or Uber Jar and Shading dependencies

  1. https://imagej.net/develop/uber-jars
  2. https://stackoverflow.com/questions/19150811/what-is-a-fat-jar/57592130#57592130
  3. https://stackoverflow.com/questions/11947037/what-is-an-uber-jar-file
  4. https://stackoverflow.com/questions/49810578/what-is-a-shaded-jar-file-and-what-is-the-difference-similarities-between-an-ub

Fat JAR or Uber JAR

A jar is a Java archive, basically a single file that typically contains a number of Java class files along with associated metadata and resources.

Über is the German word for above or over (it’s actually cognate with the English over).

Hence, in this context, an uber-jar is an “over-jar”, one level up from a simple JAR, defined as one that contains both your package and all its dependencies in one single JAR file. The name can be thought to come from the same stable as ultrageek, superman, hyperspace, and metadata, which all have similar meanings of “beyond the normal”.

The advantage is that you can distribute your uber-jar and not care at all whether or not dependencies are installed at the destination, as your uber-jar actually has no dependencies.

All the dependencies of your own stuff within the uber-jar are also within that uber-jar. As are all dependencies of those dependencies. And so on.


An uber JAR file is also known as fat JAR, i.e., a JAR file with dependencies.

There are three common methods for constructing an uber JAR file:

  1. Unshaded: Unpack all JAR files, and then repack them into a single JAR file. It works with Java’s default class loader. Tools maven-assembly-plugin
  2. Shaded: Same as unshaded, but rename (i.e., “shade”) all packages of all dependencies. It works with Java’s default class loader. It avoids some (not all) dependency version clashes. Tools maven-shade-plugin
  3. JAR files of JAR files: The final JAR file contains the other JAR files embedded within. It avoids dependency version clashes. All resource files are preserved. Tools: Eclipse JAR File Exporter

An uber JAR is a JAR which contains the contents of multiple JARs (or, less commonly, multiple other JARs themselves)

Your application will almost certainly use other packages and these packages might be provided as JARs. When using Maven these dependencies would be expressed as follows:

<dependency>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
</dependency>

At runtime your application will expect to find the classes contained in this JAR on its classpath.

Rather than shipping each of these dependent JARs along with your application, you could create an uber JAR which contains all of the classes etc from these dependent JARs and then simply run your application from this uber JAR.

Shading

Shading provides a way of creating an uber JAR and renaming the packages which that uber JAR contains. If your uber JAR is likely to be used as a dependency in another application then there’s a risk that the versions of the dependent classes in the uber JAR might clash with versions of those same dependencies in this other application. Shading helps to avoid any such issue by renaming the packages within the uber JAR.

For example:

  1. You create an uber JAR which contains v1.0.0 of the Foo library.
  2. Someone else uses your uber JAR in their application, Bar
  3. The Bar application has its own dependency on Foo but on v1.2.0 of that library.

Now, if there is any clash between versions 1.0.0 and 1.2.0 of Foo we may have a problem because the owner of Bar cannot rely on which one will be loaded so either their code will misbehave or your code - when running within their application - will misbehave.

Shading helps to avoid issues such as this and also allows the provider of Foo to be explicit about the versions of the dependent libraries it uses.

The maven-shade-plugin allows you to (a) create an uber JAR and (b) to shade its contents.

https://maven.apache.org/plugins/maven-shade-plugin/

XHTML
<build>
  <finalName>${project.artifactId}</finalName>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot.experimental</groupId>
          <artifactId>spring-boot-thin-layout</artifactId>
          <version>${wrapper.version}</version>
        </dependency>
      </dependencies>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <configuration>
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <shadedArtifactAttached>true</shadedArtifactAttached>
        <shadedClassifierName>shaded</shadedClassifierName>
      </configuration>
    </plugin>
  </plugins>
</build>

Summary

Creating an uber JAR is a useful technique for simplifying your deployment process.

Shading is an extension to the uber JAR idea which is typically limited to use cases where

  1. The JAR is a library to be used inside another application/library
  2. The authors of the JAR want to be sure that the dependencies used by the JAR are in their control
  3. The authors of the JAR want to avoid ‘version clash’ issues for any applications/libraries using the JAR

Links to this note