How i can do use utils gradles script in the main build.gradle


(marcomsousa) #1

I want to have some files utils files like:

==files:== build.gradle

utils\a.gradle
utils\b.gradle

==a.gradle==

def a() {
 println "a"
}

==b.gradle==

def b() {
 println "b"
}

==build.gradle==

def ba() {
  a()
  b()
}
$gradle -q ba
a
b

How can I do this?


(Peter Niederwieser) #2

You can’t call methods from the command line (as in ‘gradle ba’), only tasks. To call the helper methods from the main script, you’ll have to include the utility scripts (‘apply from: “utils/a.gradle”’) and turn the methods into extra properties holding closures (‘ext.a = { println “a” }’).


(marcomsousa) #3

Thank you Peter.

==files:==

build.gradle utils\a.gradle utils\b.gradle

==a.gradle==

def a(String text) {
 println text
}

==b.gradle==

def b(String text) {
 println text
}

==build.gradle==

apply from: "utils/a.gradle"
apply from: "utils/b.gradle"
task ba() {
  a("text a")
  b("text b")
}
$gradle -q ba
text a
text b

can I do this? Or is better I have a groovy utils class?