Anton3
Anton3

Reputation: 586

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.

Upvotes: 7

Views: 1841

Answers (2)

Anton3
Anton3

Reputation: 586

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

Upvotes: 2

hotkey
hotkey

Reputation: 147991

Yes it's possible and close to what you supposed it to look like.

Add a separate subproject, say, :pancakes-simple and configure its publishing as you did for your normal modules.

Then just add the dependencies that you want it to expose. If you are not using the java-library plugin, use the compile configuration:

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

Unlike implementation dependencies, these will be available on the consumer's compile classpath if they add a dependency on the pancakes-simple module.

With the java-library plugin, api dependencies should also work

Upvotes: 1

Related Questions