2

gradle docs say that the way I declare task in my build.gradle file should be

task greeting(type: GreetingTask) {
    greeting = 'greetings from GreetingTask'
}

can anybody explain to me what happens here in terms of groovy syntax? I know that {} block is the closure and it can be passed as a parameter to the function but I still don't get what's happening here

Ilia
  • 341
  • 3
  • 12
  • 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 07:40

1 Answers1

4

The task keyword is a gradle-specific stuff. It's not standard groovy, but something added by gradle using an AST transformation, in order to make the DSL simpler. It's basically equivalent to

project.tasks.create([name: 'greeting', type: GreetingTask]) { ... }

See https://discuss.gradle.org/t/how-to-translate-task-keyword-in-dsl-into-groovy-call/7243

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • what does it mean when the closure is following function call ?@JB Nizet – Ilia Sep 03 '17 at 15:00
  • 2
    That's standard groovy syntax. It's equivalent to having the closure as last argument. – JB Nizet Sep 03 '17 at 15:01
  • Is there any general way of telling what is gradle-specific and what is groovy-specific without knowing everything about everything first? – topher217 Aug 05 '21 at 06:09