Gradle - Include Properties File
GradleGradle Problem Overview
How would I include a properties file in Gradle?
For example in Ant, I could do the following:
<property file="${basedir}/build.properties" />
Gradle Solutions
Solution 1 - 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"
Solution 2 - Gradle
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.
Solution 3 - Gradle
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.
Solution 4 - Gradle
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!
Solution 5 - Gradle
Here is how I include a .csv file in my .jar that 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, that is done independently of Gradle.
Solution 6 - Gradle
Use GRADLE_USER_HOME env variable to set gradle's home directory. Put there gradle.properties file and set parameters.
https://docs.gradle.org/current/userguide/build_environment.html > The properties file in the user's home directory has precedence over > property files in the project directories.
Solution 7 - Gradle
The accepted answer works unless you want to add the properties to the project, since this is the first search result I'll add my solution which works in gradle 3.x. An easy way to do it is through ant.
ant {
property(file: 'properties.file')
}
If you need to convert the property to Camel Case for easy access in your script (remember dot based syntax expects and actual object to 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 value if it's not specified:
def propertyWithDefault = project.getProperties().get("dot.property.from.file","defaultValue");
By using this method, at least with IntelliJ/Android Studio, the IDE will link your properties file and gradle script and add completion, reference checking, etc.
Solution 8 - Gradle
I have been sorely missing this feature of Ant, especially the in-property replacement like work.dir=${basedir}/work
. With some of the large builds I work with that perform lots of environment setup for automated tests, there are some tasks that just need user and environment configurable settings. The simple "gradle.properties" just isn't enough, especially for settings that are shared across multiple projects.
I put together an example gradle file that manages a reasonable property loading scheme: load-properties.gradle. This lets the project define exactly the properties it uses in a default-properties.gradle
file, and allows users to define their local settings to override them. Because the "property" files read in are gradle scripts, you can include extra logic in them, and the values aren't limited to just strings.
Additionally, it allows for defining a per-environment properties file to use (such as "mac", or "jenkins-machine", or "remote") by passing in a "-Penv=(env name)" parameter to gradle.
Solution 9 - Gradle
In your task (jar or any other task) you can do this way to include any file into the destination folder
jar{
//jar task related code
from("src/main/......(source)") {
include 'xxx.properties' into('com/.....(destination)')
}
}
Solution 10 - Gradle
This is for Kotlin DSL (build.gradle.kts):
import java.util.*
// ...
val properties = Properties().apply {
load(rootProject.file("local.properties").reader())
}
val prop = properties["propName"]
In Android projects (when applying the android plugin) you can also do this:
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties
// ...
val properties = gradleLocalProperties(rootDir)
val prop = properties["propName"]