Hi everyone,
I’m writing a custom Gradle plugin and have encountered an issue with storing properties between tasks. I have created an object within my plugin to store properties that can be modified by tasks. Here’s a simplified version of my code:
class MyPlugin : Plugin<Project> {
companion object {
var myProperty: String = ""
}
override fun apply(project: Project) {
...
One of the task change this object’s property “myProperty” using option
abstract class MyTask1 : DefaultTask(){
@Option(
option = "sub-path",
description = "some-description",
)
fun setSubPath(subPath: String) {
myProperty = subPath
}
@TaskAction
fun action(){
println(myProperty)
}
...
No other task depends upon above Task.
Now suppose, I have other task which do something.
abstract class MyTask2 : DefaultTask(){
@TaskAction
fun action(){
println(myProperty)
}
When I run MyTask1
with the option --sub-path=pizza
, it prints pizza
, as expected. However, when I subsequently run MyTask2
, it also prints pizza
but I would like MyTask2
to use an empty string instead of the property set by MyTask1
.
What I want is to if they run together then it should be behave as exactly like they are supposed to do but if one of task run separately (most probably Task2) , it should use default value.
How can I ensure that MyTask2
uses an empty string (or any default value) for myProperty
instead of the value set by MyTask1
? Is there a way to reset the property or achieve this behavior within the Gradle plugin framework?