gpt4 book ai didi

gradle - 在运行时跳过gradle中的任务

转载 作者:行者123 更新时间:2023-12-03 05:02:20 24 4
gpt4 key购买 nike

我有两个简单的任务

task initdb { println 'database' }
task profile(dependsOn: initdb) << { println 'profile' }

在运行时,控制台中的结果如下所示

当我的任务看起来像这样
task initdb { println 'database' }
task profile() << { println 'profile' }

控制台中的结果是

在运行时任务 initdb中未使用 profile任务时如何跳过? (不使用 -x)

最佳答案

出现此现象的原因是initDb没有正确声明。它缺少<<,因此println语句在配置时而不是执行时运行。这也意味着该语句始终会运行。这并不意味着任务将被执行(在第二个示例中则不会)。

为避免此类错误,我建议使用更明确,更常规的doLast语法,而推荐使用<<:

task profile {
// code in here is about *configuring* the task;
// it *always* gets run (unless `--configuration-on-demand` is used)
dependsOn initdb

doLast { // adds a so-called "task action"
// code in here is about *executing* the task;
// it only gets run if and when Gradle decides to execute the task
println "profile"
}
}

关于gradle - 在运行时跳过gradle中的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23624734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com