"Could not get unknown property" task error


(bitsofinfo) #1

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.

(bitsofinfo) #2

Why is this?

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"
    }
}

}

(Stefan Oehme) #3

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')


(Igor Ganapolsky) #4

I would still have to use findProperty('myVarA') inside of a method block. Can’t I use that at the top-level of a script?


(Stefan Oehme) #5

Sorry I didn’t understand your question. Could you write some pseudo-code of what you would like to do?