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?
gradle is a Groovy script. Thus it can execute arbitrary code and access any Java library, build-specific Gradle DSL and the Gradle API.
Gradle and Groovy are primarily classified as "Java Build" and "Languages" tools respectively. "Flexibility" is the primary reason why developers consider Gradle over the competitors, whereas "Java platform" was stated as the key factor in picking Groovy. Gradle and Groovy are both open source tools.
The Gradle build language Gradle provides a domain specific language, or DSL, for describing builds. This build language is available in Groovy and Kotlin. A Groovy build script can contain any Groovy language element. A Kotlin build script can contain any Kotlin language element.
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.
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:
Methods:
->
is core groovy syntax for closures.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With