How to pass files generated by one task to another

Hey guys,

I have two custom tasks that should generate some files, one depends on the output of the other for generation. What would be the best way to share the output files of the first task to the second task?


Hey Ante!

Best practice is to make one or more of the properties of the custom task annotated with @OutputDirectory or @OutputFiles for things that are outputs and annotate with @InputFiles for anything that’s an input. You can then do something like:

task generateTaskA(type: CustomTaskA) {
    // config

task generateTaskB(type: CustomTaskB) {
   filesToGenerateFrom = files(generateTaskA)

Here filesToGenerateFrom is a FileCollection and marked with @InputFiles. Anything marked with @Output* will be included in that FileCollection for generateTaskB. You don’t have to worry about setting up dependencies since Gradle can infer it.

These might be useful too:

1 Like

Thanks, what about sharing of output directories? Is this a good idea? The task that I have needs to have the files either copied over to it’s input directory or share the directory with the previous.

Sharing output directories can work, but you’d need to be able to distinguish between output produced by one task vs the other. e.g., one task only produces .c files and the other only produces .cpp and the output files are filtered by extensions. Otherwise, both tasks will see “new” files in their output directory and think they need to re-run.

Re-reading your question… do you mean that the second task could use the first task’s output directory as an input and an output?

Yes I meant that the second task would use it as input and the output.

Can the second task identify its output uniquely somehow (extension, prefix, etc)?

Can the second task output to a different directory?

Yes it could output to the different directory, and it can differentiate between input files based on extension.

Ok I was able to do it without needing to use the output directory of one task as the input and output of the second.

Great. Let me know if you need any other help.