Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can quotes be left out in names of gradle tasks

I don't understand why we don't need to add quotes to the name of gradle task when we declare it like:

task hello (type : DefaultTask) {
}

I've tried in a groovy project and found that it's illegal, how gradle makes it works. And I don't understand the expression above neither, why we can add (type : DefaultTask), how can we analyze it with groovy grammar?

like image 428
Zijian Avatar asked Jun 07 '16 03:06

Zijian


People also ask

How do I exclude tasks in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.

How do Gradle tasks work?

The work that Gradle can do on a project is defined by one or more tasks. A task represents some atomic piece of work which a build performs. This might be compiling some classes, creating a JAR, generating Javadoc, or publishing some archives to a repository.

What is doLast in Gradle task?

doFirst(action) Adds the given Action to the beginning of this task's action list. doLast(action) Adds the given closure to the end of this task's action list. The closure is passed this task as a parameter when executed.


2 Answers

As an example in a GroovyConsole runnable form, you can define a bit of code thusly:

// Set the base class for our DSL

@BaseScript(MyDSL)
import groovy.transform.BaseScript

// Something to deal with people
class Person { 
    String name
    Closure method
    String toString() { "$name" }
    Person(String name, Closure cl) {
        this.name = name
        this.method = cl
        this.method.delegate = this
    }
    def greet(String greeting) {
        println "$greeting $name"
    }
}

//  and our base DSL class

abstract class MyDSL extends Script {
    def methodMissing(String name, args) {
        return new Person(name, args[0])
    }

    def person(Person p) {
        p.method(p)
    }
}

// Then our actual script

person tim {
    greet 'Hello'
}

So when the script at the bottom is executed, it prints Hello tim to stdout

But David's answer is the correct one, this is just for example

See also here in the documentation for Groovy

like image 161
tim_yates Avatar answered Oct 18 '22 20:10

tim_yates


A Gradle build script is a Groovy DSL application. By careful use of "methodMissing" and "propertyMissing" methods, all magic is possible.

I don't remember the exact mechanism around "task ". I think this was asked in the Gradle forum (probably more than once).

like image 1
David M. Karr Avatar answered Oct 18 '22 19:10

David M. Karr