如何在属性文件中使用 groovy 形式参数

How to use groovy formal parameters in a properties file

我有这样一个属性文件:

envHost=http://some.host
api.host=${envHost}
backoffice.host=${envHost}

然后当我从我的属性文件中得到 api.host 属性 时:

        InputStream input = new FileInputStream(new File(getClass().getResource("/envs/someEnv.properties").toURI()))
        Properties prop = new Properties();
        prop.load(input)
        println prop.'api.host'

我得到 ${envHost} 而不是 http://some.host

如何使用其中的形式参数从属性文件中获取值 http://some.host

如果您可以将属性文件更改为 config-slurper 有效格式:

envHost="http://some.host"
api.host=envHost
backoffice.host=envHost

然后你可以这样做:

def cfg = new ConfigSlurper().parse(getClass().getResource("/envs/someEnv.properties"))
println cfg.api.host

它应该可以工作