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:

  • Actions — pieces of work that do something, like copy files or compile source
  • Inputs — values, files and directories that the actions use or operate on
  • Outputs — files and directories that the actions modify or generate

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

  • In one project directory
gradlew tasks 
# or
gradlew tasks --all
gradle :services:webservice:tasks

Property

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

Execute

gradle -q taskName

where:

  • -q stands for quiet

Exclude

  • Specify a task to be excluded from execution.
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)





Discover More
Card Puncher Data Processing
Gradle

gradle is a build tool that can run: a build (a set of tasks) or a single task gradle/gradle/
Gradle - Api

The gradle api is the core module of Gradle and provides task. The API can be seen in the lang reference
Gradle - Build

A build is a lifecyle task that executes a graph of task Gradle executes build scripts in three fixed phases: Initialization: Sets up the environment for the build and determine which projects will...
Gradle - Cache (.gradle)

Defaults to .gradle in the root project directory. Specify the project-specific cache directory. Ignore previously cached task results. Disables the Gradle build cache. Enables the Gradle...
Gradle - Dependency

dependency management in Gradle of module Type Module...
Gradle - Graph of Task (Dag)

A build execute a graph (dag) of task. This dag is build itself in the second phase of the build execution.
Gradle - Plugin

plugin adds task to Gradle. A gradle distribution comes out of the box with: the base. the gradle core task api. (Ref)...
Gradle - Project Property

Project property is a type property that has a project scope You can access a project property in your build script simply by using its name as you would use a variable. Example: project property...
How to skip a task if another task is executed in Gradle?

This page shows you how you can skip a task if another task is executed. It can happen in a test development when you have some build task you don't want to execute in a dev setting but that you want...
JavaExec - How to execute a main class from the command line with Gradle

The below build file define a JavaExec task with the name myCli and set: the main class the class path the current directory the arguments The dos script that calls the task myCli. This file...



Share this page:
Follow us:
Task Runner