Table of Contents

Gradle - Task

About

A build process execute a graph of tasks.

Task may just be:

Example

tasks.register("Hello") {
  println("Hello")
}
gradlew Hello
Hello

Model

Tasks themselves consist of:

A task should never call another task.

Tasks should define inputs and outputs to get the performance benefits of incremental build functionality.

Output

When declaring the outputs of a task, make sure that the directory for writing outputs is unique among all the tasks in your project.

Intermingling or overwriting output files produced by different tasks compromises up-to-date checking causing slower builds. In turn, these filesystem changes may prevent Gradle’s build cache from properly identifying and caching what would otherwise be cacheable tasks.

Management

Create

Task are provided by:

Built-in

Example for copy

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}

Register

Kotlin

tasks.register("logInfo") {
  logging.captureStandardOutput(LogLevel.INFO)
  doFirst {
    println("A task message which is logged at INFO level")
  }
}

Default

task echo(description: 'Default task') << {
    println 'Hello'
}
defaultTasks 'echo'

List

gradlew tasks 
# or
gradlew tasks --all
gradle :services:webservice:tasks

Property

tasks.withType(JavaCompile) {
        options.encoding = 'UTF-8'
}
val testTask =  tasks.getByName<Test>("test");
testTask.failFast = true;
tasks.test {
   failFast = true;
}

Execute

gradle -q taskName

where:

Exclude

gradle [-x|--exclude-task]

Cache

Ignore previously cached task results.

gradle --rerun-tasks

Help

gradlew help --task <task>

Disable

Run the builds with all task actions disabled.

gradle [-m|--dry-run]

Dependency

https://docs.gradle.org/current/userguide/tutorial_using_tasks.html#sec:task_dependencies

tasks.register("hello") {
    doLast {
        println("Hello world!")
    }
}
tasks.register("intro") {
    dependsOn("hello")
    doLast {
        println("I'm Gradle")
    }
}

Cache

See Gradle - Cache (.gradle)