TaskExecutionListener executing multiple times per task

I have a TaskExecutionListener implemented, similar to the following with irrelevant functionality removed:

@Slf4j
class TaskListener implements TaskExecutionListener {

    Clock clock
    def timings = []

    @Override
    void beforeExecute(Task task) {
        clock = new Clock()
    }

    @Override
    void afterExecute(Task task, TaskState taskState) {

        def ms = clock.getTimeInMs()
        def st = clock.getStartTime()

        log.info "${task.path} took ${ms}ms"
        }
    }
}

This executes multiple times per task. I am unable to determine why.

=> gradle -is -p obiee/brokerage tasks
... (truncated)...
:obiee/brokerage:tasks took 47ms
:obiee/brokerage:tasks took 1021ms
:obiee/brokerage:tasks took 1823ms
:obiee/brokerage:tasks (Thread[Daemon worker Thread 4,5,main]) completed. Took 2.723 secs.

can you share how you register this listener?

The following code is implemented in a Gradle plugin:

// from the plugin
void apply(Project project) {

        // apply the Gradle extension plugin
        applyExtension(project)

        project.afterEvaluate {
            // I do some stuff in here, excluded as irrelevant
        }

        // add the custom Task Listener that streams Task records
        project.gradle.taskGraph.addTaskExecutionListener new TaskListener()
    }

do you probably apply this plugin on multiple projects within a multiproject build? the taskgraph is build - global, I mean one taskgraph per build, but projects are not.

1 Like

That appears to be it. I have the root project, and two subprojects in the sample I’m using. I just turned off one of the subprojects, and I got two records instead of three.

Is there a solution to this? I’m only executing a task for a single subproject… directly specifying it with -p… which makes me think I’m calling a single task. Can you think of a way to boil this down to a single call?

Thanks Rene… I appreciate it.

I figured it out Rene… Just removing the Plugin application from the “allprojects” section and putting it into “subprojects” solved this… I’m not sure why I was initially applying it there… but this has been corrected.

Thanks.