Hello,
Could you advise what is the right way to expose files generated by a task? I tried an approach below (kotlin DSL) and it didn’t work:
abstract class Producer : DefaultTask() {
@get:OutputFiles
abstract val targetFiles: ListProperty<File>
@TaskAction
fun printMessage() {
val files = listOf(
project.layout.buildDirectory.get().file("file1").asFile,
project.layout.buildDirectory.get().file("file2").asFile
)
project.logger.lifecycle("exposing ${files.size} files: ${files.joinToString { it.canonicalPath }}")
files.forEach(targetFiles::add)
}
}
abstract class Consumer : DefaultTask() {
@get:InputFiles
abstract val inputFiles: ListProperty<File>
@TaskAction
fun processFiles() {
val files = inputFiles.get()
project.logger.lifecycle("consuming ${files.size} files: ${files.joinToString { it.canonicalPath }}")
}
}
tasks.register<Producer>("producer")
tasks.register<Consumer>("consumer") {
inputFiles = (tasks.getByName("producer") as Producer).targetFiles.get()
}
output:
./gradlew producer
> Task :producer FAILED
exposing 2 files: /Users/denis/Downloads/tmp/build/file1, /Users/denis/Downloads/tmp/build/file2
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':producer'.
> The value for task ':producer' property 'targetFiles' is final and cannot be changed any further.
It seems that such task output property is assumed to be configured by script author in build script and then its value should be used by the task itself. However, my goal is to expose files created by the task and use them as another task’s input.