gradlecommand-line-interfacebuild-script

Gradle: How to run a task of a subproject from root directory, and how to add it as a final task to other root project task?


I've two main questions, the first being how do I run a task of a subproject from cli and from root project directory and not the task inside the root project?

Suppose I have one root project, let's call it NetworkCommunicator.
And I have one sub project inside it, called CommandNode.
Now, I am inside the root directory, and I want to call the build of the CommandNode project,
not the one of NetworkCommunicator project, so it only builds CommandNode project.
How can I do it? Isn't there any simpler way than cd /CommandNode && ./gradle build?

Now onto my second question, there is a task inside the CommandNode called buildAndPost,
I want to add a similar line to this: build.finalizedBy "buildAndPost(of the subproject)" that finalizes the build task of the root project with the buildAndPost task of the sub project, How can I do this?


Solution

    1. To run the task build of the subproject CommandNode from the main project directory you can execute:
    ./gradlew :CommandNode:build
    

    That was for a Unix-based operating system; on Windows you can write:

    gradlew.bat :CommandNode:build
    
    1. To make the build task in CommandNode be finalised by that subproject's buildAndPost task you can write the following in CommandNode's build.gradle or build.gradle.kts file (ie this syntax works in Groovy and Kotlin):
    tasks.named("build") {
        finalizedBy("buildAndPost")
    }
    

    buildAndPost here refers to the buildAndPost task of the same subproject; you would need supply the full task path (similarly to part 1) to refer to a buildAndPost task in another subproject.