在Gradle Tooling API中将varargs作为第二个参数传递给Groovy

时间:2016-11-07 10:07:30

标签: gradle groovy gradle-tooling-api

我正在尝试执行Gradle Tooling API,但我无法调用addProgressListner()方法,因为我在编译期间遇到了令人惊讶的问题:

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}"
    }
}

由于未正确识别vararg,编译失败

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)

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我猜测ApolloBuildProgressListener正在实施org.gradle.tooling.ProgressListener,应该是org.gradle.tooling.events.ProgressListener。试试这个:

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

请注意,BuildLauncher有四种addProgressListener(...)方法。一个接受org.gradle.tooling.ProgressListener,另一个接受org.gradle.tooling.events.ProgressListener