System Property (in Gradle)

System Properties:

  • System properties can be defined at runtime by passing them as JVM arguments. They can be accessed using System.getProperty(). Note: System Properties are #not-tracked-by-gradle .

    Example:

    gradle build -DmyProperty=value
    

    Access in build script:

    val myProperty = System.getProperty("myProperty")
    println("System property: $myProperty")
    

Not tracked by gradle

Not tracked by gradle

#not-tracked-by-gradle

Correct, system properties are not tracked by Gradle by default. Just like environment variables, changes to system properties won't automatically trigger Gradle tasks to be re-executed, and they aren’t included in Gradle’s up-to-date checking or incremental build system.

Why System Properties Aren’t Tracked:

  • System properties are typically set outside of the Gradle build process (e.g., through JVM arguments or command line options like -DmyProperty=value), so Gradle does not track them as part of its normal task input/output model.
  • If a task depends on system properties, but those properties change between builds, Gradle won't automatically detect this and re-execute the task unless you explicitly tell Gradle to treat those properties as inputs.

Workaround: Manually Declare System Properties as Task Inputs

To ensure Gradle considers system properties in its up-to-date checks and re-executes tasks when system properties change, you can declare them as task inputs manually, similar to environment variables.

Example: Tracking a System Property as a Task Input

tasks.register("printSysProp") {
    val sysProp = System.getProperty("myProperty")

    // Declare the system property as an input
    inputs.property("myProperty", sysProp)
    outputs.file("build/output.txt")    // Output file

    doLast {
        val outputFile = file("build/output.txt")
        outputFile.writeText("System property: $sysProp")

        println("doLast is running... System property: $sysProp")
    }
}
Glass thought Sandbox Snapshot

Command to reproduce:

gt.sandbox.checkout.commit 05d2b11 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "gs"

Recorded output of command:

On branch sandbox-commit-d7ac1d1
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   lib/build.gradle.kts

no changes added to commit (use "git add" and/or "git commit -a")

In this example:

  • The system property myProperty is declared as a task input using inputs.property().
  • Gradle will now track this property, and if it changes, the task will be re-executed.

How to Pass a System Property via Command Line:

./gradlew printSysProp -DmyProperty=someValue

Key Points:

  • System properties are not tracked by default, meaning they won’t trigger task re-execution if they change.
  • To ensure a task is re-executed when a system property changes, you need to manually declare the system property as a task input using inputs.property().
  • Once declared as a task input, Gradle will include the system property in its up-to-date checks.

Conclusion:

Just like environment variables, system properties are not automatically tracked by Gradle. However, you can manually declare system properties as task inputs if they are critical to the task’s behavior and you want Gradle to re-run the task when they change.


Children
  1. System Properties Not Tracked by Gradle

Backlinks