gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:19:52 26 4
gpt4 key购买 nike

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

task hello (type : DefaultTask) {
}

我在一个 groovy 项目中尝试过,发现它是非法的,gradle 是如何让它工作的。
而且我也不明白上面的表达,为什么我们可以加上 (type : DefaultTask) ,我们如何用groovy语法分析它?

最佳答案

作为 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'
}

所以当底部的脚本被执行时,它会打印 Hello tim到标准输出

但大卫的答案是正确的,这只是举例

See also here in the documentation for Groovy

关于gradle - 为什么在 gradle 任务的名称中可以省略引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37670201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com