Root project depends on subprojects build

How can I mention subproject should build before root project.

in settings.gradle = 'loginmodule'
include 'servicebundle'
include 'webbundle'
include 'webredirectbundle'

root project build.gradle will bundle all subprojects

task createESA(type: Zip, dependsOn: generateSubSystemFile) {
    subprojects.each { dependsOn("${}:build") }
    from subprojects.collect { "${it.buildDir}/libs" }
    from (subsystemFile) {
        into 'OSGI-INF'
    from ('resources/OSGI-INF') {
        into 'OSGI-INF'
    extension 'esa'

build.finalizedBy createESA

When I try this build dependson subprojects:build it is giving error like circular dependency.

I am using gradle clean build to build the project.

Is there any better way to do that ?? I just want to build all subprojects first before root project bundling them.

A better approach would be to use a configuration in the root project and add the subprojects as dependencies.
Something along the lines of:

configurations {
dependencies {
    subprojects.each {
        esa it
task createESA( type: Zip, ...) {
    from configurations.esa

The subproject tasks required to build the artifacts will implicitly become dependencies of the createESA task and will be executed first.

1 Like

Also, build.finalizedBy createESA is probably not what you need. finalizedBy creates a relationship whereby the finalizing task is always executed, even if the task being finalized fails.
If you just want your createESA task to be run as part of running build, then I suggest making it a dependency of the assemble lifecycle task.

assemble.dependsOn createESA