How To Pass a Property to Project Build Script

It is sometimes needed to use properties to pass values to a project build script. This avoids to have hardcoded values in the project sources.

Gradle allows to define System Properties with the command line thanks to the -D prefix:

$ ./gradlew build -DmyPropertyName="myPropertyValue"

and use them with the API providers.systemProperty("myPropertyName").get().

For example to define a local VEE Port directory, the project can be configured with:

dependencies {
   microejVee(files(providers.systemProperty("myVeePortPath").get()))
}

and built with:

$ ./gradlew build -DmyVeePortPath="C:\\path\\to\\my\\veePort\\directory"

The providers.systemProperty("myPropertyName") API returns a org.gradle.api.provider.Provider object, which provides other capabilities like:

  • defining a default value if the System Property does not exist: providers.systemProperty("myPropertyName").getOrElse("myDefaultValue")
  • returning null if the value does not exist: providers.systemProperty("myPropertyName").getOrNull().