我想使用Gradle在 list 文件中写入版本字符串。为此,我使用git describe
。为了获得此字符串,我编写了一个exec
任务:
task gitVersion(type: Exec) {
commandLine 'git', 'describe'
standardOutput = new ByteArrayOutputStream()
ext.output = {
return standardOutput.toString()
}
}
如果我用它来处理资源,它就可以工作,例如:
processResources {
dependsOn gitVersion
filesMatching('build.properties') {
expand 'buildVersion': "${gitVersion.output()}"
}
}
不幸的是,如果我在
jar
任务中尝试此操作,将无法正常工作。jar {
manifest {
attributes(
// Other attributes
'Implementation-Version': "${gitVersion.output()}" // Not working
)
}
}
据我了解Gradle Build Lifecycle,这是因为
jar
任务是“配置阶段”,而exec
任务是“执行阶段”。有什么方法可以在配置阶段执行
exec
任务?
最佳答案
您可以使用“GString惰性评估” Groovy功能(请参阅一些详细信息/示例here):
jar{
manifest {
attributes(
'Implementation-Version': "${->gitVersion.output()}" // use " ${->prop} syntax for lazy evaluation
)
}
}
关于git - 运行exec任务作为配置阶段的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53851245/