33

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?

Brant Bobby
  • 14,956
  • 14
  • 78
  • 115

5 Answers5

41

You can use the ant.propertyfile task:

    ant.propertyfile(
        file: "myfile.properties") {
        entry( key: "propertyName", value: "propertyValue")
        entry( key: "anotherProperty", operation: "del")
    }
David
  • 496
  • 6
  • 8
7

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")
Shorn
  • 19,077
  • 15
  • 90
  • 168
5

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)
user2543120
  • 131
  • 2
  • 4
3

A simple solution is to code a task that uses java.util.Properties to write the file. If you really want to incrementally update the file, you'll have to implement this on your own. Or maybe you find an Ant task that does what you want (all Ant tasks can be used as-is from Gradle). For best results, you should also declare the inputs and outputs of the task, so that Gradle only executes the tasks when the properties file needs to be changed.

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • You can also use Apache commons configurations library which provides a more convenient API (http://commons.apache.org/configuration/userguide/howto_filebased.html). Of course, you'll still have to implement most of the logic yourself. – rodion Jan 28 '12 at 03:39
  • Ant's [PropertyFile task](http://ant.apache.org/manual/Tasks/propertyfile.html) did exactly what I needed. – Brant Bobby Feb 06 '12 at 20:30
2

You can use ReplaceTokens

Say you have a file called db.properties in src/main/java/resources/com.stackoverlow (the location doesn't matter) with the following content

database.url=@url@

Note that the @ surrounding the url text is required.

You can then define this in your build.gradle file.

processResources {
        filter ReplaceTokens, tokens: [
                "url": "https://stackoverflow.com"
        ]
}

When you build your code, this would replace @url@ with https://stackoverflow.com.

If you are only interested in applying this rule to a specific file, you can add a filesMatching

processResources {
    filesMatching('**/db.properties') {
        filter ReplaceTokens, tokens: [
                "url": "https://stackoverflow.com"
        ]
    }
}

See gradle doc for more explanation

ssgao
  • 5,151
  • 6
  • 36
  • 52