Druudik
Druudik

Reputation: 1015

How to build 3 separate gradle project using composite build

I have 3 projects - A, B and C. They share the same core module shared_core. Every one of that project can be build using its gradlew build command. But I would like that shared_core will be built only once not thrice, since it is waste of time. I was told that I can use multi project build or composite build for that.

Right now I am trying to create composite build of those 3 but I am struggling.

Can someone show me the example of how my build.gradle and settings.gradle should look like for this task ?

I am really new to gradle so thank you for any help.

Upvotes: 1

Views: 1035

Answers (1)

0xadecimal
0xadecimal

Reputation: 726

The gradle guide to creating multi project builds is an excellent starting point in understanding this topic - I suggest you work through in. In the short-term here's an answer to your question based on your feedback in the comments:

Assuming your project structure is something like

testproject/A
testproject/B
testproject/C
testproject/shared_modules

you need to:

  1. Add testproject/settings.gradle with the following entry: include 'A', 'B', 'C', 'shared_core'

  2. The build.gradle files in A, B, C should contain the following dependency definition:

    dependency compile project(':shared_core')

As a concrete example, here is the configuration for a simple dummy gradle multimodule project that has the structure outlined above:

testproject/settings.gradle:

rootProject.name = 'testproject'
include 'A', 'B', 'C', 'shared_core'

testproject/build.gradle

plugins {
    id 'java'
}

group 'uk.co.so.answers'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

testproject/{A,B,C}/build.gradle

plugins {
    id 'java'
}

group 'uk.co.so.answers'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile project(':shared_core')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

testproject/shared_core/build.gradle

plugins {
    id 'java'
}

group 'uk.co.so.answers'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Now, to build all projects, run from the project root:

   ./gradlew clean build

Upvotes: 2

Related Questions