Why am I getting this "No matching variant" failure message?

I am trying to set up two variants of my producer library - one for Java 8 and another for Java 11 but the consumer always chooses the ones gradle ships by default (runtimeElements or apiElements) even though they have org.gradle.jvm.version set to 8 and consumer project is in java 11 and my feature variant has this attribute set to 11 as well. Therefore I tried to force the consumer to consume my feature variant by setting the org.gradle.jvm.version on both runtimeElements and apiElements to 15 (so that my Java 11 consumer project for sure chooses my feature java 11 producer variant. Yet it doesn’t and upon executing dependencyInsight task, the following message appears:

> Task :dependencyInsight
org.example:producer:1.0-SNAPSHOT FAILED
   Failures:
      - Could not resolve org.example:producer:1.0-SNAPSHOT.
          - No matching variant of org.example:producer:1.0-SNAPSHOT was found. The consumer was configured to find an API of a library compatible with Java 11, preferably in the form of class files, and its dependencies declared externally but:
              - Variant 'apiElements' capability org.example:producer:1.0-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 15 and the consumer needed a component compatible with Java 11
              - Variant 'java11ConfigApiElements' capability org.example:producer-java11-config:1.0-SNAPSHOT declares an API of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally
              - Variant 'java11ConfigRuntimeElements' capability org.example:producer-java11-config:1.0-SNAPSHOT declares a runtime of a library compatible with Java 11, packaged as a jar, and its dependencies declared externally
              - Variant 'runtimeElements' capability org.example:producer:1.0-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 15 and the consumer needed a component compatible with Java 11

org.example:producer:1.0-SNAPSHOT FAILED
\--- compileClasspath

Producer’s gradle.build

import org.gradle.api.attributes.java.TargetJvmVersion

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

group 'org.example'
version '1.0-SNAPSHOT'

java {
    registerFeature('java11Config') {
        usingSourceSet(sourceSets.main)
    }
}

configurations {
    java11ConfigRuntimeElements {
        attributes {
            attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11)
        }
    }

    java11ConfigApiElements {
        attributes {
            attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 11)
        }
    }

    apiElements {
        attributes {
            attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 15)
        }
    }

    runtimeElements {
        attributes {
            attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 15)
        }
    }
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    java11ConfigRuntimeElements('com.google.code.gson:gson:2.8.6')
    runtimeElements('org.slf4j:slf4j-api:1.7.30')
}

test {
    useJUnitPlatform()
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
}

What could be the cause of this message?

I tried to address this in the other related topic here.

1 Like