How do I grab the output of a task (going to stdout) and write it to a file?

This seems like it should be pretty simple, but I cant find a way to do it.

I have the following task:

task dotDescription(type: JavaExec, dependsOn: "classes"){
    classpath configurations.runtime, sourceSets.main.output
    main = "mini.DotTest"
    args relativePath("src\main\java\funs.mini")
}

which generates a GraphViz file and outputs it to stdout.

I can run:

gradle dotDescription > file.dot

but it would be nice if the build would just put it into file.dot for me.

Is there a way to do this?

In your javaExec task, you can do something like:

standardOutput = new ByteArrayOutputStream()
doLast {
  def theOutput = standardOutput.toString()
  //do something with the output, e.g. print to the file, etc.
}

Hope that helps!