Home > Net >  Why is there no easy way to create a fat Jar with Gradle?
Why is there no easy way to create a fat Jar with Gradle?

Time:03-13

I am building a lightweight API client that depends on a few small libraries. I need to package it into a jar to distribute to clients. I am using IntelliJ, Gradle, Kotlin ecosystem.

Gradle offers a task to build a Jar but it doesn't include the dependencies and therefore is useless to me. Isn't building source dependencies more common than just source? Why is there no such task? Seems like most of my google searches point me to self-written Gradle tasks that would bundle dependencies together.

Or is there a different practice on how usually libraries are built?

CodePudding user response:

I am assuming the JAR in question is private by your usage of client.


There is a plugin from the community that is often recommended: https://imperceptiblethoughts.com/shadow/

Gradle also offers a plugin to create a fat/uber JAR, but is more geared towards applications hence the name: Application plugin. -- more on this later

If you intend for the API client JAR you are creating to be used in other clients' build tools such as Maven or Gradle, then you should not create a fat JAR and instead document that in order for clients to include your API client JAR, they will need to provide X, Y, Z dependencies as part of their build process. You will also need to communicate the minimum or maximum supported version your support. This gives the client the flexibility to provide X, Y, Z dependencies from wherever they want. This is where the jar task provided by Java plugin comes into play.

But if you do not want clients to override the version of internal dependencies your client uses, then yes, you will need to build a fat JAR that includes those dependencies which is where the Shadow plugin (linked above) comes into play.

Now back to the Application plugin, underneath, it applies the Distribution plugin which provides the capability to create a fat JAR. However, this is not commonly used for reasons I do not know and instead you will find the aforementioned Shadow plugin used more commonly. This is probably because the Shadow plugin offers more control on how the dependencies are packaged in the fat JAR such as relocating packages.

  • Related