Gradle properties

Using properties in Gradle can be confusing for a newcomer, because there are different sorts of properties, and some complexities in how they apply to a Gradle build. Firstly, we can use three different types of property:
  1. Gradle properties – specified with -P flag.
  2. Java system properties – specified with -D.
  3. Environment variables.
Gradle -P properties? What are these? Let’s take a look.

Gradle -P properties

You can use Gradle properties on the command line to conditionally change a build. These properties are specified with a -P flag. Do NOT confuse them with java system properties which are set with the -D flag! These properties are the correct choice for making parts of your build conditional. e.g. enable / disable tests like this:
test {
    // disable tests by default
    onlyIf { project.hasProperty('functionalTests') && project.property('functionalTests') == 'true' }
}

Setting properties for tests

Neither -P nor -D properties will be passed to tests by default, as the tests run in a forked JVM which does not get any of the properties of the main JVM.

To pass properties to the forked JVM used for tests, you must either “forward” them (for properties you are expecting to be dynamic, which are specified on the command line), or just set them directly in the test configuration for either JUnit or TestNG.

To forward the value of a system property set on the command line, in your project, in your test configuration block, simply use the systemProperty call to set the value of the system property, from the value of the system property supplied to the main JVM, like this:

test {
    // forward properties to the forked JVM
    systemProperty "docker.username", System.getProperty("docker.username")
    systemProperty "docker.password", System.getProperty("docker.password")
}
This entry was posted in Gradle. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

HTML tags are not allowed.

519,296 Spambots Blocked by Simple Comments