Thursday, April 14, 2011

Gradle: execute a task and write output to a file

i searched for a while to get this working as expected. The solution is really simple and straightforward.

task writeOutputToFile << { new FileOutputStream(new File("$outdir/$outfile")).withStream { os ->
def res = exec {
executable = "/bin/echo"
args = ["param1","param2"]
standardOutput = os
}
}
}

I was initially using Process to write, but groovy has a bug where the main process exits when it finishes, causing the thread to exit before it finishes writing (using a FileWriter).