在 Gradle 脚本中,我有一个带有委托的 Groovy 闭包,并且我在该委托上创建了一个函数调用方法,如下所述:
// Simplified example
ant.compressFiles() {
addFile(file: "A.txt")
addFile(file: "B.txt")
addAllFilesMatching("C*.txt", getDelegate())
}
def addAllFilesMatching(pattern, closureDelegate) {
// ...
foundFiles.each {
closureDelegate.addFile(file: it)
}
}
是否有可能以更漂亮的方式做到这一点,而不必将委托传递给函数?例如,是否可以使用新方法以某种方式扩展委托?