Execution

In the Execution phase of Gradle, the focus is primarily on executing task actions, but there are a few additional important aspects beyond just the task actions:

Task Execution Plan:

  • Gradle doesn't execute all tasks. During the Configuration phase, Gradle determines which tasks need to be executed by analyzing task dependencies, user-specified tasks, and the current state of inputs/outputs (up-to-date checks).
  • Only tasks that are part of the task execution plan (i.e., those that are explicitly requested or required by dependencies) will be executed.

UP-TO-DATE Checks (Task Avoidance):

  • Before executing a task, Gradle checks whether the task is up-to-date by comparing its inputs and outputs. If the inputs haven't changed and the outputs are still valid, the task is skipped.
  • This is an optimization feature to avoid unnecessary execution and speed up builds.

Incremental Build

Parallel Execution (Optional):

  • If enabled, Gradle can execute tasks in parallel if they have no dependencies on each other. This can improve build performance, especially in multi-project or large builds.
  • You can enable parallel execution in the gradle.properties file:
    org.gradle.parallel=true
    

Task Finalization:

  • Some tasks may have finalizer tasks, which are executed after the main task completes. These can be used for cleanup or notification purposes.

  • You can specify finalizer tasks using finalizedBy:

    tasks.register("mainTask") {
        doLast {
            println("Main task executed")
        }
    }
    
    tasks.register("cleanupTask") {
        doLast {
            println("Cleanup task executed")
        }
    }
    
    tasks.named("mainTask") {
        finalizedBy("cleanupTask")
    }
    

Task Execution State:

  • Gradle keeps track of each task's state (whether it was executed, skipped, failed, etc.). This helps provide detailed build reports and logs, which are useful for debugging and monitoring the build process.

Summary of the Execution Phase:

  1. Task actions are run for tasks included in the task execution plan.
  2. Up-to-date checks prevent redundant tasks from running.
  3. Parallel execution may occur if enabled and no dependencies prevent it.
  4. Finalizer tasks can run after the main task completes.
  5. Task states are tracked to provide insights into the build process.

In essence, while task actions are the primary focus, Gradle also handles task dependency resolution, optimization checks, and optional features like parallel and finalizer tasks in the Execution phase.


Children
  1. Incremental Build
  2. Task Actions in Gradle (Execution Phase)
  3. UP-TO-DATE Checks (Task Avoidance)

Backlinks