Extracting part of the code from gradle file

(Michal W) #1


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 << {

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

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,

(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 {
    def var1
    def var2
    void doSthElse() {
        println "${var1} ${var2}"
task sayHello(type: NewFunctionalityClass) {
    var1 = 'Hello'
    var2 = 'World!'