如何将多个 delete fileTree gradle 任务合并为一个任务

How to combine multiple delete fileTree gradle tasks into single one

我有以下 Gradle 任务来删除不同目录和不同模式的文件

task cleanNodeCertFiles(type: Delete) {
    delete fileTree("node_modules/sub_dir1").matching {
        include "**/*.key"
    }

    delete fileTree("node_modules/sub_dir1").matching {
        include "**/*.txt"
    }

    delete fileTree("node_modules/sub_dir2").matching {
        include "**/*.key"
    }

    delete fileTree("node_modules/sub_dir2").matching {
        include "**/*.txt"
    }

}

有没有办法将这 4 个 delete fileTree 命令优化为 2 个或 1 个?

不知道我是否理解你的意思,但你可以使用多个包含来做到这一点:

task cleanNodeCertFiles(type: Delete) {
    delete fileTree("node_modules").matching {
        include "sub_dir1/**/*.key"
        include "sub_dir1/**/*.txt"
        include "sub_dir2/**/*.key"
        include "sub_dir2/**/*.txt"
    }
}