Simple way to create "uber" or "fat" jar for multi-project build

The Gradle documentation gives a solution for creating a fat jar by using:

from configurations.runtimeClasspath.findAll {'jar') }.collect { zipTree(it) }

This works well for a single module gradle build. It does not work properly for multi-module gradle projects where one project depends on another. What I’ve experienced is that the jar task can be executed before dependent jars are created, which causes the jar task to fail since it cannot access dependencies.

I think this occurs because while the project is dependent on another project, it is not dependent on the other project’s jar artifact. So it will work sometimes, but sometimes it can fail due to missing jars. I can get it to work by manually listing all the dependent jar tasks (dependsOn project(':other-proj').jar) but this does not scale well for larger builds.

Is there any easy way to get this to work properly with multi-project builds? I know I can use the shadow plugin but I prefer to keep it simple and avoid extra plugins unless absolutely necessary. It’s also harder to replace the default artifact with a fat jar using the shadow plugin, so it’s not my first choice.

1 Like