Java: slow jar task on multi-project build with common dependencies

I have 2 projects (call them ProjectA and ProjectB). Both projects depend on a library jar (call it myJar) and ProjectA depends on ProjectB. I have the following in my build.gradle for ProjectA:

dependencies {

compile group: myGroup, name: myJar, version: latestVersion

compile project(’:ProjectB’) } jar {

from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } }

I have something similar for ProjectB without the project dependency. With this setup, when I run a jar task for Project A, gradle also runs the jar task on ProjectB and myJar gets unpacked and packed twice. This is very slow compared to jar creation in ant. Is there a way to improve my jar creation time? Thanks!

Does ProjectB need compile group: myGroup, name: myJar, version: latestVersion ? If so, try removing it from Project A.

Yes, they both depend on myJar. I have other use cases that are more complex than this and keeping track of duplicate dependencies manually is not preferable.

If so, try removing it from Project A. Project A will inherit this from Project B.

I understand that. As I said before, I have use cases that are more complex and my expectation is that Gradle should be able to recognize when there is this kind of dependency and avoiding unpacking and packing the jar several times. Other build tools like Ant do not have this issue.

Ok. So just to confirm this, the issue is the unpacking of myJar in ProjectA/build/tmp/expandedArchives AND ProjectB/build/tmp/expandedArchives ?


In Gradle, a project dependency gets resolved to a dependency on that project’s Jar (at least if the depended-on project has the ‘java’ plugin applied and you haven’t reconfigured its publications). Hence, Gradle is just doing what you are telling it to. One solution is to only create a fat Jar when needed, e.g. when publishing a Jar, and do it in a separate task (e.g. ‘jarAll’).