How to figure out why a task is executed?

I am looking for a way to figure out why a task is being ran. I know I can use the --dry-run flag to see what tasks would be executed, but I am trying to figure out why a certain task is executing.

The reason I am looking for this is because I use a 3rd party swagger generation plugin and for some reason it executes any time the build task runs. However, I don’t see anything in its source code where it adds itself as a finalizedBy task to build or make build depend on itself. I want to run the swagger generation task on demand, not anytime build executes.

I see it adding itself as a dependsOn classes. But the reason it runs before build continues to allude me:

Is there any way to figure out why this task runs after the build task?

I think because of a few lines down where it adds the tasks output under certain conditions to the artifacts of the archives configuration and thus is a dependency of assemble which is a dependency of build.

You can add the task tree plugin to your build

plugins {
    id "com.dorongold.task-tree" version "1.5"

Then you can run gradle build taskTree

And get output similar to

+--- :assemble
|    \--- :jar
|         \--- :classes
|              +--- :compileJava
|              \--- :processResources
\--- :check
     \--- :test
          +--- :classes
          |    +--- :compileJava
          |    \--- :processResources
          \--- :testClasses
               +--- :compileTestJava
               |    \--- :classes
               |         +--- :compileJava
               |         \--- :processResources
               \--- :processTestResources

If you want this available everywhere, you can put the config into a init script. Eg:

initscript {
    repositories {
        maven { url "" }
    dependencies {
	classpath ""
rootProject {
    apply plugin: com.dorongold.gradle.tasktree.TaskTreePlugin

Just a few notes:

  • Currently the task-tree plugin does not work under Gradle 6.8 or newer. I hopefully will find time soon to fix this.
  • the plugin does not yet detect tasks added by finalizedBy.
1 Like

For some reason I never got notifications that I got responses on this. I appreciate the responses.

The “archives” configuration depending on build sounds very plausible and I am going to take a look at that. (I already have a fix in place just by being smarter about when I apply the swagger plugin…but still very curious about why it runs anytime build does, if it is applied, so going to explore this a bit).

Also, thanks for the pointer to the task tree plugin, I use Gradle 6.8 so can’t use it but will definitely keep an eye on it for my future needs.

but still very curious about why it runs anytime build does, if it is applied, so going to explore this a bit

I explained that, didn’t I?

Yes, by “curious about why it runs build” and “going to explore this a bit” I just meant I was going to experiment with it a bit based on your explanation (i.e. make changes to the swagger plugin to not add the artifact to the archives configuration)

1 Like