Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Include Properties File

Tags:

gradle

You could do it using the java syntax, e.g.:

Properties props = new Properties()
InputStream ins = new FileInputStream("/path/file.properties")
props.load(ins)
ins.close()

This should work in any groovy script. There might be a more "groovy" way of doing it though, using closures or some other fancy shortcut.

EDIT: "in" is a reserved word in groovy. A variable can't be named that way, renaming it to "ins"


I would actually recommend using Gradle's default properties file. If you put the properties in gradle.properties in the same directory as the build.gradle, they will automatically be available.

See the user guide.


What you propably are looking for is something like:

    Properties props = new Properties()
    props.load(new FileInputStream("$project.rootDir/profile/"+"$environment"+".properties"))
    props.each { prop ->
      project.ext.set(prop.key, prop.value)
    }

Your properties should then be accessible directly through their name. I.e.:

  println "$jdbcURL"
  println project.jdbcURL

Theres probably a reason (shadowing?) why its a bad idea to do this? Not sure why its not supported out of the box or can be found somewhere else.


For Gradle 1.x (deprecated in Gradle 2.x)

To include your .properties file you can just use something like this:

    apply from: "version.properties"

and that's it!