Gradle 两个任务之间的冲突

Gradle conflict between two tasks

我在 gradle 中编写了一些任务,但出现了一个奇怪的错误。

task buildProduction() {
    description 'Build the production version of the app (creates the yaml file)'
    copyAndReplaceYaml("Production")
}

task buildStaging() {
    description 'Build the staging version of the app (creates the yaml file)'
    copyAndReplaceYaml("Staging")
}

当我 运行 buildStaging 时,它工作正常,但是当我 运行 buildProduction 时,就像我在 运行ning buildStaging。

如果我切换方法在文件中的位置 buildProduction 有效而不是 buildStaging

知道为什么会这样吗?

您正在将副本作为任务 配置 的一部分执行,无论命令是什么,它总是在执行任务之前执行。你需要把这两个任务的代码改成

task buildStaging {
    description 'Build the staging version of the app (creates the yaml file)'
}
buildStaging << {
    copyAndReplaceYaml("Staging")
}

task buildStaging {
    description 'Build the staging version of the app (creates the yaml file)'
    doLast {
        copyAndReplaceYaml("Staging")
    }
}