Search code examples
gradlekotlinmulti-module

Pack a multi-module Gradle library into a single module


I've split my pancakes Gradle-based library written in Kotlin into multiple modules: pancakes-core, pancakes-addon1, ..., pancakes-addonN. The addon ones include the core one.

Now, most users shouldn't care and will just want the default configuration with all the dependencies included. But they will have to write boilerplate:

dependencies {
    implementation("pancakes:pancakes-core")
    implementation("pancakes:pancakes-addon1")
    ...
    implementation("pancakes:pancakes-addonN")
}

This is a no-go for me. I'll probably have to merge all the modules, although I've just spent some time to branch off some replaceable features into their own modules.

Unless! There is a way to write something like the following:

project(":pancakes-simple") {
    dependencies {
        autoForwardedApi(":pancakes-core")
        autoForwardedApi(":pancakes-addon1")
        ...
        autoForwardedApi(":pancakes-addonN")
    }
}

Unfortunately, api is not enough.


Solution

  • java-library Gradle plugin is required for api dependencies to be forwarded from the current module. So that's how to pack all the modules into a single one:

    1. Add java-library plugin (and remove java) in all modules of the library
    2. Create a module like my :pancakes-simple that api-depends on all the other modules