Properties Gradle三值/elvis运算符未设置';其他';未设置外部属性时的值

Properties Gradle三值/elvis运算符未设置';其他';未设置外部属性时的值,properties,gradle,ternary-operator,Properties,Gradle,Ternary Operator,如果我不使用“-p”选项运行gradle,则我的gradle生成文件中elvis/Trimal运算符的“else”值未设置属性值。 下面是根项目的build.gradle defaultTasks 'loadConfiguration' task loadConfiguration << { def profile = hasProperty('profile') ? profile : 'dev' ext.profile = profile def con

如果我不使用“-p”选项运行gradle,则我的gradle生成文件中elvis/Trimal运算符的“else”值未设置属性值。 下面是根项目的build.gradle

defaultTasks 'loadConfiguration'

task loadConfiguration << {
    def profile = hasProperty('profile') ? profile : 'dev'
    ext.profile = profile
    def configFile = file('profile.properties')
    def config = new ConfigSlurper(profile).parse(configFile.toURL())
    ext.config = config
}

configure (subprojects) {
     profile = profile //inject property into sub-project
     println profile
     task buildear << {
        ear
     }   
}
外部属性集为空时:

$ gradle -Pprofile=wwww
Parallel execution is an incubating feature.
wwww
wwww
wwww
:loadConfiguration

BUILD SUCCESSFUL

Total time: 5.834 secs
$ gradle -Pprofile
Parallel execution is an incubating feature.



:loadConfiguration

BUILD SUCCESSFUL

Total time: 5.389 secs
$ gradle
Parallel execution is an incubating feature.

FAILURE: Build failed with an exception.

* Where:
Build file '/home/robert/codingk/kazi/trunk2/mazama2/build.gradle' line: 13

* What went wrong:
A problem occurred evaluating root project 'mazama2'.
> Could not find property 'profile' on project ':ear'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.104 secs
没有外部属性集:

$ gradle -Pprofile=wwww
Parallel execution is an incubating feature.
wwww
wwww
wwww
:loadConfiguration

BUILD SUCCESSFUL

Total time: 5.834 secs
$ gradle -Pprofile
Parallel execution is an incubating feature.



:loadConfiguration

BUILD SUCCESSFUL

Total time: 5.389 secs
$ gradle
Parallel execution is an incubating feature.

FAILURE: Build failed with an exception.

* Where:
Build file '/home/robert/codingk/kazi/trunk2/mazama2/build.gradle' line: 13

* What went wrong:
A problem occurred evaluating root project 'mazama2'.
> Could not find property 'profile' on project ':ear'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.104 secs
第13行是“profile=profile//inject property into sub-project”行

我不明白为什么“profile”属性没有设置为“dev”。有几件事我试过但没有成功:

profile= "${profile}

def profile = project.hasProperty('profile') ? profile : 'dev'
project.ext.profile = profile

通过以下修改使其正常工作。我可能错了,但我相信这与任务排序有关;将loadConfiguration()声明为默认任务并不会导致首先执行它,这正是我所需要的

def loadConfiguration() {
    def profile = hasProperty('profile') ? profile : 'dev'
    ext.profile = profile
    def configFile = file('profile.properties')
    def config = new ConfigSlurper(profile).parse(configFile.toURL())
    ext.config = config
}

configure (subprojects) {
     loadConfiguration()
     profile = profile //inject property into sub-project
     println profile
     task buildear << {
        ear
     }   
}
def loadConfiguration(){
def profile=hasProperty('profile')?profile:'dev'
ext.profile=profile
def configFile=file('profile.properties')
def config=new ConfigSlurper(profile).parse(configFile.toURL())
ext.config=config
}
配置(子项目){
loadConfiguration()
profile=profile//将属性注入子项目
println配置文件

task buildear关于初始构建脚本的一些评论。 在初始构建脚本中,您声明了一个任务(loadConfiguration)来设置配置文件。此逻辑在下面的配置块之后执行 这就是为什么配置块中的“profile”总是空的原因之一

第二个问题是,您需要注意范围界定

ext.profile = profile
您将动态属性添加到loadConfiguration任务,而不是项目本身。这就是您无法引用的原因 如果尚未通过命令行传递配置文件属性,则返回该属性

与其在单独的任务中进行配置加载,不如在构建的顶层进行:

ext.profile = hasProperty('profile') ? profile : 'dev'
def configFile = file('profile.properties')
def config = new ConfigSlurper(profile).parse(configFile.toURL())
ext.config = config


configure (subprojects) {
     profile = profile //inject property into sub-project
     println profile
     task buildear << {
        ear
     }
}
ext.profile=hasProperty('profile')?profile:'dev'
def configFile=file('profile.properties')
def config=new ConfigSlurper(profile).parse(configFile.toURL())
ext.config=config
配置(子项目){
profile=profile//将属性注入子项目
println配置文件
任务构建器