1

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
    }
}
Grzegorz P
  • 43
  • 2

1 Answers1

1

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.

Radim
  • 4,721
  • 1
  • 22
  • 25
  • I expect to have in method onComplete(..) a result from executed task returnString(), which is a "STRING!". So the output should be "Result received: STRING!". This example was simplified to expose ResultHandler problem. I want to use ResultHandler in communication between different tasks in different locations (across buildfiles and projects) – Grzegorz P Sep 12 '14 at 12:19
  • 1. Your `return "STRING"` is executed during configuration phase and not as a part of task action! 2. Action executed by tasks do not have return values. http://www.gradle.org/docs/current/javadoc/org/gradle/api/Task.html - they are added with `doFirst()` or `doLast()` or you can use `task name << { ... }` syntax. 3. Starting another build sounds highly ineffective. I'd recommend you to take a closer look at how tasks work - http://www.gradle.org/docs/current/userguide/more_about_tasks.html The part about task inputs and outputs can give you some ideas. – Radim Sep 12 '14 at 13:00