How to read a properties files and use the values in project Gradle script?
Solution 1:
If using the default gradle.properties
file, you can access the properties directly from within your build.gradle
file:
gradle.properties
:
applicationName=Admin
projectName=Hello Cool
build.gradle
:
task printProps {
doFirst {
println applicationName
println projectName
}
}
If you need to access a custom file, or access properties which include .
in them (as it appears you need to do), you can do the following in your build.gradle
file:
def props = new Properties()
file("build.properties").withInputStream { props.load(it) }
task printProps {
doFirst {
println props.getProperty("application.name")
println props.getProperty("project.name")
}
}
Take a look at this section of the Gradle documentation for more information.
Edit
If you'd like to dynamically set up some of these properties (as mentioned in a comment below), you can create a properties.gradle
file (the name isn't important) and require it in your build.gradle
script.
properties.gradle
:
ext {
subPath = "some/sub/directory"
fullPath = "$projectDir/$subPath"
}
build.gradle
apply from: 'properties.gradle'
// prints the full expanded path
println fullPath
Solution 2:
We can use a separate file (config.groovy
in my case) to abstract out all the configuration.
In this example, we're using three environments viz.,
- dev
- test
- prod
which has properties serverName, serverPort and resources. Here we're expecting that the third property resources may be same in multiple environments and so we've abstracted out that logic and overridden in the specific environment wherever necessary:
config.groovy
resources {
serverName = 'localhost'
serverPort = '8090'
}
environments {
dev {
serverName = 'http://localhost'
serverPort = '8080'
}
test {
serverName = 'http://www.testserver.com'
serverPort = '5211'
resources {
serverName = 'resources.testserver.com'
}
}
prod {
serverName = 'http://www.productionserver.com'
serverPort = '80'
resources {
serverName = 'resources.productionserver.com'
serverPort = '80'
}
}
}
Once the properties file is ready, we can use the following in build.gradle
to load these settings:
build.gradle
loadProperties()
def loadProperties() {
def environment = hasProperty('env') ? env : 'dev'
println "Current Environment: " + environment
def configFile = file('config.groovy')
def config = new ConfigSlurper(environment).parse(configFile.toURL())
project.ext.config = config
}
task printProperties {
println "serverName: $config.serverName"
println "serverPort: $config.serverPort"
println "resources.serverName: $config.resources.serverName"
println "resources.serverPort: $config.resources.serverPort"
}
Let's run these with different set of inputs:
-
gradle -q printProperties
Current Environment: dev serverName: http://localhost serverPort: 8080 resources.serverName: localhost resources.serverPort: 8090
-
gradle -q -Penv=dev printProperties
Current Environment: dev serverName: http://localhost serverPort: 8080 resources.serverName: localhost resources.serverPort: 8090
-
gradle -q -Penv=test printProperties
Current Environment: test serverName: http://www.testserver.com serverPort: 5211 resources.serverName: resources.testserver.com resources.serverPort: 8090
-
gradle -q -Penv=prod printProperties
Current Environment: prod serverName: http://www.productionserver.com serverPort: 80 resources.serverName: resources.productionserver.com resources.serverPort: 80