Gradle 更改渐变中的全局ext属性

Gradle 更改渐变中的全局ext属性,gradle,Gradle,我正在尝试为不同的环境编写一个构建脚本。但全局属性不会针对特定任务进行更新。 以下是脚本: ext { springProfilesActive = 'development' angularAppBasePath = '/test/' } task setActiveProfiles { doLast { if (project.hasProperty('activeProfiles')) { springProfilesAct

我正在尝试为不同的环境编写一个构建脚本。但全局属性不会针对特定任务进行更新。 以下是脚本:

ext {
    springProfilesActive = 'development'
    angularAppBasePath = '/test/'
}

task setActiveProfiles {
    doLast {
        if (project.hasProperty('activeProfiles')) {
            springProfilesActive = project.property('activeProfiles')
        }
    }
}

task setProperties(dependsOn: ':setActiveProfiles') {
    doLast {
        if (springProfilesActive != 'development') {
            angularAppBasePath = '/'
        }
        println springProfilesActive
        println angularAppBasePath
    }
}

task buildAngular(type: Exec, dependsOn: ':setProperties') {
    workingDir angularPath
    commandLine 'cmd', '/c', "npm run build.prod -- --base ${angularAppBasePath}"
}
如果我运行
buildAngular-PactiveProfiles=integration
,则属性设置正确。但是
angularAppBasePath
仍然是npm命令中的旧
/test/
值。输出:

Executing external task 'buildAngular -PactiveProfiles=integration'...
:setActiveProfiles
:setProperties
integration
/
:buildAngular

> angular-seed@0.0.0 build.prod C:\myapp\src\main\angular
> gulp build.prod --color --env-config prod --build-type prod "--base" "/test/"

为什么在
setProperties
任务中更改了属性,但在
buildAngular
任务中保留了旧值?

请尝试按如下方式重写
setActiveProfiles
setProperties
任务:

task setActiveProfiles {
    if (project.hasProperty('activeProfiles')) {
        springProfilesActive = project.property('activeProfiles')
    }
}

task setProperties(dependsOn: ':setActiveProfiles') {
    if (springProfilesActive != 'development') {
        angularAppBasePath = '/'
    }
    println springProfilesActive
    println angularAppBasePath
}  

此行为是由不同的生成生命周期引起的。您在执行阶段(在
doLast
closure中)修改变量,但在配置阶段使用它,而配置阶段恰好发生在执行之前。您可以在中阅读有关生成生命周期的内容。

不幸的是,此更改没有任何效果。当然,
setProperties
任务也应该在配置阶段执行。只是忘了将其添加到代码段中