gradle use variables in parent task that are defined in child

前端 未结 1 764
独厮守ぢ
独厮守ぢ 2021-01-04 22:57

I have a multiproject gradle build where I declare a task in the parent build that uses a variable that gets declared in the child projects (the value can change depending o

相关标签:
1条回答
  • 2021-01-04 23:37

    There is a way to do this (project.evaluationDependsOnChildren()), but I recommend to only use it as a last resort. Instead, I'd configure the commonalities at the top level, and the differences at the subproject level:

    build.gradle (top level):

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

    build.gradle (subproject):

    myTask {
        prop = 'someValue'
    }
    

    Another way to avoid repeating yourself is to factor out common code into a separate script, and have subprojects include it with apply from:. This is a good choice when the logic only applies to selected subprojects, or in cases where it's desirable to avoid coupling between parent project and subprojects (e.g. when using Gradle's new configuration on demand feature).

    0 讨论(0)
提交回复
热议问题