Gradle does not detect compatible JDKs of multi-release jar

1.4k views Asked by At

I created a multi-release jar library using Gradle 6.7 being on JDK 14 which is geared to be compatible with JDK 11 and 14. Therefore I use the following build.gradle which puts all classes in a common folder and JDK 11 specific class versions into another. It seems to work fine since the generated jar contains all classes at their desired positions (either the main folder or the META-INF/versions/11 folder) as well as the required manifest entry for enabling multi release jars.

plugins {
    id 'java-library'
    id 'maven-publish'
    id 'signing'
}

group 'bayern.steinbrecher'
version '1.1.6-SNAPSHOT'

repositories {
    mavenCentral()
}

sourceSets {
    java11 {
        java {
            srcDirs = ['src/main/java11']
        }
    }
}

dependencies {
    java11Implementation files(sourceSets.main.output.classesDirs){
        builtBy compileJava
    }
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

compileJava {
    sourceCompatibility = 14
    targetCompatibility = 14
}

compileJava11Java {
    sourceCompatibility = 11
    targetCompatibility = 11
}

jar {
    into('META-INF/versions/11'){
        from sourceSets.java11.output
    }
    manifest.attributes(
            'Multi-Release': 'true'
    )
}

java {
    modularity.inferModulePath = true
    withSourcesJar()
    withJavadocJar()
}

javadoc {
    if(JavaVersion.current().isJava9Compatible()) {
        options.addBooleanOption('html5', true)
    }
}

publishing {
    <publishing-configuration>
}

signing {
    useGpgCmd()
    sign publishing.publications.library
}

If I depend on this library in a project the build fails as soon as I set source compatibility to JDK 11 instead of JDK 14 yielding the following message:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Could not resolve bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT.
     Required by:
         project :
      > No matching variant of bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT:20201204.075748-1 was found. The consumer was configured to find an API of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally but:
          - Variant 'apiElements' capability bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT declares an API of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 14 and the consumer needed a component compatible with Java 11
          - Variant 'javadocElements' capability bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)
          - Variant 'runtimeElements' capability bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 14 and the consumer needed a component compatible with Java 11
          - Variant 'sourcesElements' capability bayern.steinbrecher:jzlib:1.1.6-SNAPSHOT declares a runtime of a component, and its dependencies declared externally:
              - Incompatible because this component declares documentation and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java version (required compatibility with Java 11)
                  - Doesn't say anything about its elements (required them packaged as a jar)

Since this message suggests that Gradle assumes that the library is not compatible for JDK 11 I wonder how to tell Gradle that the library works for JDK 11 as well.

0

There are 0 answers