Gradle: 保存 属性 文件

Gradle: save property file

我在我的项目中使用 NewRelic。它需要 newrelic.properties 个带有应用程序令牌的文件。我需要使用不同的令牌创建多个构建。在 gradle 中,我使用下一个片段生成属性:

Properties props = new Properties()
props.setProperty("com.newrelic.application_token","MYTOKEN")

如何将它保存到我的应用程序文件夹?我需要 "newrelic.properties" 根文件夹中带有文本 "com.newrelic.application_token=MYTOKEN" 的文件。

您可以使用 -P 标志来指示构建配置文件:

gradle clean build -Pprofile=dev

然后在构建脚本中使用它:

File propsFile = new File('/libs/newrelic.properties')
if (project.getProperty('profile') == "dev") {
    println "Target environment: $profile"
    Properties props = new Properties()
    props.setProperty("com.newrelic.application_token","DEV_TOKEN")
    props.store(propsFile.newWriter(), null)
}

我使用更简单的方法:

ant.propertyfile(file: "newrelic.properties") {
            entry( key: "com.newrelic.application_token", value: "AA165ad2e3e518***********************sd2")
        }

来自这个: How can I transform a .properties file during a Gradle build?