9

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?

ulidtko
  • 14,740
  • 10
  • 56
  • 88
Scheintod
  • 7,953
  • 9
  • 42
  • 61
  • 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:39
  • 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 Feb 19 '20 at 12:35

2 Answers2

5

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.

JBaruch
  • 22,610
  • 5
  • 62
  • 90
  • 1
    Technically, the Gradle build language is a domain specific language (DSL) implemented using Groovy compile-time and runtime meta-programming. You can learn how to use the DSL (but not how it is implemented) in the [Gradle Build Language Reference](http://gradle.org/docs/current/dsl/index.html). – Peter Niederwieser Apr 02 '14 at 09:17
  • 3
    Thanks for the quick answer. But I regard "don't care" not satisfying. I think understanding the underlying principles is fundamental in understanding how to use something. At least from a programmers standpoint. E.g. I don't want to remember that I have to use quotes *here* but not *there*, but I want to know that *this stuff* is a function declaration and therefor don't need them while *that stuff* takes a string argument while *that other stuff* takes a string but automatically converts ... and so on. – Scheintod Apr 02 '14 at 09:27
2

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.

tkruse
  • 10,222
  • 7
  • 53
  • 80