gpt4 book ai didi

gradle - 变量不起作用的Gradle Exec任务

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

我试图将变量从其他任务传递给Exec任务,并将其用作参数,如下所示。但是,没有在Exec任务“sideloadOraDb”的“args”变量中取消引用gradle变量。

//变量在脚本根目录中定义
def字符串租户
def字符串实例

task getInstance(type:Exec){
workingDir System.getenv('T_WORK')
commandLine 'echo'
args 'abc'
standardOutput = new ByteArrayOutputStream()
doLast{
instance = standardOutput.toString().trim()
print instance
}
}

task getTenant(type:Exec,dependsOn:getInstance){
workingDir System.getenv('T_WORK')
commandLine 'echo'
args 'xyz'
standardOutput = new ByteArrayOutputStream()
doLast{
tenant = standardOutput.toString().trim()
print tenant
}
}

task sideloadOraDb(type:Exec,dependsOn:getTenant){
def String cmd

doFirst{
println "Instacne="+instance
println "Tenant="+tenant
}
commandLine 'echo'
args tenant,instance

}

输出量
bash-4.1$ vi build.gradle 
bash-4.1$ gradle sideloadOraDb
Parallel execution is an incubating feature.
:getInstance
abc:getTenant
xyz:sideloadOraDb
Instacne=abc
Tenant=xyz

:sideloadOraDb FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':sideloadOraDb'.
> java.lang.NullPointerException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

我尝试将变量直接放入命令中,但这也无济于事:-
task sideloadOraDb(type:Exec,dependsOn:getTenant){
def String cmd

doFirst{
println "Instacne="+instance
println "Tenant="+tenant
}
commandLine 'echo',tenant,instance
// args tenant,instance

}

输出
bash-4.1$ gradle sideloadOraDb
Parallel execution is an incubating feature.
:getInstance
abc:getTenant
xyz:sideloadOraDb
Instacne=abc
Tenant=xyz
:sideloadOraDb FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':sideloadOraDb'.
> java.lang.NullPointerException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.326 secs
bash-4.1$

你能告诉我,这是怎么回事?

最佳答案

@nickb的工作解决方案:-

//the variable is defined within script root
def String tenant
def String instance

task getInstance(type:Exec){
workingDir System.getenv('T_WORK')
commandLine 'echo'
args 'abc'
standardOutput = new ByteArrayOutputStream()
doLast{
instance = standardOutput.toString().trim()
print instance
}
}

task getTenant(type:Exec,dependsOn:getInstance){
workingDir System.getenv('T_WORK')
commandLine 'echo'
args 'xyz'
standardOutput = new ByteArrayOutputStream()
doLast{
tenant = standardOutput.toString().trim()
print tenant
}
}

task sideloadOraDb(type:Exec,dependsOn:getTenant){
def String cmd

doFirst{
println "Instacne="+instance
println "Tenant="+tenant
args tenant,instance
}
commandLine 'echo'
}

输出量
bash-4.1$ gradle sideloadOraDb
Parallel execution is an incubating feature.
:getInstance
abc:getTenant
xyz:sideloadOraDb
Instacne=abc
Tenant=xyz
xyz abc

BUILD SUCCESSFUL

关于gradle - 变量不起作用的Gradle Exec任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52767823/

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