You can pass command line arguments to gradlew
(the Gradle wrapper) in the same way you would pass arguments to any other command line tool. Here's how:
Passing Arguments to Gradle Tasks
-
Directly after the task name: You can append arguments to the task name, separated by spaces.
./gradlew build -x test -Dmy.custom.property=value
build
: The Gradle task to execute.-x test
: Excludes thetest
task from execution.-Dmy.custom.property=value
: Sets a system property namedmy.custom.property
to the valuevalue
.
-
Using
--args
: You can pass arguments to the task using the--args
flag../gradlew build --args "-x test -Dmy.custom.property=value"
build
: The Gradle task to execute.--args "-x test -Dmy.custom.property=value"
: Passes arguments to thebuild
task.
Passing Arguments to the Gradle Wrapper
-
Using
-P
: You can set Gradle properties using the-P
flag../gradlew -Pmy.custom.property=value build
-Pmy.custom.property=value
: Sets the Gradle propertymy.custom.property
to the valuevalue
.build
: The Gradle task to execute.
Accessing Arguments in Gradle Scripts
You can access command line arguments in your Gradle build scripts using the project.properties
map.
task myTask {
doLast {
println "Custom property value: ${project.properties['my.custom.property']}"
}
}
Practical Insights
- Use
-x
to exclude tasks from execution. - Use
-D
to set system properties, which are available to all tasks. - Use
-P
to set Gradle properties, which are specific to the current build. - Access arguments in your Gradle scripts using the
project.properties
map.