我对在Gradle上创建自定义任务的正确方法有些困惑。在Creation of custom tasks教程中,他们使用tasks.register
像这样:
def check = tasks.register("check")
def verificationTask = tasks.register("verificationTask") {
// Configure verificationTask
}
check.configure {
dependsOn verificationTask
}
取而代之的是here(仍然是Gradle的官方文档),它们以这种方式创建新任务:
task('hello') {
doLast {
println "hello"
}
}
task('copy', type: Copy) {
from(file('srcDir'))
into(buildDir)
}
和
tasks.create('hello') {
doLast {
println "hello"
}
}
tasks.create('copy', Copy) {
from(file('srcDir'))
into(buildDir)
}
最后,根据文件https://docs.gradle.org/current/userguide/task_configuration_avoidance.html,他们建议从第二种/第三种情况转移到第一种情况。这是否意味着第二/第三种情况已过时?如果是,为什么Gradle仍在其文档中大量使用旧API?
用户应使用哪种变体?
最佳答案
Gradle API具有多种定义任务的方式。只要您保持一致,应用程序开发人员不会有“正确”或“错误”的方式,但对于 Gradle插件作者而言,这确实很重要。
您链接状态的避免任务配置 doc(重点是我的):
As of Gradle 5.1, we recommend that the configuration avoidance APIs be used whenever tasks are created by custom plugins.
因此,如果您是插件作者,请尽可能避免使用任务配置
对于其他所有人(应用程序开发人员),在某种程度上,这并不重要,只要您在整个应用程序中保持一致即可。
关于gradle - gradle创建自定义任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57571714/