Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: task's standardOutput to file and terminal simultaneously

I want to change standardOutput of one build task to file, because it will be parsed later by another task.

But also, I would like to have simultaneously output in the terminal to see what's going on in the build.

This is how I changed output of the task to the file:

task sampleTaskWithOutputToFile(type: Exec) {
    commandLine 'someCommand', 'param1'

    doFirst {
        standardOutput = new FileOutputStream('someFolder/someFile.out')
    }
} 

As I understand, I can write own OutputStream implementation with output to file and standard System.out simultaneously but I would like to use existing solution.

Also, I can not use unix tools like tee for that, because task can be launched from any OS (Mac OS, Some Linux or even Windows...)

Thanks!

like image 640
Artem Zinnatullin Avatar asked Jul 08 '14 13:07

Artem Zinnatullin


People also ask

What is doFirst in gradle?

The first "doFirst" function is defined in the initialize task. The second is defined outside of the configuration block. Why doesn't the first instance execute before the second one? The order of execution looks backward. I would have expected the first one, inside the configuration definition, to execute first.


1 Answers

Expounding on Peter N's comment regarding TeeOutputStream:

task sampleTaskWithOutputToFile(type: Exec) {
    commandLine 'someCommand', 'param1'

    doFirst {
        standardOutput = new org.apache.tools.ant.util.TeeOutputStream(
            new FileOutputStream("someFolder/someFile.out"), System.out);
    }
}
like image 68
Roberto Avatar answered Oct 23 '22 18:10

Roberto