Grzegorz P
Grzegorz P

Reputation: 53

Gradle ResultHandler returns null

I want to get results from tasks run by Gradle Tooling. I define ResultHandler, but in method onComplete(Object result) i get null value. After typing gradle cR, I get following output:

:returnString UP-TO-DATE
BUILD SUCCESSFUL
Total time: 0.98 secs
Result received: null
:checkResult UP-TO-DATE

How can I get result from my task "returnString()". This is my build.gradle:

import org.gradle.tooling.*

apply plugin: 'groovy'

dependencies {  
    compile 'org.codehaus.groovy:groovy-all:2.3.3'
}

task returnString() {
    return "STRING!"
}

task checkResult() <<{
    GradleConnector connector = GradleConnector.newConnector();       
    connector.forProjectDirectory(new File(".")); 
    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks("returnString");
        // Run the build
        ResultLoader resultHandler =  new ResultLoader()
        launcher.run(resultHandler);            
    } finally {
        // Clean up
        connection.close();
    } 
}

class ResultLoader implements ResultHandler<Object>{

    void onComplete(Object result){
        println "Result received: "+ result.toString()
    }
    void onFailure(GradleConnectionException failure){
        println failure
    }
}

Upvotes: 1

Views: 466

Answers (1)

Radim
Radim

Reputation: 4808

Your operation was completed successfully and onComplete() callback was notified. I am not sure what you expect to receive. There will be a result value if you run one of the getModel() methods or if you ask to execute BuildAction. LongRunningOperation is used to run all these three operations: build, model retrieval, actions (http://www.gradle.org/docs/current/javadoc/org/gradle/tooling/LongRunningOperation.html).

BTW: It is not clear to me why you are starting another build from already running one but it is unrelated to your question.

Upvotes: 1

Related Questions