gradlegroovybuildbuild.gradlegradlew

Gradle - How to get task output from subproject


I'm new to Gradle. I have a sample project structure like this:

rootA/
├── build.gradle
├── settings.gradle
└── subA
    └── build.gradle

In subA project, there is a task, let call, goInsideSubA() and this task can return a string (something like) "this is inside subA project"

Here is my sample context:

//subA/build.gradle:

task goInsideSubA() {
    def string = "this is inside subA project"
}
// rootA/build.gradle

task showInsideSubA() {
    doLast {
        // how can I get the string from :subA:goInsideSubA task here
    }
}
// settings.gradle

include ':subA'

My question is how can I get that string "this is inside subA project" for the task showInsideSubA() in rootA project to reuse?


Solution

  • I found a solution, by using "task properties":

    //subA/build.gradle:
    
    task goInsideSubA() {
        ext {
            myString = "this is inside subA project"
        }
    }
    
    
    // rootA/build.gradle
    
    task showInsideSubA() {
        doLast {
            println tasks.getByPath(':subA:goInsideSubA').myString
        }
    }
    
    // settings.gradle
    
    include ':subA'
    

    result:

    $ gradle -q showInsideSubA
    > this is inside subA project