dokaspar
dokaspar

Reputation: 8624

Trying to understand the most simple Gradle plugin

Being new to Gradle and Groovy, I'm having a hard time understanding the following piece of code I came across in the Writing a simple plugin tutorial:

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('hello') {
            doLast {
                println 'Hello from the GreetingPlugin'
            }
        }
    }
}
  1. What is project.task('hello') {...}? It looks like a method declaration without a return type or like a method call with additional brackets... what is this construct?
  2. What is doLast {...} and where does it originate from? What syntax pattern is that anyway... some keyword followed by brackets. I cannot find anything like it in the Groovy syntax page, which mostly has examples of using ${} or using brackets to define a class or method.

Upvotes: 0

Views: 35

Answers (1)

lance-java
lance-java

Reputation: 27958

In groovy, if the last argument to a method is a closure, you can put it outside the round brackets.

Eg

project.task('foo', {
    doStuff()
})

Is equivelant to

project.task('foo') {
    doStuff()
}

Here's the links to the javadocs for the two methods

  1. Project.task(String, Closure)

  2. Task.doLast(Closure)

Upvotes: 2

Related Questions