Following up on https://github.com/gradle/gradle/issues/7784 , I tried to embrace the new feature of Property
/Provider
tracking their input tasks, but it only seems to work for files.
I extended the sample code from the issue to demonstrate the problem:
import javax.inject.Inject
open class Producer : DefaultTask() {
@OutputFile
val outputFile : RegularFileProperty = project.objects.fileProperty()
@TaskAction
fun run() {
println("Write")
outputFile.get().asFile.writeText("Hello World")
}
}
open class Consumer : DefaultTask() {
@InputFiles
val ins : ConfigurableFileCollection = project.files()
@TaskAction
fun run() {
ins.forEach {
println(it.absolutePath)
}
}
}
open class SingleFileConsumer @Inject constructor (objects: ObjectFactory) : DefaultTask() {
@InputFile
val ins: RegularFileProperty = objects.fileProperty()
@TaskAction
fun run() {
println(ins.asFile.get().readText())
}
}
// I tried to see if there is any difference between injecting an ObjectFactory and using project.objects,
// but it doesn't seem to be the case
open class StringConsumer @Inject constructor (objects: ObjectFactory) : DefaultTask() {
@Input
val ins : Property<String> = objects.property(String::class.java)
@TaskAction
fun run() {
println(ins.get())
}
}
val first = tasks.register("first", Producer::class) {
outputFile.set(layout.buildDirectory.file("foo"))
}
// depends on Producer
task("second", Consumer::class) {
ins.from(first.map({ it.outputFile }))
}
// doesnt depend on Producer
task("third", StringConsumer::class) {
ins.set(first.flatMap({ it.outputFile }).map({ it.asFile.readText() }))
}
// depends on Producer
task("fourth", SingleFileConsumer::class) {
ins.set(first.map({ it.outputFile.get() }))
}
(Note that this is a construed example, as StringConsumer
could just as easily take the file as an input directly, but in my real use cases - a plugin to deploy docker containers - there are no files involved at all)
Is this intentional, or will this feature be added in a later release? Or am I using it wrong?
Thanks for your help!