How I can move a common method to separate files?


(dusiabh) #1

I have multi-project build. Some task are using common part of code:

build.gradle

def doSometing1(var1){
 ...
}
def doSometing2(var1, var2){
   ...
}
def doSometing3(var1, var2, var3){
 ...
}
    task Aufgabe1 << { task ->
 ...
 doSometing1(var1)
 ...
 doSometing3(var1, var2, var3)
}
  task Aufgabe2 << { task ->
 ...
 doSometing3(var1, var2, var3)
 ...
 doSometing2(var1, var2)
}

How I can move a common code/a method to separate files?

moving the methods to separate file and using it like that apply from: “buildmodule.gradle”

doesn’t work:

  • What went wrong: Execution failed for task ‘:proj:Aufgabe1’. > Could not find method doSometing1() for arguments […] on task ‘:proj:Aufgabe1’.

Thank you for your help


(alla.shamis) #2

It’s possible, but there are some tricks. You should change your code like that:

common-methods.gradle

ext.doSometing1 = { String var1 ->
    ...
}
ext.doSometing2 = { String var1, String var2 ->
   ...
}

build.gardle

apply from: "common-methods.gradle"
task Aufgabe2 << { task ->
   def myVar1= project.name
   def myVar2= [projectDir].join()
         doSometing2 (myVar1, myVar2)
 }