Task Creation Syntax Clarification

The following is from the User Guide :

task(hello) << {
    println "hello"
}
  task(copy, type: Copy) {
    from(file('srcDir'))
    into(buildDir)
}

I’m confused as to how this works. Is “task” a keyword, a method invocation, etc.?

I thought they were method invocations on the Project object, but the API for that class doesn’t show any matching signatures . . .

It is a method invocation. http://www.gradle.org/docs/current/javadoc/org/gradle/api/Project.html#task(java.util.Map,%20java.lang.String)

the left-shift (<<) operator invokes the “doLast” method on the task object.

So, how is ‘hello’ a string? That method takes a string; that’s part of what I don’t quite understand.

And, then the second one?

The following DSL syntax…

task copy(type: Copy) {
    from srcDir
    into buildDir
}

…is rewritten to…

project.task("copy", type: Copy) {
    from srcDir
    into buildDir
}

…by way of a Groovy compiler plugin (AST transform).

Thanks Peter! I guess I’m sometimes confused because as a groovy noob and a gradle noob, it’s sometimes hard to tell whether the syntax is coming from groovy or the gradle DSL.

Thanks Peter! I guess I’m sometimes confused because as a groovy noob and a gradle noob, it’s sometimes hard to tell whether the syntax is coming from groovy or the gradle DSL.