Publishing a multi-project build as a library


(Nick Smith) #1

What we want to do is publish a multi module java project as a library.

We have dependencies between modules (these are declared as dependencies in a build.gradle file) and we seem to be successfully publishing a jar file.
However, when we reference this jar as a dependency, gradle moans about not not being able to ‘find’ the subprojects.

So for a structure like this:

water/
  build.gradle
  settings.gradle
  bluewhale/
  krill/ 

water is just a gradle wrapper for bluewhale where some code lives.
krill is a collection of useful methods that bluewhale depends on.
Especially if we were to add greenwhale at some point in the future it makes sense for krill to be a separate (java) module.

In the settings.gradle file I’ll include both of the subprojects
include 'bluewhale', 'krill'

And the build.gradle (roughly)

apply plugin: 'java'
<.......etc.......>
     
project(':bluewhale') {
   dependencies {
      compile project(':krill')
   }

project(':krill') {
   dependencies {
      compile 'some:dep:1.0'
   }

Now, I want to bundle this up as a jar and share it with the world.
Really, I only want to expose bluewhale but I don’t care if I have to publish it as water

Having successfully published water.jar when I reference it in another project gradle moans:

> Could not find water:krill:unspecified.

It seems like the jar is holding on to some reference of a gradle project dependency.

Surely I don’t have to publish every single module separately?
Our project structure has close to 20 modules which we’d probably want to split into 2/3, not 20…
Do we have to put everything into 1 monolith module to publish as a library?

I feel like this is something gradle can handle but I’m baffled as to what I’m doing wrong.
Any pointers would be greatly appreciated!


(Matt Johnson) #2

Check out the shadow plugin: https://github.com/johnrengelman/shadow

It allows you to package dependencies into a single JAR.