[GRADLE-566] fatJar option for building jar-with-dependencies Created: 23/Jul/09  Updated: 10/Feb/17  Resolved: 10/Feb/17

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: None

Type: New Feature
Reporter: Szczepan Faber Assignee: Unassigned
Resolution: Won't Fix Votes: 5


<shameless copy-paste from the mailing list>

I think I'm missing the concept of 'fat jar' in Gradle. Many times
when I worked on a library/console app I needed to stick all
dependencies in single jar. It would be great if java plugin added
extra task that could produce fat jar (or mavenized:
jar-with-dependencies) out of the box.

It would be easy to provide such a fatJar task for the default jar task. This would be the framework way of doing things (like Maven does).

But it would be much cooler, if there were the pattern fat<JarName> which works for any jar that is related to source directories. To make this possible we need a richer abstraction for source directories which we plan to do for 0.8. If we have such source dir objects you could also define a jar like:

task mySrcJar {
add srcDir // srcDir is not String but a rich object, which knows all of the srcDir including its classpath
fat = true

This would work with any project layout, not just with single src root projects. And it would enable to provide a rule for a pattern like above.

Toolset over Framework rules

<you bet!>

Comment by Tomek Kaczanowski [ 24/Sep/09 ]

Hi Szczepan,

Thomas Kinnen was kind to provide a solution for fat-jar creation - see http://docs.codehaus.org/display/GRADLE/Cookbook#Cookbook-Creatingafatjar

It is not a fully-fledged solution, but can be sufficient in some cases.


Comment by Hans Dockter [ 20/Jan/10 ]

We are kind of conservative to making our API bigger. We want to be really sure that its worth it. With the newest Gradle there is now a fantastic way to 'stage' potential API enhancement candidates. Any script can now be a plugin. Those scripts can live on your local machine or remotely.

I have added a fatjar plugin script. You can say now (with the latest Gradle):

apply url: 'http://github.com/hansd/huglins/raw/master/fatjar.gradle'

jar {
   merge configurations.runtime

task otherJar {
   merge files(fileTree('lib').matching { include '**/.jar' })
Comment by Ben McCann [ 25/Jan/11 ]

I would very much like to see something along the lines of the Maven Assembly Plugin's jar-with-dependencies added.

Hans, I attempted to use the plugin you posted and recieved:
No such property: url for class: org.gradle.api.internal.plugins.DefaultObjectConfigurationAction

Comment by Ben McCann [ 25/Jan/11 ]

I tried the following and it seemed to load the script though it looks like there are problems with the script still:
apply from: 'https://github.com/hansd/huglins/raw/master/fatjar.gradle'

Comment by Robert Fischer [ 25/Jan/11 ]

BTW, I've got a OneJar plugin: https://github.com/RobertFischer/gradle-plugins

Comment by Ben McCann [ 25/Jan/11 ]

Thanks, Robert. I'm anxious to try it out. I'm getting the error message below right now:
Cause: Could not find property 'com' on project

I have a multi-maven project. I've attached the build.gradle file from the parent project where I included the plugin in case you see anything obvious that I did wrong (which is likely since I'm still new to gradle :o)


Comment by Ben McCann [ 25/Jan/11 ]

I updated my build script, but still cannot get Robert's plugin to work. I didn't realize the first time around that buildscript was a reserved word in gradle. I'm still getting the same error:
Caused by: groovy.lang.MissingPropertyException: Could not find property 'com' on project

Comment by Ben McCann [ 25/Jan/11 ]

Finally got it figured out. I left "Plugin" off the plugin name. I filed a new bug (http://jira.codehaus.org/browse/GRADLE-1352) asking for Gradle to give back a better error message in this situation. I'll try out the plugin now and let you know how it works. Thanks!

Comment by Ben McCann [ 25/Jan/11 ]

I'm sorry, I still can't figure this out. Robert, could you post an example of how to use the plugin? I have it installed now, but am not sure how to call it.

Comment by Peter Niederwieser [ 26/Jan/11 ]

Have you tried gradle oneJar?

Comment by Ben McCann [ 26/Jan/11 ]

Yep, no luck.

$ gradle oneJar
:unpackOneJar SKIPPED
:java/com/benmccann/test/db:compileJava UP-TO-DATE
:java/com/benmccann/test/db:processResources UP-TO-DATE
:java/com/benmccann/test/db:classes UP-TO-DATE
:java/com/benmccann/test/db:jar UP-TO-DATE
:java/com/benmccann/test/backend:compileJava UP-TO-DATE
:java/com/benmccann/test/backend:processResources UP-TO-DATE
:java/com/benmccann/test/backend:classes UP-TO-DATE
:java/com/benmccann/test/backend:jar UP-TO-DATE

FAILURE: Build failed with an exception.

  • Where:
    Build file '/home/bmccann/workspace/test/java/com/benmccann/test/backend/build.gradle'
  • What went wrong:
    Execution failed for task ':java/com/benmccann/test/backend:oneJar'.
    Cause: Problem: failed to create task or type one-jar
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any <presetdef>/<macrodef> declarations have taken place.
Comment by Peter Niederwieser [ 26/Jan/11 ]

Please provide such information upfront rather than letting us guess what the problem might be.

I can confirm that the OneJar plugin doesn't work for multi-project builds, at least not with Gradle 0.9.2.

Comment by Ben McCann [ 26/Jan/11 ]

Thanks. I wasn't sure that "gradle oneJar" was the correct invocation since there were no docs. I filed a bug requesting that it be clarified: https://github.com/RobertFischer/gradle-plugins/issues/issue/3

I also filed a bug for the fact that it's broken on multi-project builds:

Comment by Robert Fischer [ 26/Jan/11 ]

I'm on it.

Comment by Ben McCann [ 26/Jan/11 ]

Awesome. Thanks so much for being super responsive and providing this plugin. I'll be really excited to use it.

Comment by Ben McCann [ 01/Feb/11 ]

It looks like the Gradle Cookbook also has a tip on how to do this, but it doesn't seem to work, so I filed a new bug for that:

Comment by Ravi Teja [ 25/Jul/12 ]

I created a similar sample project using uberjar example but anyway need this feature.

Code: https://github.com/bond-/gradle-groovy-jar-example/blob/master/build.gradle
Example: http://stackoverflow.com/questions/9749032/create-a-groovy-executable-jar-with-gradle

Comment by Diwaker Gupta [ 18/Sep/12 ]

I think gradle should provide a standard plugin (or even in core) for this functionality. While there are many "quick" plugins, scripts, snippets and Cookbook recipes to build a fat jar out there, none of them work out of the box for me. Building a working fat jar requires numerous (potential) steps, including but not limited to:

  • excluding 'META-INF/.RSA', 'META-INF/.DSA' etc. to avoid signing issues when building executable jars
  • combining META-INF/services files to avoid failures when applications use the ServiceLoader framework (easy to hit when you build an app that supports multiple JDBC drivers, for instance)
  • combining NOTICE files, LICENSE files and other useful transformations

Basically, a plugin that can match maven shade/assembly in features

Comment by Benjamin Muschko [ 15/Nov/16 ]

As announced on the Gradle blog we are planning to completely migrate issues from JIRA to GitHub.

We intend to prioritize issues that are actionable and impactful while working more closely with the community. Many of our JIRA issues are inactionable or irrelevant. We would like to request your help to ensure we can appropriately prioritize JIRA issues you’ve contributed to.

Please confirm that you still advocate for your JIRA issue before December 10th, 2016 by:

  • Checking that your issues contain requisite context, impact, behaviors, and examples as described in our published guidelines.
  • Leave a comment on the JIRA issue or open a new GitHub issue confirming that the above is complete.

We look forward to collaborating with you more closely on GitHub. Thank you for your contribution to Gradle!

Comment by Benjamin Muschko [ 10/Feb/17 ]

Thanks again for reporting this issue. We haven't heard back from you after our inquiry from November 15th. We are closing this issue now. Please create an issue on GitHub if you still feel passionate about getting it resolved.

Generated at Wed Jun 30 11:33:17 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.