Gradle multi project distribution

Sason Ohanian picture Sason Ohanian · Jul 17, 2013 · Viewed 8.6k times · Source

I'm trying to make a dist of a multi project build. The root project looks something like this:

apply plugin: 'distribution'
version 1.0
distributions {
    main {
        baseName = 'someName'
        contents {
            from 'nodes' 
        into 'nodes'
        }
    }
}

It just copies a folder with some files to the dist.

I now want each subproject to inject its stuff into the dist. I want to add each subprojects jar, any dependecies, and possibly some other files etc...

I have no idea how to inject from the subproject to the root. Should I even do something like that? What i mean is something like this:

subprojects {
   apply java...
   ...

   // pseudocode
   jack into the root project dist plugin
   put my produced jars and dependencies in a folder with my name
   ...
}

Does anyone have any examples, or just point me in the right direction?

thanks!

Answer

pvdissel picture pvdissel · Sep 21, 2013

I was looking for the same thing. With some peeking at the API docs and Gradle' own build files, I came to the following:

apply plugin: 'distribution'

distributions {
    main {
        contents {
            into('bin') {
                from { project(':subproject1').startScripts.outputs.files }
                from { project(':subproject2').startScripts.outputs.files }
                fileMode = 0755
            }
            into('lib') {
                def libs = []
                libs << project(':subproject1').configurations.runtime - project(':runner').configurations.runtime
                libs << project(':subproject2').configurations.runtime
                from libs
                from project(':subproject1').jar
                from project(':subproject2').jar
            }
        }
    }
}

The contents {} closure is a CopySpec, knowing that makes using the distribution plugin way simpler :)

Check out Gradle' own subprojects/distributions/distributions.gradle file for some great examples of using the CopySpec.

This works.

  • The substraction is to remove duplicated jars.
  • The ".jar" lines are to add the jar of that project as the configurations.runtime only seem to contain the dependenceis.

Sadly, currently I've no clue on how to scale this to more than two projects in a clean way. Atleast we're one step closer :)