How do we know what available methods are in Gradle Closures? E.g.:
task greeting {
dependsOn greeting0
doLast {
println("hi")
}
}
In the above closure passed into task greeting
, where do the Gradle specific dependsOn
& doLast
come from?
Both dependsOn
and doLast
are Task
methods. However, if we take a look at the Project doc, the closure is only a simple Groovy Closure.
CodePudding user response:
As an example, here's a Groovy DSL script which shows what's going on (I hope)
class Example {
def missingMethod(String name, args) {
println name
}
static run(Closure cl) {
def project = new Project()
cl.delegate = project
cl.resolveStrategy = Closure.DELEGATE_FIRST
cl.call()
}
}
class Project {
def task(taskDefinition) {
def task = new Task()
taskDefinition[0].delegate = task
taskDefinition[0].resolveStrategy = Closure.DELEGATE_FIRST
taskDefinition[0].call()
}
def and(String what) {
println "Project $what"
}
def methodMissing(String name, args) {
args
}
}
class Task {
def say(String what) {
println "Task $what"
}
}
// So now we can "run" a script
Example.run {
task woo {
say "hello" // From task
and "world" // From project
}
}
This prints
Task hello
Project world