1

I'm currently learning how to use Gradle task API to build java projects.

I understand that

apply plugin: 'java'

is the shorthand syntax for

project.apply(['plugin': 'java'])

I find the full syntax a bit more intuitive and easier to understand

So, what is the FULL syntax for

task helloWorld {
    doLast {
        println("hello world")
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Thor
  • 9,638
  • 15
  • 62
  • 137
  • Related: https://stackoverflow.com/questions/37670201/why-we-dont-need-to-add-quotes-to-the-name-of-gradle-task-when-we-declare-it – tkruse Jan 29 '18 at 05:24
  • Possible duplicate of [Understanding the groovy syntax in a gradle task definition](https://stackoverflow.com/questions/27584463/understanding-the-groovy-syntax-in-a-gradle-task-definition) – tkruse Jan 29 '18 at 05:29

1 Answers1

1

This is eventual shorthand for:

// real syntax is project.task("helloWorld", {...}), but will be excuted like below
project.taskContainer.create("helloWorld").configure({ Task task ->
   task.doLast({ Task it -> 
        println("hello world")
    });
});

So a task is created then configured with closures that will run in given build phases.

In general in Gradle it is often helpful to add the input types to closures, but short of using a debugger it is often difficult to know, the documentation stays mostly silent on that to encourage a "declarative" style usage.

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • 1
    `it` in that situation is the task (see https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/api/internal/AbstractTask.java#L446). And it will just be `println("hello world")`, not `it.println...` as println is not a method on Task – tim_yates Jan 29 '18 at 15:25
  • 1
    @tim_yates: fixed – tkruse Jan 30 '18 at 08:26