Project Properties
- Project Properties are global to the project and can be defined:
- In the
gradle.properties
file - OR passed via the command line.
- Command line properties override
gradle.properties
.
- Command line properties override
- In the
- Accessed in build scripts using
project.propertyName
.
Example (gradle.properties
file):
version=1.0.0
Access in build script (build.gradle.kts
):
tasks.register("go") {
doLast {
println("Project version: ${project.version}")
}
}
Glass thought Sandbox Snapshot
Command to reproduce:
gt.sandbox.checkout.commit ac59f88 \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew go"
Recorded output of command:
> Task :lib:go
Project version: 2.3.1
BUILD SUCCESSFUL in 438ms
1 actionable task: 1 executed
Gradle command line project properties
From Command Line
Go to text ā
To pass project properties via the command line in Gradle, you can use the -P
flag followed by the property name and value. This allows you to override or define properties at runtime without modifying the gradle.properties
file.
Example of Passing a Project Property via the Command Line:
You can pass the version
property like this:
Glass thought Sandbox Snapshot
Command to reproduce:
gt.sandbox.checkout.commit d043a3a \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew go -Pversion=9.9"
Recorded output of command:
> Task :lib:go
Project version: 9.9
BUILD SUCCESSFUL in 360ms
1 actionable task: 1 executed
This will override any version
property defined in the gradle.properties
file and set the version for this specific build invocation.
Key Points:
- Use
-PpropertyName=value
to pass properties via the command line. - Properties passed this way override those defined in the
gradle.properties
file. - Access the property in your build script using
project.propertyName
.
Children
Backlinks