Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use command line input to select a properties file to use in a gradle build?

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.

like image 575
Marcus Stemple Avatar asked Sep 16 '25 17:09

Marcus Stemple


1 Answers

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
like image 157
mcarlin Avatar answered Sep 18 '25 09:09

mcarlin