Linked Questions

Popular Questions

I am trying to improve the performance of our Gradle builds and discovered the Gradle Task Configuration Avoidance API (https://docs.gradle.org/current/userguide/task_configuration_avoidance.html). It allows to postpone the creation and configuration of a task, unless it is really needed. This might save a lot of startup time and as we call Gradle multiple times during a build, this could amount to considerable time savings.

We developed some plugins for internal use and I put an effort into changing how I define the tasks to avoid creation when not needed. I want to test if my changes are successful and the task instantiation is delayed.

Simple example of how to create a task without instantiating it:

class MyTask extends DefaultTask {
}

TaskProvider customTask = tasks.register("customAction", MyTask) {
    println "task configured!"  // configuration time output
    doLast {
        println "action 1!"    // execution time output
    }
}

// configuration like this avoids task instantiation
tasks.named("customAction") {
    doLast {
        println "action 2!"
    }
}

tasks.withType(MyTask).configureEach {
    doLast {
        println "action 3!"
    }
}

Executing gradle help does not print the "task configured!" message, while gradle customAction does.

To make sure I do not accidentally trigger task instantiation, I would like to write tests for our plugins. But I could not find a way to determine if a task is instantiated or not.

I know about Build Scans (https://guides.gradle.org/creating-build-scans/), but our corporate guidelines are strict and clearance is pending, so I can not use it for now. Also, I do not see a way to use it in tests.

Is there a way to

  • get a list of created/instantiated tasks from the Gradle project?
  • or is there any property on Task or TaskProvider showing whether the task has been created/instantiated?
  • or can buildscans be used offline somehow?

It would be cool, if the solution could be used in the plugin's test code, but manual evaluation would also be valuable.

Related Questions