Gradle use variables in parent task defined in child

I have a gradle multiproject where I declare a task in the parent assembly that uses a variable that is declared in child projects (the value may vary depending on the subproject). However, at the configuration stage, I get an error when the variable does not exist. My setup looks like

build.gradle (top level)

subprojects {
   myTask {
     prop = valueDefinedInChild
   }

}

And then

build.gradle (subproject)

valueDefinedInChild = 'someValue'

Is there any way to do this right?

+5
source share
1 answer

There is a way to do this ( project.evaluationDependsOnChildren()), but I recommend using it only as a last resort. Instead, I set up similarities at the top level and differences at the subproject level:

build.gradle( ):

subprojects {
    task myTask { // add task to all subprojects
        // common configuration goes here
    }
}

build.gradle():

myTask {
    prop = 'someValue'
}

- script, apply from:. , , (, Gradle ).

+10

All Articles