How do I pass the stdout of an Exec task to another Exec task?

I need to capture the output of an API call that I make in task A and pass a token from the API response to another task (B). Both tasks are of type Exec. It seems that I can only get the token from task A during the execution phase, but configuring task B fails as the token is not available during configuration phase.

A simple example that demonstrates my conundrum:

task pwd(type: Exec) {
    commandLine 'pwd'
      standardOutput = new ByteArrayOutputStream()
      ext.output = {
        return standardOutput.toString()
    }
}
  task lsPwd(type: Exec, dependsOn: pwd){
    println "Configuration: '${pwd.output}'"
      doFirst {
        println "Execution First: ${pwd.output}"
    }
    doLast {
        println "Execution Last: ${pwd.output}"
    }
      commandLine 'ls'
    args = ['-l', '-d', "${pwd.output}"]
}

Executing task lsPwd fails as ${pwd.output} is not available when it’s configured. Commenting out the args line, I can see it output in the doFirst and doLast blocks, as I expected.

Is there any way to pass the output of an execution phase to an Exec task?

Thanks, Noel

Instead of using a second ‘Exec’ task you might want to try simply adding a ‘doLast()’ action to the first task and call ‘Project.exec()’ from it.

task pwd(type: Exec) {

// configure

doLast {

exec {

commandLine ‘ls’, ‘-l’, ‘-d’, output

}

}

}

Hi Mark,

Thanks for your reply. I’m not sure that will work for me as I need the two tasks to be separate. The first one uses the AWS sdk to upload a deployment artefact to S3 and the second one deploys it from S3 with CodeDeploy. I don’t always want to deploy an artefact after uploading, so I need to keep these tasks separate.

Any suggestions?

Thanks, Noel

You could alternatively set the command line also in a ‘doLast()’ block on the pwd task. For example:

task foo {

doLast {

bar.commandLine ‘ls’, ‘-al’

}

}

task bar(type: Exec, dependsOn: foo) {}