Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Option 1: executing a string

Code Block

A string can be executed in the standard java way:
def command = """executable arg1 arg2 arg3"""// Create the String
def proc = command.execute()                 // Call *execute* on the string
proc.waitFor()                               // Wait for the command to finish

// Obtain status and output
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

Gotchas: Take care if you wish to pass a quoted argument that contains white space – it will be split into multiple arguments, e.g.:

Code Block

"""executable "first with space" second""".execute()

...

In such a case, you may prefer to use one of the array or list of String variations, e.g.:

Code Block

["executable", "first with space", "second"].execute()

...

Ant has an exec task and it be accessed from the AntBuilder object

Code Block

def ant = new AntBuilder()   // create an antbuilder
ant.exec(outputproperty:"cmdOut",
             errorproperty: "cmdErr",
             resultproperty:"cmdExit",
             failonerror: "true",
             executable: '/opt/myExecutable') {
                             arg(line:"""*"first with space"* second""")
             }
println "return code:  ${ant.project.properties.cmdExit}"
println "stderr:         ${ant.project.properties.cmdErr}"
println "stdout:        ${ ant.project.properties.cmdOut}"

...