There are multiple ways to set a property (project property) in gradle.

The first and the very easy way to set is use the –project-property (or -P) option of the gradle command. Let us try that with a simple example. Create a build.gradle file with the below print statement.

println myprop

Run the below command and you should be getting output similar as seen below :

gradle  -Pmyprop=learning_gradle

learning_gradle
:help

Welcome to Gradle 1.11.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 4.87 secs

Now this is fairly simple and easy. But what if someone runs the build and forgets to set the property. What happens is shown below :

gradle

FAILURE: Build failed with an exception.

* Where:
Build file '/scratch/skgupta/git_storage/emdi/try.gradle' line: 2

* What went wrong:
A problem occurred evaluating root project 'emdi'.
> Could not find property 'myprop' on root project 'emdi'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.076 secs

Yes! gradle fails to run the build.

Setting the value permanently

Here is the second way to permanently store the variable inside the file itself. So lets modify our build.gradle to look something like :

myprop=learning_gradle
println myprop

Now you can run the gradle build without worry to set the property explicitly.

gradle.properties

Going a step further, gradle allows you to keep the data separate out of your code into a properties file named as gradle.properties. So to make the code look clean, we create gradle.properties under the same directory as build.gradle, and add the property, myprop there. The files would look like below

myprop=learning_gradle
println myprop

Precedence of evaluation

That solved your problem and in a neat way! But a month later, you realize you want to build differently based on the different values of myprop. So your build.gradle has grown like this :


if (myprop.equals("artifactory")) {
println "I will be building and publishing to artifactory."
}
else if (myprop.equals("maven") {
println "I will build and publish to local maven repository."
}

Now you always want to publish to local maven repository unless otherwise your code is tested locally and ready to be published to artifactory.

Manually

What would you do ?  Set the myprop value to maven in the gradle.properties, and when ready to publish to artifactory change it to ‘artifactory’.

And remember to revert back the value to ‘maven’ so that you don’t commit this change to your version control system and make ‘artifactory’ as default.

This is so much human error prone!

Command line option

Why not try the –project-property option we used earlier to override the value in gradle.properties. Let us try. Set the value of myprop to maven and run this command:


[skgupta@slc03psl emdi]$ gradle -b try.gradle -P myprop=artifactory
I will be building and publishing to artifactory.
:help

Welcome to Gradle 1.11.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 4.78 secs

Environment variable

Gradle also provides an environment variable template which can be used to override values defined in the gradle.properties. Any environment variable set with prefix ORG_GRADLE_PROJECT_ will be made available as gradle property in the project.


setenv ORG_GRADLE_PROJECT_myprop artifactory

[skgupta@skgupta-lap emdi]$ gradle
I will be building and publishing to artifactory.
:help

Welcome to Gradle 1.11.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 4.78 secs

Command line property Vs Environment variable.

That said, what if you set both environment variable and command line, when you also have value set in gradle.properties; The answer is value set in command line will take precedence over both environment variable and the gradle.properties

Evaluation Precedence

So the evaluation of gradle property value goes as :

  1. Values in gradle.properties are evaluated first
    1. If this is a multi-project build, the subproject’s gradle.properties are evaluated next.
  2. If there is environment variable matching prefix, ORG_GRADLE_PROJECT_, it will be converted to gradle property and overrides any existing property value.
  3. Project properties set from command line are evaluated next and if the property exists from step 1 and step 2 their values will be overridden
    1. If same property is set multiple times in command line, then last set value will be used.
  4. Lastly and most important, if you are setting the property inside the build.gradle, that will be the final value of the property.