Reputation: 139
I have an Ant task that use the standard output to print information and I want use this output in a gradle task. I execute Ant task using following command:
tasks.myAntTask.execute()
Anybody know how can I get the Ant output and pass it to the gradle task?
Upvotes: 2
Views: 1544
Reputation: 139
Here's a method that captures the output of an Ant task by registering a custom BuildListener for the duration of the call.
def captureAntOutput(ant, Closure command) {
def buffer = new ByteArrayOutputStream()
def captureStream = new PrintStream(buffer, true, "UTF-8")
def listener = new org.apache.tools.ant.DefaultLogger(
errorPrintStream: captureStream,
outputPrintStream: captureStream,
messageOutputLevel: org.apache.tools.ant.Project.MSG_INFO
)
ant.project.addBuildListener(listener)
project.configure(ant, command)
ant.project.removeBuildListener(listener)
return buffer.toString("UTF-8");
}
Example usage:
String result = captureAntOutput(ant) {
echo(message: "hello")
}
assert result.contains("hello")
Upvotes: 1