Gradle Task

Purpose:

A task in Gradle is the fundamental unit of work in the build system. Tasks perform actions such as compiling code, running tests, or packaging an application. They can be simple, like printing a message, or complex, like building an entire project.

Notes

  • Tasks can have dependencies on other tasks.
  • Tasks execute specific actions in a project’s lifecycle.

Notes

Gradle Life cycle.

Gradle tasks go through several phases:

  1. Initialization: Determines which project and tasks will be part of the build.
    1. Initialization
  2. Configuration: Configures all tasks in the project (even if they are not executed).
    1. Gradle Configuration
  3. Execution: Executes the tasks in the specified order, respecting task dependencies.
    1. Execution
    2. Task Actions in Gradle (Execution Phase)

References

Defining a Task

tasks.register("hello") {
    doLast {
        println("Hello, Gradle!")
    }
}

The doLast block specifies the action to perform when the task is executed. In this case, it simply prints a message to the console.

# To run this task
mac> ./gradlew hello
Glass thought Sandbox Snapshot

Command to reproduce:

gt.sandbox.checkout.commit 192830c \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew hello"

Recorded output of command:


> Task :lib:hello
Hello, Gradle!

BUILD SUCCESSFUL in 394ms
1 actionable task: 1 executed

Task Dependencies

Tasks can depend on other tasks. Gradle will ensure that all dependent tasks are executed in the proper order.

dependsOn (inter task dependency)

Gradle Inter-Task Dependency: dependsOn

dependsOn in Gradle defines a dependency between tasks, ensuring one task runs before another. When a task declares dependsOn, Gradle guarantees that the dependent task is executed first.

dependsOn ensures tasks execute in the correct order by defining dependencies. It's commonly used to ensure tasks that rely on outputs from other tasks run after their dependencies complete.

Usage:

  • Use dependsOn to specify that a task depends on one or more other tasks.

  • Gradle will always execute the dependent tasks before the task that declares the dependency.

    Example:

    tasks.register("compile") {
        doLast {
            println("Compiling code...")
        }
    }
    
    tasks.register("build-stuff") {
        dependsOn("compile")  // 'build' will run after 'compile'
        doLast {
            println("Building project...")
        }
    }
    
Glass thought Sandbox Snapshot

Command to reproduce:

gt.sandbox.checkout.commit ef515ba \
&& cd "${GT_SANDBOX_REPO}" \
&& cmd.run.announce "./gradlew build-stuff"

Recorded output of command:


> Task :lib:compile
Compiling code...

> Task :lib:build-stuff
Building project...

BUILD SUCCESSFUL in 350ms
2 actionable tasks: 2 executed

Key Points:

  • You can add multiple dependencies using dependsOn.
  • Dependencies are executed in the order they are declared.
  • The dependent tasks must complete successfully before the task with dependsOn runs.
  • If any dependent task is skipped, failed, or up-to-date, the task still respects the dependency state.

Task Inputs and Outputs

Declaring inputs and outputs for tasks helps Gradle optimize task execution by reusing the results from previous runs if the inputs haven’t changed.

  • Example:
tasks.register("processFiles") {
    inputs.file("input.txt")
    outputs.file("output.txt")
    
    doLast {
        val inputFile = file("input.txt")
        val outputFile = file("output.txt")
        outputFile.writeText(inputFile.readText())
    }
}

In this example, if input.txt doesn’t change between runs, Gradle will skip the processFiles task and reuse the cached result.

Predefined

Predefined Task Types

Gradle provides many built-in task types that perform common operations, including:

  • Copy: Copies files from one location to another.
  • Exec: Executes an external process. (task<Exec>)
  • JavaCompile: Compiles Java source files.
  • Test: Runs unit tests.

Example Using the Copy Task (Kotlin DSL):

tasks.register<Copy>("copyFiles") {
    from("src/main/resources")
    into("build/output")
}


Children
  1. Define Hello World Task (Gradle)
  2. Predefined Gradle Tasks
  3. Print Task Tree
  4. dependsOn (inter task dependency)

Backlinks