Gradle 跳过渐变配置阶段的变量检查

Gradle 跳过渐变配置阶段的变量检查,gradle,Gradle,我有一些gradle脚本,在其中我读取一些属性文件(不同执行配置中的不同文件),并在每个任务中将属性obect分配给“ext”属性 task provisionMongoDBCopyDockerfile(type: Copy, dependsOn: 'readTestConfiguration') { from "${projectDir}/deployment/scripts/Dockerfile.mongodb" into "/tmp/stand/mondod

我有一些gradle脚本,在其中我读取一些属性文件(不同执行配置中的不同文件),并在每个任务中将属性obect分配给“ext”属性

task provisionMongoDBCopyDockerfile(type: Copy, dependsOn: 'readTestConfiguration') {
        from "${projectDir}/deployment/scripts/Dockerfile.mongodb"
        into "/tmp/stand/mondodb"
        expand(ext.stand)
        filteringCharset = 'UTF-8'
    }

    task readTestConfiguration () {
        def props = loadStandProperties('test')
        println props
        tasks.each {
            it.ext.stand = props
            println  it.ext
        }
    }
但当我运行gradle脚本时,我得到了一个错误:“无法在额外属性扩展上获取属性'stand',因为它不存在”与“expand(ext.stand)”一致。我怎样才能解决这个问题。我不想将所有配置参数都放在“gradle.properties”中,并将其从一个配置更改为另一个配置。

考虑以下事项(使用gradle 2.14.1)。这在配置阶段有效地建立了依赖关系。它还使用
project.ext
tasks.ext

def readTestConfiguration = {
    def props = loadStandProperties('test')
    println props
    project.ext.stand = props
}

def loadStandProperties (def env) {
    // use mock data
    return ["foo": "bar"]
}

tasks.whenTaskAdded { Task task ->
    if (task.name == "provisionMongoDBCopyDockerfile") {
        readTestConfiguration()
    }
}

task provisionMongoDBCopyDockerfile(type: Copy) {
    from "${projectDir}/in"
    into "${projectDir}/out"
    expand(project.ext.stand) 
}

看起来我在gradle DAG building中使用了事件-但是如果我需要读取不同的属性,该怎么办-比如说“gradle buildTestStand”读“stand test.properties”,“gradle buildProductionStand”读“stand properties.properties”?但在配置阶段,这不是一个问题,可能是-它只是检查属性-而不是它的值…我相信您会问多个问题。我的回答解决了你所遇到的错误。如果你的目标还有其他障碍,我建议你再问一个问题。