I am wondering how default values should be used in task inputs for custom Gradle tasks. Suppose, I have the following minimal code example:
abstract class EchoMessageTask: DefaultTask() {
@get:Input @get:Optional
abstract val message: Property<String>
init {
message.convention("world") // set default value for property
}
@TaskAction
fun echoMessage() {
println("Hello ${message.get()}!")
}
}
It uses the following minimal implementation of a plugin that connects the extension with the input of the task:
class SimplePlugin: Plugin<Project> {
override fun apply(target: Project) {
val extension = target.extensions.create("simple", SimplePluginExtension::class.java)
target.tasks.register("echoMessage", EchoMessageTask::class.java) { task ->
task.message.set(extension.message)
}
}
}
abstract class SimplePluginExtension {
abstract val message: Property<String>
}
I would expect this task to execute and print Hello world
. However, it fails with the following error message:
Execution failed for task ':echoMessage'.
> Cannot query the value of task ':echoMessage' property 'message' because it has no value available.
The value of this property is derived from:
- extension 'simple' property 'message'
(This is Gradle 7.3)