0

I'm very enthusiastic about Groovy, but have just started using it very recently and would like to understand Gradle from language syntax POV as well.

My example is about Gradle task:

task hello(type: GreetingTask)

class GreetingTask extends DefaultTask {
    @TaskAction
    def greet() {
        println 'hello from GreetingTask'
    }
}

task method accepts a String name and a Closure. How can hello(type: GreetingTask) be a String out of a sudden? If type is a named parameter, what does GreetingTask mean? Is it a shortcut for GreetingTask.class? How come hello function/closure returns a String?

yuranos
  • 8,799
  • 9
  • 56
  • 65
  • Gradle build scripts are a DSL, not just pure groovy – tim_yates Sep 20 '17 at 13:08
  • Yes, but it seems to be groovy syntax compliant. Apart from what I'm asking about everything else make sense from groovy syntax POV so far. – yuranos Sep 20 '17 at 14:37
  • I don't know what else to say... Gradle build scripts are a DSL, not just pure groovy... – tim_yates Sep 20 '17 at 14:45
  • 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:48
  • hello can be interpreted as String due to a compiler plugin gradle uses. See answer in https://stackoverflow.com/questions/12326264/ – tkruse Jan 29 '18 at 05:50

1 Answers1

3

As Tim pointed out gradle scripts are written in a specific DSL which is not pure groovy. When it comes to creating tasks here you can see the methods that are used to create a task and under the hood they will be invoked to create the task. Of course under the hood this calls are delegated to TaskContainer, but there's not need to call project.tasks.task because of scopes and mentioned DSL. Now, how it happens that the code you provided creates an instance of task? With this transformer. I know that this answer if far from being sufficient, however hope it helps a bit ;)

type: GreetingTask is an instance of Map (it's equivalent of [type: reetingTask]) and, yes, in groovy .class can be omitted when a class is referred.

Opal
  • 81,889
  • 28
  • 189
  • 210