Gradle and Shadow Jar

plugins

(Ihor Mochurad) #1

I am new to Gradle and ShadowJar. I am building an api project that is internally using a dropwizard framework. It’s a Java project. There are a bunch of dependencies that have same files under META-INF/services/. When my application loads it fails to properly configure one of the auto-discoverable services and fails miserably. I learned that shadowJar can be of help in this situation. Now I’m struggling to make some sense out of how it can be integrated. Here is what I have in my build.gradle file:

jar {
    zip64 true
    from { configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) } }
    manifest {
        attributes(
        'Main-Class': mainClassName,
        'version': project.version,
        'lastBuild': buildTime()
        )
    }
}

    shadowJar {
        mergeServiceFiles()
    }

I build my project in the console by specifying: ./gradlew assemble
As the gradle outputs info while building, it doesn’t indicate invoking of the shadowJar task. It shows calling jar task, and I’d like assemble to invoke shadowJar.
When I invoke ./gradlew shadowJar it builds a fat shadowed jar with merged service files (however, this process also merges other duplicates, such as META-INF/LICENSE.txt and javax/annotation/CheckForNull.class. In the original fat jar I see multiple instances of these classes, whereas in the shadowed jar there is only one instance per unique class. I do not know if this is normal defaulted behaviour or not).
Question: Is there a way to invoke shadowJar task automatically when running assemble? And what should be done for it?


(Marco Vermeulen) #2

Hi @imochurad,

This plugin is not maintained by Gradle and is provided by John Engelman. I suggest that the first place to look would be on the plugin’s Github Page. The plugin has a very comprehensive user guide which explains all the details about how to use it, as well as it’s expected behaviour.

Regarding the final question, you can simply add the following line to your build script to make the assemble task depend on shadowJar:

assemble.dependsOn shadowJar

Hope this helps!


(Ihor Mochurad) #3

Thanks for your reply. Yes, I have touch based with the author on the GitHub page, got my answer.
In order to use shadowJar task instead of jar, I had to disable jar task by setting enabled to false and introducing dependsOn to assemble.
This thread can be closed.


(Marco Vermeulen) #4