Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a shaded jar? And what is the difference/similarities between uber jar and shaded jar? [duplicate]

Can you please help explain what is a shaded jar and how is the maven-shade-plugin useful? Also what is an uber jar.

like image 541
user3478709 Avatar asked Apr 13 '18 06:04

user3478709


People also ask

What's a shaded jar?

Shading is an extension to the uber JAR idea which is typically limited to use cases where. The JAR is a library to be used inside another application/library. The authors of the JAR want to be sure that the dependencies used by the JAR are in their control.

What is an uber jar?

An uber-JAR—also known as a fat JAR or JAR with dependencies—is a JAR file that contains not only a Java program, but embeds its dependencies as well. This means that the JAR functions as an “all-in-one” distribution of the software, without needing any other Java code.

What is shaded jar in Maven?

Together literally it means "jar-over-all-other-jars". "Shading" is the same as "package reallocation" which is needed for classes or resources that collide.

What is uber jar in AEM?

The AEM Uber jar includes all AEM APIs as a single dependency in your Maven project's pom. xml . It is always a best practice to include the Uber Jar as a single dependency instead of including individual AEM API dependencies.


1 Answers

I'll explain what an uber JAR is first because this underpins the shading explanation.

Uber JAR

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.

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

  • The JAR is a library to be used inside another application/library
  • The authors of the JAR want to be sure that the dependencies used by the JAR are in their control
  • The authors of the JAR want to avoid 'version clash' issues for any applications/libraries using the JAR
like image 111
glytching Avatar answered Oct 11 '22 20:10

glytching