A2oz

How to Pass Command Line Arguments for Gradlew?

Published in Gradle 2 mins read

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

  1. 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 the test task from execution.
    • -Dmy.custom.property=value: Sets a system property named my.custom.property to the value value.
  2. 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 the build task.

Passing Arguments to the Gradle Wrapper

  1. Using -P: You can set Gradle properties using the -P flag.

    ./gradlew -Pmy.custom.property=value build
    • -Pmy.custom.property=value: Sets the Gradle property my.custom.property to the value value.
    • 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.

Related Articles