The wrapper and the command line

Gradle · Essentials · 8 min

Every Gradle project is driven from gradlew; knowing the basic commands is table stakes for JVM and Android work.

Gradle projects ship a wrapper: the scripts gradlew (Unix) and gradlew.bat plus gradle/wrapper/gradle-wrapper.properties, which pins the exact Gradle version. Running ./gradlew downloads that version on first use, so every developer and CI machine builds with the same Gradle. Always use the wrapper rather than a globally installed gradle; update it with ./gradlew wrapper --gradle-version 9.1.

Common commands:

  • ./gradlew build compiles, runs tests and assembles artifacts.
  • ./gradlew test, ./gradlew run (with the application plugin), ./gradlew clean.
  • ./gradlew tasks lists available tasks; ./gradlew help --task test explains one.
  • ./gradlew projects and ./gradlew dependencies inspect the build.

Useful flags: -q quiet, --info / --debug for more output, --stacktrace on failures, --offline to use only the local cache, -x test to skip a task, --continue to run as much as possible after a failure, and -p dir to build another directory.

Gradle keeps a daemon process running between builds to stay fast; ./gradlew --stop stops it. Settings for the daemon, parallelism and the JVM live in gradle.properties.

Task names can be abbreviated by camel-case initials: ./gradlew cT runs compileTestJava if unambiguous.

Example

# Pin and fetch the project's Gradle version (committed to git)
cat gradle/wrapper/gradle-wrapper.properties | grep distributionUrl
# distributionUrl=https\://services.gradle.org/distributions/gradle-9.1-bin.zip

./gradlew build                 # compile + test + assemble
./gradlew test --tests "com.acme.*"   # run a subset of tests
./gradlew build -x test         # skip the test task
./gradlew run --args="--port 8080"    # application plugin
./gradlew tasks --all           # every task, including internal ones
./gradlew dependencies --configuration runtimeClasspath
./gradlew clean build --offline --stacktrace
./gradlew wrapper --gradle-version 9.1   # upgrade the wrapper
./gradlew --stop                # stop the background daemon

Remember

  • Use ./gradlew, never a global gradle, so versions match everywhere
  • build = compile + test + assemble; -x skips a task
  • tasks, dependencies and help --task inspect the build
  • A daemon keeps builds fast between runs

Watch out for

  • Committing a project without the wrapper files, so teammates build with different Gradle versions.

Check yourself

  1. What does ./gradlew build -x test do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.gradle.org/current/userguide/gradle_wrapper.html

Builds on

Nothing: this is a foundation.

Unlocks