This is starting to drive me nuts. I want to have two separate tasks that I can execute from any of my subprojects. (task1 and task2)
Such as
./gradlew task1 -PmyVarA=whatever
OR
./gradlew task2 -PmyVarB=whatever
build.gradle
subprojects {
task task1(type: TaskX) {
a = "$myVarA"
}
task task2(type: TaskY) {
b = "$myVarB"
}
}
The problem is that whenever I run “task2” I get the following types of errors, (and the inverse if I run “task2”). I don’t get why I’m getting errors for an “unset property” for a task I am not even running
> Could not get unknown property 'myVarA' for task 'task1' of type my.package.TaskX.
The only way around this is to do this everywhere, which seems… well weird. Why is “task1” being executed and evaluated for the existence of “myVarA” when I say
./gradlew task2 -PmyVarB=whatever
modified
subprojects {
task task1(type: TaskX) {
if (project.hasProperty("myVarA")) {
a = "$myVarA"
}
}
task task2(type: TaskY) {
if (project.hasProperty("myVarB")) {
a = "$myVarB"
}
}
}
The code you are showing is executed during the configuration phase when the properties/inputs/outputs of your task are configured. All tasks are configured since otherwise Gradle couldn’t tell which tasks depend on each other and which actually need to be run in which order.
You can shorten your code to a = findProperty('myVarA')