Shared master script via "apply from" and use type in child script

(Jan W.) #1


in order to share build logic among various products in our house we started to include the master script from a product specific script via apply from: 'http://url...'.

The different product scripts need to be able to configure the build in some aspects, let’s say here which database distributions needs to be built, MySQL and/or Oracle.
The important aspect for us that the product script can use a type defined in the master script (let’s say the class name is DatabaseBuilds).

Which is the best/easiest way to access the type DatabaseBuilds in our product.script?
Should I write a Plugin that introduces DatabaseBuilds in my product.gradle?
Or should I deploy a jar containing DatabaseBuilds to a repo and declare it as a dependency in product.gradle?


DatabaseBuilds dbBuilds = new DatabaseBuilds()
class DatabaseBuilds{
    Boolean mySQL = true
    Boolean oracle = true

//query dbBuilds.mySQL and in commonly used tasks


apply from: 'url/to/master.gradle'
DatabaseBuilds dbBuilds = new DatabaseBuilds() // not possible at the moment

Thanks in advance

(Stefan Oehme) #2

That’s the best way to do this, yes. Writing a plugin instead of using apply from:. It also gives you the opportunity to properly test it, which is not possible with simple scripts.

(Jan W.) #3

Thanks for the hint.
So I’m now on my way to migrate my master script that already contains some common build logic to a Plugin.
In my master script I imported an ant file using some external ant-task which I made available to the classloader in my master script as follows:

    maven {
        url 'http://ourArtifactory/artifactory/ourRepo'


    antConf group: 'org.apache.ivy', name: 'ivy', version: '2.4.0'
    antConf group: 'ant-contrib', name: 'ant-contrib', version: '1.0b3'
    // some more dependencies...

ClassLoader antClassLoader =
configurations.antConf.each { File f ->

This accomplished the declaration of the repo and resolving of antConf’s dependencies.
It came so handy to do it via (master) script, and I am somehow reluctant to migrate all the script code in groovy now…
Any ideas for an easier solution?


(Stefan Oehme) #4

Your script is a plugin and valid Groovy code. There is no difference in syntax and you can paste it 1:1 into the apply method of your plugin class.

(Jan W.) #5

Great, that worked well!
Just had to use “project.” in some cases as qualifier.
Stefan, thanks a lot

(Stefan Oehme) #6

You can also use ´project.with { code here }´ as the top level block in the apply method to get exactly the same behavior as in a script. The project is then the implicit receiver just like in a script.