Gradle 3.4:使用ConfigSlurper构建配置文件

时间:2017-06-06 20:23:37

标签: gradle groovy build

我在build.gradle中有以下任务,并且我尝试根据环境从wsdlURL获取config.groovy

loadConfiguration()

task printProps << {
    println "WSDLURL:  $config.wsdlURL"
}

def loadConfiguration() {
    def environment = hasProperty('env') ? env : 'dev'
    System.setProperty('environment', environment)
    println "Environment is set to $environment"

    def configFile = file('config.groovy')
    def config = new ConfigSlurper(environment).parse(configFile.toURI().toURL())
    System.setProperty('config', config) \\line 52
}

下面是我的config.groovy

environments {
    dev {
        wsdlURL = 'https://dev.com/ts/svs.asmx?wsdl'
    }

    qa {
        wsdlURL = 'https://qa.com/ts/svs.asmx?wsdl'        
    }

    stg {
        wsdlURL = 'https://stg.com/ts/svs.asmx?wsdl'        
    }

    prod {
        wsdlURL = 'https://prod.com/ts/svs.asmx?wsdl'
    }
}

当我运行gradle -q -Penv=prod printProps命令时,它失败并出现以下错误

Environment is set to prod

FAILURE: Build failed with an exception.

* Where:
Build file 'build.gradle' line: 52

* What went wrong:
A problem occurred evaluating root project 'SignService'.
> No signature of method: static java.lang.System.setProperty() is applicable fo
r argument types: (java.lang.String, groovy.util.ConfigObject) values: [config,
[wsdlURL:https://prod.com/ts/svs.asmx?wsdl]]
Possible solutions: setProperty(java.lang.String, java.lang.String), getProperty
(java.lang.String), getProperty(java.lang.String, java.lang.String), hasProperty
(java.lang.String), getProperties(), getProperties()

下面是我的环境详细信息

------------------------------------------------------------
Gradle 3.4
------------------------------------------------------------

Build time:   2017-02-20 14:49:26 UTC
Revision:     73f32d68824582945f5ac1810600e8d87794c3d4

Groovy:       2.4.7
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_40 (Oracle Corporation 25.40-b25)
OS:           Windows 7 6.1 amd64

看起来gradle无法将groovy对象设置为系统属性,有人可以帮我修复它吗?

1 个答案:

答案 0 :(得分:1)

错误信息非常清楚

System.setProperty(key, value)需要两个String个参数,但您传递的是StringConfigObject

也许你想要

System.properties['config'] = config

或者

System.properties.put('config', config)