I have a java project that can use one of a few different properties files based on the environment I will build it for (eg. development.properties will give me properties that are good for a development environment), and I want to be able to control which one gradle includes in the build by selecting it at the command line. I know you can select a properties file to include in the build by putting something like this in your build.gradle file:
Properties props = new Properties()
props.load(new FileInputStream("/path/file.properties"))
But I want to be able to select which properties file to use on the fly, as I build the project.
There are a number of ways you can pass this information into your gradle build, command line properties being one.
If you check out https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_properties_and_system_properties
you'll see there's a way to pass in command line arguments that you can access inside your gradle script.
For Example:
./gradlew -PenvFile="/path/file.properties" clean jar
You can later access this from the gradle by referencing it by the key you chose such as
task printProps << {
println envFile
}
which outputs
./gradlew -PenvFile="/path/test.properties" printProps
:printProps
/path/test.properties
BUILD SUCCESSFUL
Total time: 0.747 secs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With