I have a task that takes an input source file and has one or more output files. The very first time the task is executed, everything is fine. But if I do a clean, gradle doesn’t see that the output files are gone and the task needs to be rerun. The confusion is understandable since I’m using a wildcard to specify the output files and beyond that I don’t know the output file names in advance.
Here is a minimal example (in Kotlin):
task(“clean”) {
delete(project.buildDir)
}
task(“one2many”) {
val source = “one.txt”
val manyFiles = “${project.buildDir}/many/*.txt”
Try using outputs.dir(project.buildDir.resolve("many")) to specify that your output is every file in that directory. Not sure if this helps, but it may be a step in the right direction.
Thanks, Róbert! That does work. Although I don’t understand what role resolve is playing. I’m going to tinker with this some more.
It would also be nice to be able to specify a pattern for specific files in the directory and not just all files in the directory. But that’s a nice-to-have, not a requirement.
File.resolve("foo") extension function in Kotlin is a nicer way of doing "${file}/foo", more typed and safe. For example if file is null resolve will throw NPE, but interpolation will give you null/foo and probably create a folder named null later on instead of failing. buildDir is non-null in this particular case, but in general it’s a better pattern I found.