I want to get exit code from a command executed through Ant task exec
. See below code example.
import groovy.ant.AntBuilder
def cmd = 'ls -l'
def ant = new AntBuilder()
ant.echo('Hello, Ant Builder!')
ant.exec(executable: 'bash', resultproperty: 'my.cmd.exitCode') {
arg(value: '-c')
arg(value: cmd)
}
ant.echo('Exit code is ${my.cmd.exitCode}')
// below return null
def cmdExitCode = ant.properties['my.cmd.exitCode']
println "Exit code: ${cmdExitCode}"
println "Properteis: ${ant.properties}"
Below is partial output:
[echo] Hello, Ant Builder!
[exec] total 64
...
[echo] Exit code is 0
Exit code: null
Properteis: [saveStreams:true, current:null, antXmlContext:org.apache.tools.ant.helper.AntXMLContext@78411116, class:class groovy.ant.AntBuilder, antProject:org.apache.tools.ant.Project@aced190, project:org.apache.tools.ant.Project@aced190]
The Ant documentation on exec states that
outputproperty The name of a property in which the output of the command should be stored. Unless the error stream is redirected to a separate file or stream, this property will include the error output.
It is not clear how to access this property programmatically in Groovy AntBuilder.
You are accessing the wrong properties here. Each GroovyObject
has
a getProperties
(or properties
in Groovy-short-hand). And the one
from the AntBuilder
is just what is used to "talk to" ant
.
The properties you are looking for are in the Project
, which you can
access on the builder like this:
def cmdExitCode = ant.antProject.properties['my.cmd.exitCode']
// XXX ~~~~~~~~~~
println "Exit code: ${cmdExitCode}"