Passing varargs as second argument in Groovy in Gradle Tooling API

(Crazyjavahacking) #1
I am trying to execute the Gradle Tooling API, but I am unable to call the addProgressListner() method as I am experiencing surprising issues during compilation:

buildscript {
    repositories {
        maven { url '' }

    dependencies {
        classpath "org.gradle:gradle-tooling-api:3.1"
        classpath 'org.slf4j:slf4j-simple:1.7.10'

ext {

import org.gradle.tooling.*

task testGradleToolingAPI {
    doLast {
        ProjectConnection projectConnection = GradleConnector.newConnector()
                                                             .forProjectDirectory(GRADLE_PROJECT_HOME as File)

        def operationTypes = [OperationType.TASK] as OperationType[]
        println "operationTypes.class: ${operationTypes.class}"

                         .addProgressListener(new ApolloBuildProgressListener(), operationTypes)

        finally {

class ApolloBuildProgressListener implements ProgressListener {
    void statusChanged(ProgressEvent event) {
        println "Progress event: ${event.description}"

The compilation fails as the vararg is not correctly recognized:

operationTypes.class: class [;
:testGradleToolingAPI FAILED

FAILURE: Build failed with an exception.

* Where:
Build file '/home/martin/devel/tmp/gradle-tooling-api/build.gradle' line: 36

* What went wrong:
Execution failed for task ':testGradleToolingAPI'.
> No signature of method: org.gradle.tooling.internal.consumer.DefaultBuildLauncher.addProgressListener() is applicable for argument types: (ApolloBuildProgressListener, [;) values: [ApolloBuildProgressListener@7c2dfa2, [TASK]]
  Possible solutions: addProgressListener(, [;), addProgressListener(org.gradle.tooling.ProgressListener), addProgressListener(, addProgressListener(, java.util.Set)

What am I missing?

(Stefan Oehme) #2

The problem is not the varargs. ApolloBuildProgressListener is implementing the wrong interface. There are two, an old one in the org.gradle.tooling package and the new one in the package (which is what you need).