Crazyjavahacking Crazyjavahacking - 24 days ago 11
Groovy Question

Passing varargs as second argument in Groovy in Gradle Tooling API

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?

Answer

I'm guessing that ApolloBuildProgressListener is implementing org.gradle.tooling.ProgressListener when it should be org.gradle.tooling.events.ProgressListener. Try this:

class ApolloBuildProgressListener implements org.gradle.tooling.events.ProgressListener {
   ...
}

Note that the BuildLauncher has four addProgressListener(...) methods. One accepts a org.gradle.tooling.ProgressListener and the other 3 accept a org.gradle.tooling.events.ProgressListener

Comments