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 'https://repo.gradle.org/gradle/libs-releases' }
    }

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

ext {
    GRADLE_PROJECT_HOME = '...'
}


import org.gradle.tooling.*
import org.gradle.tooling.events.OperationType

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

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

        projectConnection.newBuild()
                         .addProgressListener(new ApolloBuildProgressListener(), operationTypes)
                         .run()

        finally {
            projectConnection.close()
        }
    }
}

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

The compilation fails as the vararg is not correctly recognized:

operationTypes.class: class [Lorg.gradle.tooling.events.OperationType;
: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, [Lorg.gradle.tooling.events.OperationType;) values: [ApolloBuildProgressListener@7c2dfa2, [TASK]]
  Possible solutions: addProgressListener(org.gradle.tooling.events.ProgressListener, [Lorg.gradle.tooling.events.OperationType;), addProgressListener(org.gradle.tooling.ProgressListener), addProgressListener(org.gradle.tooling.events.ProgressListener), addProgressListener(org.gradle.tooling.events.ProgressListener, 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 org.gradle.tooling.events package (which is what you need).