Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle / Groovy Syntax and keywords

Tags:

gradle

groovy

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?

like image 388
Scheintod Avatar asked Apr 02 '14 08:04

Scheintod


People also ask

Is Gradle written in Groovy?

gradle is a Groovy script. Thus it can execute arbitrary code and access any Java library, build-specific Gradle DSL and the Gradle API.

What is difference between Groovy and Gradle?

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.

What is Groovy language Gradle?

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.


2 Answers

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.

like image 98
JBaruch Avatar answered Oct 21 '22 12:10

JBaruch


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.

like image 38
tkruse Avatar answered Oct 21 '22 11:10

tkruse