Gradle Exec task with variables not working

I am trying to pass the variables from other tasks to Exec task and use them as arguments as below. But, gradle variables are not being de-referenced in "args" variable of the Exec task "sideloadOraDb".

//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
   }
   commandLine 'echo'
   args tenant,instance

}

Output

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

I tried with placing variables into command directly, but it is also not helping :-

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

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

}

output

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$ 

Can you please let me know, whats going over here ?

1 answer

  • answered 2018-10-11 20:12 Subra M

    Working solution from @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'
    }
    

    Output

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