变量不起作用的Gradle Exec任务

时间:2018-10-11 19:46:15

标签: gradle

我试图将变量从其他任务传递给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$ 

您能告诉我,这里发生了什么事?

1 个答案:

答案 0 :(得分:0)

@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
相关问题