为什么可以在gradle任务的名称中省略引号

Zij*_*ian 6 groovy build-tools gradle

我不明白为什么当我们声明它时,我们不需要为gradle任务的名称添加引号:

task hello (type : DefaultTask) {
}
Run Code Online (Sandbox Code Playgroud)

我试过一个时髦的项目,发现它是非法的,gradle是如何工作的.而且我不理解上面的表达式,为什么我们可以添加(type : DefaultTask),我们如何用groovy语法分析它?

tim*_*tes 3

作为 GroovyConsole 可运行形式的示例,您可以这样定义一些代码:

// 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'
}
Run Code Online (Sandbox Code Playgroud)

因此,当执行底部的脚本时,它会打印Hello tim到 stdout

但大卫的答案是正确的,这只是一个例子

另请参阅 Groovy 文档中的此处