Gres
Gres

Reputation: 139

Gradle: Get Ant Task Output

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

Answers (2)

Gres
Gres

Reputation: 139

Solution

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

Opal
Opal

Reputation: 84784

It seems that this link may be helpful. Changing log level along with writing custom wrapper may be what you're looking for.

Upvotes: 0

Related Questions