Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Gradle Goodness: Define System Properties in gradle.properties File

DZone's Guide to

Gradle Goodness: Define System Properties in gradle.properties File

· Java Zone
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

To define system properties for our Gradle build we can use the command line option --system-prop or -D. But we can also add the values for system properties in the gradle.properties file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp. the property is turned into a system property. For a multi-module build only system properties defined in thegradle.properties file in the root of the project structure are used, others are ignored.

In the following build script we have the task showSystemProperty. Inside the task we assert the value of the system property sample and the project property sample:

// Simple task to show 
// some properties.
task showSystemProperty << {

    // System property 'sample' is set 
    // in gradle.properties file.
    assert System.properties.sample == 'Gradle is gr8'

    // Regular project property set
    // in gradle.properties file.
    assert project.sample == 'Gradle is great'

}

We can run the following command line command to make sure the assertions are true:$ gradle --system-prop "sample=Gradle is gr8" --project-prop "sample=Gradle is great" showSystemProperty.

Or we could create the following gradle.properties file in our project directory:

systemProp.sample = Gradle is gr8
sample = Gradle is great

Now we can run $ gradle showSystemProperty and the assertions are true.

Written with Gradle 2.3.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}