Wednesday, August 17, 2016

Externalize Common Gradle Functions

Content of helpers/common-methods.gradle:

// Define methods as usual
def commonMethod1(param){
    return true
def commonMethod2(param){
    return true

// Export methods by turning them into closures
    commonMethod1 = this.&commonMethod1
    otherNameForMethod2 = this.&commonMethod2
And this is how I use those methods in another script:

// Use double-quotes, otherwise $ won't work
apply from: ""

// You can also use URLs
//apply from: ""

task myBuildTask{    
    def myVar = commonMethod1("parameter1")

No comments: