9
votes

Being new to both Gradle and Groovy I find myself having a hard time understanding the syntax of a build.gradle script.

I understand (at least I think so) that build.gradle is plain groovy code used as DSL where the keywords are defined elsewhere.

Please explain what the different parts are. Taken from the Tutorial:

defaultTasks 'distribution'

task distribution << {
    println "We build the zip with version=$version"
}

task release(dependsOn: 'distribution') << {
    println 'We release now'
}

gradle.taskGraph.whenReady {taskGraph ->
    if (taskGraph.hasTask(release)) {
        version = '1.0'
    } else {
        version = '1.0-SNAPSHOT'
    }
}

e.g. I think I know println is a function. I know text in quotation marks is a string. I guess stuff in curly braces is a closure. But what is release/distribution? Is it a string, too? Is it a parameter to a function task? And why can I use it in hasTask(release) without quotation marks?

So what exacly is: defaultTasks, task, release, <<, gradle, whenReady, ->?

Bonus: is there a groovy way to find out myself?

2
Jea. It's the same question. Same intention. (Even same confustion about "strings"). Technically the other one is the duplication since it's 8 month younger. But never mind. At least he/she got a helpful answer. - Scheintod

2 Answers

5
votes

Generally, you kind of shouldn't care. It's a DSL in which terms like "parameter to a function task" shouldn't bother you. What you should know is adding a new task is task taskName.

If you really want to dig in (e.g. for extending Gradle, implementing plugins, etc.) Gradle DSL docs are your friends. From there, you can learn that task is a method on Project object.

2
votes

But what is release/distribution? Is it a string, too? Is it a parameter to a function task? And why can I use it in hasTask(release) without quotation marks?

Those are Strings in Gradle, but not in vanilla Groovy. This is mentioned in this response

So what exactly is: 'defaultTasks', 'task', '<<', 'gradle', 'whenReady', '->'?

Those are mostly methods or fields. Fields:

  • project.defaultTasks
  • project.gradle

Methods:

  • project.task()
  • task.whenReady()
  • task.leftShift()

-> is core groovy syntax for closures.