Gradle Task Execution Overview

Key Points on Gradle Task Execution

1. Not All Tasks Run by Default

  • Running ./gradlew tasks --all lists all available tasks but does not execute them.
  • Gradle only runs tasks that are explicitly invoked or required by dependencies.

2. How Tasks Get Executed

✅ Explicit Invocation

./gradlew myCustomTask

This manually runs myCustomTask if it exists.

🔄 Task Dependencies

Tasks can be linked via dependsOn, causing them to execute automatically:

task myTask {
    dependsOn 'compileJava'
}

Now, running ./gradlew myTask also runs compileJava.

⚙️ Gradle Lifecycle Tasks

Certain high-level tasks trigger multiple others:

./gradlew build

This runs:

  • compileJava
  • processResources
  • test
  • jar
  • assemble (if needed)

3. Why Some Tasks Never Run

  • They are debugging or utility tasks (e.g., tasks, dependencies).
  • They lack dependencies linking them to the main build flow.
  • Gradle caching skips them if outputs didn’t change.

4. How to Check What Will Run

To see what tasks will execute without actually running them:

./gradlew <task-name> --dry-run

Example:

./gradlew build --dry-run

This simulates execution and prints the task sequence.


TL;DR

  • Gradle doesn’t run all tasks by default—only invoked or required ones.
  • Use dependsOn to link tasks and ensure execution order.
  • Lifecycle tasks (build, test) trigger necessary dependencies.
  • Check execution with --dry-run before running a full build.