Hello,
I’m trying to understand implicit dependencies between tasks. Basically, I want a producer task to write something into a directory and a consumer task that reads something from there. As far as I understood implicit dependencies it should be possible that the consumer has an implicit dependency on the producer using the shared directory.
I don’t want a dependency on the task itself, because I will have more than one task writing into said directory and the consumer should be dependent on all of them.
What I’ve tried is:
abstract class ProducerTask: DefaultTask() {
@get:OutputDirectory
abstract val outDir : DirectoryProperty
@TaskAction
fun action() {
println("Producer: ${outDir.get()}")
outDir.get().file("foo.bar").asFile.writeText("foobar")
}
}
abstract class ConsumerTask : DefaultTask() {
@get:InputDirectory
abstract val dir : DirectoryProperty
@TaskAction
fun test() {
println("Consumer: ${dir.get()}, Content: ${dir.file("foo.bar").get().asFile.readText()}")
}
}
and in the build.gradle.kts:
val myDir = layout.buildDirectory.dir("mytest")
task<ProducerTask>("producer") {
outDir.set(myDir)
}
task<ConsumerTask>("consumer") {
dir.set(myDir)
}
I expected that when I call the ConsumerTask the ProducerTask would be automatically triggered. But this does not work, instead the Consumer throws an error because the directory does not exist.
Can you please shed some light on how this should work?