Multi-Project Cross platform gradle build

I am new to Gradle and facing some problem to build my multi-project cross platform project. Project structure is as below:

Root projects A
Build.gradle
Setting.gradle

  • SubProject (Java)
    Build.gradle
    Setting.gradle
    SubProject J1
    SubProject J2

  • SubProject Native (C/C++)
    Build.gradle
    Setting.gradle
    SubProject C1
    SubProject C2

I can build both SubProjects (C & java) individually from respective subproject’s build file. Now I want to build java and native subprojects separately (as individual tasks) from the root folder, how can I do that? What will be the content of setting.gradle file of project A?

Afaik, you are supposed to have one settings.gradle file per multi-project build. In your case - at top level (Project A)

If you need to keep multiple settings.gradle and be able to build both projects separately, please take a look at composite builds

Thank you for your response. But in section 10.5 it is mention that composite builds are not supported for Native Builds. I am trying to create two separate tasks for java and native in root projects with corresponding subprojects. I am not sure whether this is right approach or not.

If your structure for your entire multi-project build looks like this:

- Root Project: A
   - build.gradle
   - settings.gradle
   - SubProject: Java 
      - build.gradle
      - SubProject: J1
      - SubProject: J2
   - SubProject: Native
      - build.gradle
      - SubProject: C1
      - SubProject: C2

It is correct that you would only have one settings.gradle for a single multi-project build (no composite). It should like this for the above structure:

rootProject.name = 'A'
include 'Java'
include 'Java:J1'
include 'Java:J2'
include 'Native'
include 'Native:C1'
include 'Native:C2'

If you are in the root build, gradle build would build everything. If you’re wanting to build just one subproject tree, use the path when specifying the task. For the Java subprojects, use gradle :Java:build. For the Native project, use gradle :Native:build. The expectation is that the Java project build task is wired up to build everything in the subprojects already. If you want to create a root project task like buildJavaProjects or buildNativeProjects, you can wire up a dependency in the A root project:

task buildJavaSubProjects(dependsOn ':Java:build') {
    group = 'Build'
    description = 'Builds all Java projects.'
}

Alternatively, if you want the behavior of calling only J1 and J2 project build tasks (without wiring up anything special in Java ), you can use:

task buildJavaSubProjects {
    group = 'Build'
    description = 'Builds all Java projects.'
    project(':Java').subprojects { dependsOn "${it.path}:build" }
}

Replace project(':Java').subprojects with project(':Java').allprojects if you want build to run on Java, J1, and J2 without doing anything special in Java (same as running gradle build from a project normally).

The same procedure would apply on the Native side.

Thank you some much…