考虑 gradle 构建脚本中包含的以下方法调用:
gradle.taskGraph.whenReady{taskGraph ->
println gradle.toString()
println "Ready"
}
它打印
build 'task_graph'
Ready
我以为我们在 Project 的范围内工作对象,自
gradle
应该是 Project
的属性目的。但是既没有属性,也没有具有这样名称的方法。为什么我们可以在构建脚本中使用它?
最佳答案
我可能错了,但我认为您的困惑是存在 getGradle()
Project
上的方法接口(interface),但没有名为 gradle
的公共(public)字段.这是一个 Groovy 功能。在 Groovy 中,getter 和 setter 方法可以作为属性引用。例如:
println project.description // same as project.getDescription()
project.description = 'My java project' // same as project.setDescription('My java project')
我强烈建议您通过查看 their documentation 来熟悉 Groovy。 .您将在 Gradle 示例中看到许多不同的语法,因为在 Groovy 中有许多不同的方法可以完成相同的事情。
关于groovy - gradle 构建脚本中的 gradle 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317563/