Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Getting Started

Gradle Command-Line Basics: Tasks, Flags and Logging

Learn the Gradle command line: run and chain tasks, exclude tasks with -x, abbreviate names, see more output, run dry runs, and use the daemon and build scans.

Whether you use an IDE or not, you will spend a lot of time in a terminal running ./gradlew with different arguments. The command is simple (./gradlew [options] task...) but it has a handful of options worth knowing by heart. This lesson goes through the ones you will use every day, plus the daemon and build scans. Gradle itself is not runnable on the site, so the examples are read-only; two small stand-ins in Java demonstrate how task-name abbreviation and -x exclusion work.

Running tasks

Name one or more tasks after the command. Gradle runs each (and everything it depends on) once, in an order that respects dependencies:

Terminal
./gradlew build
./gradlew clean build
./gradlew test jar

clean deletes the build/ folder. ./gradlew clean build is the "from scratch" build; use it when you suspect stale output, but avoid it as a habit, because it throws away incremental work.

In multi-project builds you target a project with a colon path:

Terminal
./gradlew :app:test           # only the app project
./gradlew test                # test in every project
./gradlew :libs:core:jar      # nested project

Discovering tasks

Terminal
./gradlew tasks               # main tasks, grouped
./gradlew tasks --all         # every task, including internal ones
./gradlew help --task test    # details about one task
./gradlew projects            # the project tree
./gradlew properties          # all properties of the project

help --task is underrated: it prints the task type, its path, options (such as --tests) and its description.

Task name abbreviation

You may type just enough characters of each camelCase word to be unambiguous. ./gradlew cB may select cleanBuild-style names, and ./gradlew coJ could mean compileJava. If more than one task matches, Gradle lists the candidates and stops. Abbreviation is handy interactively but avoid it in scripts, where a new plugin could make the abbreviation ambiguous.

Java Stand-in: camelCase abbreviation matching

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The abbreviation pR matches only processResources because it has two words; processTestResources has three. That is the same disambiguation Gradle performs. (Real Gradle's matcher is a little more flexible; this is a simplified model.)

Excluding tasks with -x

-x (or --exclude-task) removes a task, and anything only it needed, from the run:

Terminal
./gradlew build -x test           # build without running tests
./gradlew build -x test -x check  # exclude several

Use it for quick local iterations, not in CI where skipped tests give false confidence.

Java Stand-in: -x removes a task from the run

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Useful options

Option Effect
-q / --quiet Only errors
-i / --info More detail (for example why a task ran)
-d / --debug Very verbose; for deep debugging
--stacktrace / -s Show the stack trace of a failure
--full-stacktrace / -S Do not filter Gradle's internal frames
--console=plain No animated progress; best in logs
--dry-run / -m Show the tasks that would run, without running them
--rerun-tasks Ignore up-to-date checks, rerun everything
--rerun Rerun only the named task (Gradle 7.6+)
--continue Keep going after a failure to report more problems
--offline Use cached dependencies only
--refresh-dependencies Re-check remote repositories
--parallel / --no-parallel Run independent projects in parallel
-D<prop>=<v> JVM system property
-P<prop>=<v> Project property (see Properties)
--scan Publish a build scan

A dry run is a great first step in an unfamiliar build:

Terminal
./gradlew build --dry-run
TEXT
:app:compileJava SKIPPED
:app:processResources SKIPPED
:app:classes SKIPPED
:app:jar SKIPPED
...
:app:build SKIPPED

Every task is listed with SKIPPED because dry run does not execute anything.

Task status labels

When Gradle prints a task header, a suffix says what happened:

  • no suffix: the task executed;
  • UP-TO-DATE: inputs and outputs unchanged, skipped;
  • FROM-CACHE: output was restored from the build cache;
  • NO-SOURCE: the task has nothing to process (for example an empty src/main/resources);
  • SKIPPED: excluded with -x, an onlyIf predicate, or dry run;
  • FAILED: the task threw an error.

The Gradle daemon

Starting a JVM for every command is slow, so Gradle keeps a background daemon process alive between builds. The first run is slow; later runs reuse a warm JVM. Check it with ./gradlew --status and stop all daemons with ./gradlew --stop. Daemons stop themselves after a few idle hours. Use --no-daemon only in special cases (for example a one-off container); CI builds that run once often do not benefit from a daemon but the cost is small.

Build scans

Add --scan to publish a detailed report to scans.gradle.com (after accepting the terms) showing timings, dependency trees, test results and failure causes. A scan has a shareable URL, so it is the best way to ask a colleague for help. Do not publish scans of builds that print secrets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command builds the project but skips the test task?

    -x (or --exclude-task) removes the named task from the execution plan.
  2. 2. What does ./gradlew build --dry-run do?

    Dry run prints the execution plan, marking every task SKIPPED.
  3. 3. A task shows FROM-CACHE. What does that mean?

    FROM-CACHE means a matching result was found in the local or remote build cache.
  4. 4. Why does Gradle keep a daemon process?

    JVM start-up and class loading are expensive; the daemon keeps them warm between invocations.

Summary

  • ./gradlew <tasks> runs the tasks and their dependencies; use :project:task paths in multi-project builds.
  • tasks, tasks --all, help --task and projects help you explore a build.
  • -x excludes tasks, --dry-run previews, --continue reports more failures, --rerun-tasks ignores up-to-date checks.
  • -q, -i, -d and --stacktrace control output; --console=plain suits logs.
  • Task labels such as UP-TO-DATE, FROM-CACHE and NO-SOURCE explain what happened.
  • The daemon speeds up repeated builds; --scan creates a shareable report.

Next: Projects and settings.gradle