Shared Gradle Logic


(mmichalek) #1

Hi, is it possible to have a shared gradle script which can be imported into another script? I’m looking for the ability to import methods and such into a script and potentially override those methods with local implementations.

It looks like this is possible with a multi-project build, but I’m looking to factor out logic that applies across unrelated projects.

I’ve also tried

apply from: 'other.gradle'

, but I’m not able to call methods in the other.gradle script.


(mmichalek) #2

I was able to get the effect I was looking for by writing closures instead of methods. (based on the tip at http://gradle.1045684.n5.nabble.com/Understanding-apply-from-lt-file-gt-td4684988.html)

So changing from this:

String timestampAsString() {
    new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())
}

To this, worked:

timestampAsString = { ->
     new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())
}