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?
master.gradle:
DatabaseBuilds dbBuilds = new DatabaseBuilds()
class DatabaseBuilds{
Boolean mySQL = true
Boolean oracle = true
}
//query dbBuilds.mySQL and dbBuilds.oracle in commonly used tasks
product.gradle:
apply from: 'url/to/master.gradle'
DatabaseBuilds dbBuilds = new DatabaseBuilds() // not possible at the moment
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.
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:
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?
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.