Extracting part of the code from gradle file


(Michal W) #1

Hi,

I need to extend a gradle build file with some new functionality.
I’ve encapsulated the new code in a new class inside the gradle file and use the object in a method called by a a task.

task << {
    doSth()
}

def doSth() {
   ...
   NewFunctionalityClass myClass = new NewFunctionalityClass(var1, var2); 
   myClass.doSthElse();
   ...
}

class NewFunctionalityClass {
  ...
  def doSthElse() {...}
  ...
}

The created class is quite large and I would like to extract it to a new file.
Could you give a hint what’s the best way to do this?

I’ve read about “apply from: ‘other.gradle’”, but as far as I understand it requires creating another task.

Thanks in advance,
Michał


(Chris Doré) #2

The apply from you mentioned should work, it’s like an include.

I think you should also look into creating custom task classes instead of the class and method you have. For example:

class NewFunctionalityClass extends DefaultTask {
    @Input
    def var1
    @Input
    def var2
    @TaskAction
    void doSthElse() {
        println "${var1} ${var2}"
        //...
    }
}
task sayHello(type: NewFunctionalityClass) {
    var1 = 'Hello'
    var2 = 'World!'
}