Search code examples
gradlemulti-project

How to organize gradle subprojects?


I'm trying to organize my subprojects to specify common dependencies at build.gradle.kts(it is kotlin-based) in the root project. Let's say I have 5 subprojects, A, B, C, D, E, without root project. In this case, I wanna group these subprojects like below.

Group Foo -> A, B, D
Group Bar -> B, C
Group Baz -> C, D, E

As I'm not familiar with gradle, I couldn't find a way to achieve this. Even though I can configure all subprojects by specifying subproject's name one by one(e.g. subprojects.filter { listOf(":A", ":B", ":C").contains(it) }), but I have too many subprojects and also need too many groups to do. Is there any way to do this ? Thanks.


Solution

  • You can use the Version catalog published in gradle 7. It support you to build multiple bundles and use it in your project to inject group introduction dependency. The step is used to declare the versions of all direct dependencies used in your build in one central location, the file gradle/libs.versions.toml.

    [versions]
    groovy = "3.0.5"
    
    [libraries]
    groovy-core = { module = "org.codehaus.groovy:groovy", version.ref = "groovy" }
    groovy-json = { module = "org.codehaus.groovy:groovy-json", version.ref = "groovy" }
    groovy-nio = { module = "org.codehaus.groovy:groovy-nio", version.ref = "groovy" }
    commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version = { strictly = "[3.8,4.0[", prefer="3.9" } }
    
    [bundles]
    groovy = ["groovy-core", "groovy-json", "groovy-nio"]
    groovy1 = ["groovy-core", "groovy-json", "commons-lang3"]
    

    And use it in your build.gradle.

      dependencies {
            implementation 'org.springframework.boot:spring-boot-starter'
            compileOnly 'org.projectlombok:lombok'
            annotationProcessor 'org.projectlombok:lombok'
    // here is a use example
            implementation libs.bundles.groovy1
        }
    

    If you want more information of this, you can goto the gradle 7 to learn more. The websit is : https://gradle.org/whats-new/gradle-8/