In Java you can pass system properties from the command line like this:
java -D MyProperty=foo MyClass
And you can then get them in your code like so:
public class MyClass { public String getMyProperty() { return System.getProperty("MyProperty"); } }
Pretty easy, no?
You can pass system arguments the same way with Gradle:
gradle -D MyProperty=foo run
But what if you want to manipulate or use those properties in Gradle first?
Instead of -D you can use -P to pass properties to the Gradle object, and then you can do whatever you want with it.
gradle -P MyProperty=foo MyClass
And then you can use your properties in Gradle and then pass them to the System properties thusly:
task setProperty << { if (project.hasProperty("ENV")) { println "project has property ENV" System.properties["ENV"] = "$ENV" } else { println "project does not have property ENV" System.properties["ENV"] = "dev" } println "ENV: " + System.properties["ENV"] }
Some references:
https://docs.gradle.org/current/userguide/writing_build_scripts.html#N10FDD
https://docs.gradle.org/current/userguide/build_environment.html
http://mrhaki.blogspot.com/2010/10/gradle-goodness-pass-command-line.html
http://mrhaki.blogspot.com/2010/09/gradle-goodness-different-ways-to-set.html