cry*_*ity 22 git variables groovy task gradle
我想在同一个build.gradle文件中将变量从一个任务传递到另一个任务.我的第一个gradle任务拉出最后一个提交消息,我需要将此消息传递给另一个任务.代码如下.提前感谢您的帮助.
task gitMsg(type:Exec){
commandLine 'git', 'log', '-1', '--oneline'
standardOutput = new ByteArrayOutputStream()
doLast {
String output = standardOutput.toString()
}
}
Run Code Online (Sandbox Code Playgroud)
我想将变量'output'传递给下面的任务.
task notifyTaskUpcoming << {
def to = System.getProperty("to")
def subj = System.getProperty('subj')
def body = "Hello... "
sendmail(to, subj, body)
}
Run Code Online (Sandbox Code Playgroud)
我想将git消息合并到'body'中.
Ren*_*hke 49
我认为应该避免全局属性,gradle通过向任务添加属性为您提供了一个很好的方法:
task task1 {
doLast {
task1.ext.variable = "some value"
}
}
task task2 {
dependsOn task1
doLast {
println(task1.variable)
}
}
Run Code Online (Sandbox Code Playgroud)
Sta*_*lav 12
您可以output在doLast方法之外定义变量,但是在脚本根目录中,然后只需在另一个任务中使用它.仅举例如:
//the variable is defined within script root
def String variable
task task1 << {
//but initialized only in the task's method
variable = "some value"
}
task task2 << {
//you can assign a variable to the local one
def body = variable
println(body)
//or simply use the variable itself
println(variable)
}
task2.dependsOn task1
Run Code Online (Sandbox Code Playgroud)
这里定义了2个任务.Task2取决于Task1,这意味着第二个将仅在第一个之后运行.该variable字符串类型在构建脚本根和宣布,以初始化task1 doLast方法(注意,<<是等于doLast).然后初始化变量,它可以被任何其他任务使用.