gradle:从另一个子项目设置子项目属性

时间:2014-04-21 04:13:04

标签: gradle

settings.gradle

include ':projectA',':projectB'

了projectA /的build.gradle

task task1()  {
    doFirst {
        println 'setting project.ext.testProperty1'
        project(':projectB').ext.testProperty1 = 'MyProperty'
    }
}

task task2 (dependsOn: ['task1', ':projectB:task3']) {
    doLast {
        println "Executed project B Task3 from projectA task2"
    }
}

项目B /的build.gradle

task task3() {
    doLast {
        println "task3 from projectB"
        println project(':projectB').ext.testProperty1
        if(project(':projectB').ext.hasProperty("testProperty1")) {
            ext.prop1  = project.property("testProperty1")
            println "+++++++++If : Clause++++++++++++++++++++++++++++++++"
        } else {
            println "+++++++++Else  :Clause++++++++++++++++++++++++++++++++"
        }
    }
}

现在当我调用gradle task2时,我得到的输出总是

$ ../gradlew task2
:projectA:task1
setting project.ext.testProperty1
:projectB:task3
task3 from projectB
MyProperty
+++++++++Else  :Clause++++++++++++++++++++++++++++++++
:projectA:task2
Executed project B Task3 from projectA task2

它永远不会进入"如果"条款,我缺少什么,甚至似乎在ProjectB中获得ext.property testProperty1但hasProperties似乎没有正确评估它。

1 个答案:

答案 0 :(得分:1)

如果你从代码中取出“分机”

if(project(':projectB').ext.hasProperty("testProperty1")) 

而是将其更改为

if(project(':projectB').hasProperty("testProperty1")) 

然后转到if子句,我不知道为什么在readtime时使用ext会导致问题,因为我们可以在写入时使用它。