如何重构常见的Gradle任务配置?

如何重构常见的Gradle任务配置?,gradle,Gradle,我有如下几点: test { // bunch of common config stuff // some config stuff specific to `test` } task integrationTest(type: Test) { // bunch of common config stuff // some config stuff specific to `integrationTest` } 如何避免重复“一堆常见的配置内容”?您可以

我有如下几点:

test {
    // bunch of common config stuff

    // some config stuff specific to `test`
}

task integrationTest(type: Test) {
    // bunch of common config stuff

    // some config stuff specific to `integrationTest`
}
如何避免重复“一堆常见的配置内容”?

您可以使用以下方法:

tasks.withType(Test) {
    // common stuff
}

test { ... }

task integrationTest(type: Test) { ... }

这不起作用,因为
config
方法的作用域不同。它无法访问
测试任务可用的属性。如何配置(这个)?它可以达到适当的范围。但我同意@PeterNiederwieser的答案能更好地解决这个问题。
这个范围也不一样
config(delegate)
就快到了。我发现如果您想按类型以外的内容进行选择,还可以编写“tasks.with(Closure)”。
test {
    config()
    // some config stuff specific to `test`
}

task integrationTest(type: Test) {
    config()
    // some config stuff specific to `integrationTest`
}

void config() {
    // bunch of common config stuff
}