Gradle - include properties file

How to include a properties file in Gradle?

For example, in Ant, I could do the following:

<property file="${basedir}/build.properties" /> 
+50
gradle
Jul 31 '12 at 21:37
source share
8 answers

You can do this using java syntax, for example:

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

This should work in any groovy script. Perhaps there is a more β€œgroovy” way using a closure or other bright shortcut.

+57
Aug 01 '12 at 1:17
source share

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

See user manual .

+40
Aug 01 2018-12-12T00:
source share

What you are looking for is similar to:

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

Then your properties should be available directly through their name. I.e:.

  println "$jdbcURL" println project.jdbcURL 

Perhaps the reason (shadowy?) Is why its a bad idea to do this? Not sure why it is not supported out of the box or can be found somewhere else.

+26
Nov 02 '15 at 18:51
source share

This is how I include the CSV file in my .jar, which is Gradle builds:

 sourceSets { main { java { srcDir 'src/main/java' output.classesDir = 'build/classes/main' } resources { srcDir 'src/main/resources' include '*.csv' output.resourcesDir = 'build/resources/main' } } } 

Then, when my application loads the resource, this is done regardless of Gradle.

+11
Nov 19 '12 at 18:32
source share

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

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

  apply from: "version.properties" 

what is it!

+11
Dec 17 '13 at 16:31
source share

Use the env variable for GRADLE_USER_HOME to set the gradle home directory. Place the gradle.properties file and set the parameters.

https://docs.gradle.org/current/userguide/build_environment.html

The properties file in the user's home directory takes precedence over the properties files in the project directories.

+3
Jul 05 '15 at 15:27
source share

I really missed this Ant function, especially the replacement of properties, for example work.dir=${basedir}/work . With some of the large assemblies that I work with that perform many environment settings for automated tests, there are some tasks that require only custom user and environment settings. Simple "gradle.properties" is simply not enough, especially for parameters that are shared between several projects.

I have compiled an example gradle file that controls a reasonable scheme for loading properties: load-properties.gradle . This allows the project to pinpoint the properties that it uses in the default-properties.gradle file, and allows users to define their local settings to override them. Since the read property files are gradle scripts, you can include additional logic in them, and the values ​​are not limited to just strings.

In addition, it allows you to define a properties file for each environment (for example, "mac" or "jenkins-machine" or "remote"), passing the parameter "-Penv = (env name)" to gradle.

+2
Sep 17 '16 at 3:43
source share

The accepted answer works if you do not want to add properties to the project, since this is the first search result, I will add my solution, which works in gradle 3.x. An easy way to do this is through ant.

 ant { property(file: 'properties.file') } 

If you need to convert the property in Camel Case for easy access to your script (remember that the point-based syntax of the points and the actual object exist in gradle):

 println "property value:{dot.property.from.file}" //this will fail. def camelCaseProperty = project.getProperties().get("dot.property.from.file") println "camelCaseProperty:${camelCaseProperty}" //this wont fail 

If you need a default if not specified:

 def propertyWithDefault = project.getProperties().get("dot.property.from.file","defaultValue"); 

Using this method, at least with IntelliJ / Android Studio, the IDE will link your properties file and gradle script and add completion, link checking, etc.

+2
Feb 10 '17 at 18:52
source share



All Articles