如何在Gradle构建期间转换.properties文件?

Bra*_*bby 25 gradle

作为Gradle中部署任务的一部分,我想将属性的值更改foo.properties为指向生产数据库而不是开发数据库.

我宁愿不直接替换整个文件,因为它相当大,这意味着我们必须维护两个不同的版本,这些版本只在一行上有所不同.

完成此任务的最佳方法是什么?

Dav*_*vid 33

您可以使用ant.propertyfile任务:

    ant.propertyfile(
        file: "myfile.properties") {
        entry( key: "propertyName", value: "propertyValue")
        entry( key: "anotherProperty", operation: "del")
    }
Run Code Online (Sandbox Code Playgroud)


Sho*_*orn 7

你应该能够发起一个蚂蚁"替换"任务,做你想做的事:http://ant.apache.org/manual/Tasks/replace.html

ant.replace(file: "blah", token: "wibble", value: "flibble")
Run Code Online (Sandbox Code Playgroud)


use*_*120 5

创建一个属性对象,然后使用目标属性文件路径创建文件对象,使用 load 将文件加载到属性对象上,使用 setProperty 设置所需的属性,并使用 store 保存更改。

def var = new Properties() 
File myfile = file("foo.properties");

var.load(myfile.newDataInputStream())
var.setProperty("db", "prod")
var.store(myfile.newWriter(), null)
Run Code Online (Sandbox Code Playgroud)

  • 请添加一些文档/解释。 (5认同)