我有以下简化的build.gradle
ext.greeting = "not set"
task configHello{
project.greeting = "Hello"
doLast{
project.greeting = "I said hello"
}
}
task configGoodbye{
project.greeting = "Goodbye"
}
task print(dependsOn: configHello){
def localString = project.greeting
doLast{
println project.greeting
println localString
}
}
运行gradle打印给出: :configHello :打印 我说了你好 再见
我想要的是它说“你好”。我应该如何以一种很好的方式设置localString?或者我是以错误的方式来做这件事的?真实生活场景打印任务是JavaExec类型,但我发现这更容易理解。
编辑:或者是通过DAG配置打印任务的方式去?
嗯,很快找到了答案......诀窍是利用doFirst。工作解决方案:ext.greeting =“not set”
task configHello{
doLast{
project.greeting = "I said hello"
}
}
task configGoodbye{
project.greeting = "Goodbye"
}
task print(dependsOn: configHello){
def localString
doFirst{
localString = project.greeting
}
doLast{
println project.greeting
println localString
}
}
得到: :configHello :打印 我说了你好 我说了你好