Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I transform a .properties file during a Gradle build?

Tags:

gradle

As part of a deploy task in Gradle, I want to change the value of a property in foo.properties to point to a production database instead of a development database.

I'd rather not replace the whole file outright, as it's rather large and it means we would have to maintain two separate versions that only differ on a single line.

What is the best way to accomplish this?

like image 374
Brant Bobby Avatar asked Jan 27 '12 15:01

Brant Bobby


People also ask

How do I pass system properties to Gradle build?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.

How do you pass JVM arguments in Gradle?

Try to use ./gradlew -Dorg. gradle. jvmargs=-Xmx16g wrapper , pay attention on -D , this marks the property to be passed to gradle and jvm. Using -P a property is passed as gradle project property.


3 Answers

You can use the ant.propertyfile task:

    ant.propertyfile(
        file: "myfile.properties") {
        entry( key: "propertyName", value: "propertyValue")
        entry( key: "anotherProperty", operation: "del")
    }
like image 140
David Avatar answered Oct 09 '22 03:10

David


You should be able to fire off an ant "replace" task that does what you want: http://ant.apache.org/manual/Tasks/replace.html

ant.replace(file: "blah", token: "wibble", value: "flibble")
like image 7
Shorn Avatar answered Oct 09 '22 03:10

Shorn


Create a properties object, then create the file object with the targeted properties file path, load the file on the properties object with load, set the desired property with setProperty, and save the changes with store.

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

var.load(myfile.newDataInputStream())
var.setProperty("db", "prod")
var.store(myfile.newWriter(), null)
like image 5
user2543120 Avatar answered Oct 09 '22 03:10

user2543120