I found a solution that has been working well for me. The key is that you add a separate subproject for creating the dist. This subproject is sibling to the other subprojects. That is, do not try to script the distribution in your top-level build.gradle file.
Let's call the new subproject dist. The first thing to do is to add it to your top-level settings.gradle file in your multi-project root:
include "subproject1", "subproject2", "subproject3", ....... , "dist"
Your dist project must at minimum include:
build.gradle - to be detailed below
src/main/dist/at_least_one_dummy_file.txt - the distribution plugin always requires a src/main/$distribution.name directory. Having a non-empty one with a distribution.name of main tricks the plugin into following all the transitive dependencies of all the main sourcesets of all the sibling projects.
Next, the build.gradle file for the dist project:
/* Hook in all sibling project jars and their transitive dependencies */
apply plugin: 'java'
dependencies {
compile project(':subproject1')
compile project(':subproject2')
compile project(':subproject3')
. . .
}
/* Distribution */
apply plugin: 'java-library-distribution'
distributions {
main {
baseName = "your-top-level-project-name"
contents {
exclude "dist-${version}.jar"
. . .
}
}
}
Then run gradle distZip. The ZIP file in dist/build/distributions will have a lib subdirectory with every single JAR you want: the sibling project JARs and their transitive dependencies.
Because of the trickery we used, the distribution plugin will make an empty JAR called dist-${version}.jar. For cosmetic reasons, I remove it with the exclude call above, but it is harmless. You could also use a second exclude call to remove the at_least_one_dummy_file.txt if there really is no content under src/main/dist that you want to include. If you do not want add any artifacts and/or remove the ones mentioned here, then you don't even need a contents section at all.
I also have found ways of selectively including different artifacts based on whether this is a "dev" or "prod" distribution, a la Maven profiles. If you want me to add that, please post in comments and I will.