Created
April 23, 2010 10:34
-
-
Save lcorneliussen/376421 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mixinAntExec() { | |
String.metaClass.antExec = {failOnError=true -> antExec([delegate], failOnError)} | |
ArrayList.metaClass.antExec << {failOnError=true -> antExec(delegate, failOnError)} | |
} | |
def antExec(command, failOnError=true) { | |
def commandLiteral = command.collect{it.contains(' ') ? '"' + it + '"' : it}.join(' ') | |
println "ant exec with: ${commandLiteral}" | |
def ant = new AntBuilder() | |
ant.exec(outputproperty:"text", | |
errorproperty: "error", | |
resultproperty: "exitValue", | |
failonerror: false, | |
executable: command[0]) { | |
if (command.size()>1) | |
arg(line:command[1..-1].collect{it.contains(' ') ? '"' + it + '"' : it}.join(' ')) | |
} | |
def result = new Expando( | |
text: ant.project.properties.text, | |
error: ant.project.properties.error, | |
exitValue: ant.project.properties.exitValue as Integer, | |
toString: {text} | |
) | |
if (failOnError && result.exitValue != 0){ | |
throw new Exception("""command failed with ${result.exitValue} | |
executed: ${commandLiteral} | |
error: ${result.error} | |
text: ${result.text}""") | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog Entry: http://startbigthinksmall.wordpress.com/2010/04/23/antexec-executing-external-processes-in-groovy/