How to collapse source sets inside a java library

I’m looking for a way to collapse source sets inside a java-library subproject, and have the distribution zip contain jars only, not individual classes.

I am attaching a gist illustrating my problem. Referring to it-

I am showing 2 versions of api/build.gradle, one in which :api is a java-library subproject (which is what I want) and one where it’s only a java subproject.

As it is, the java-library version works, but it creates archives as follows:

  • api.jar contains AnInterface.class only, not AnEnum.class.
  • dist.zip contains api.jar and AnEnum.class.

Is there a way to get the individual .class files from dist.zip to be packed in an inner Jar? E.g. get Enum.class packed inside api.jar?

I know I can obtain inner jars only by having :api be java subproject (see other api/build.gradle), the difference boiling down to this: https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_classes_usage

However, I would prefer having :api be a java-library because of the finer-grained dependency management it offers.

What seems to have done the trick is:

plugins {
    "java-library"
}

sourceSets {
    base {
    }
    main {
        output.dir(sourceSets.base.output)   // for consumers runtime
    }
}

dependencies {
    compileOnly sourceSets.base.output   // for main compiletime
    apiElements sourceSets.base.output   // for consumers comiletime
}