kmongo icon indicating copy to clipboard operation
kmongo copied to clipboard

From version 4.7.0 Java 8 compatibility is broken

Open gigitalz opened this issue 1 year ago • 6 comments

I reveive this error:

* What went wrong:
Execution failed for task ':backend:compileKotlin'.
> Could not resolve all files for configuration ':backend:compileClasspath'.
   > Could not resolve com.github.jershell:kbson:0.4.5.
     Required by:
         project :backend > org.litote.kmongo:kmongo-coroutine-serialization:4.7.0 > org.litote.kmongo:kmongo-serialization-mapping:4.7.0
      > No matching variant of com.github.jershell:kbson:0.4.5 was found. The consumer was configured to find an API of a library compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'apiElements' capability com.github.jershell:kbson:0.4.5 declares an API of a library, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
          - Variant 'runtimeElements' capability com.github.jershell:kbson:0.4.5 declares a runtime of a library, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8

Please also check #391 same issue (and yes it keeps happening). I am running on java version "1.8.0_301". I'd like to update but I can't.

gigitalz avatar May 13 '23 12:05 gigitalz

In reproduce the bug.

This is a kbson bug (see https://github.com/jershell/kbson/issues/25)

Workaround:

In your build.gradle.kts:

@CacheableRule
abstract class TargetJvmVersionRule @Inject constructor(val jvmVersion: Int) : ComponentMetadataRule {
    @get:Inject abstract val objects: ObjectFactory

    override fun execute(context: ComponentMetadataContext) {
        context.details.withVariant("apiElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
                attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_API))
            }
        }
    }
}


dependencies {
    components {
        withModule<TargetJvmVersionRule>("com.github.jershell:kbson") {
            params(8)
        }
    }
  ...
}

(source: https://docs.gradle.org/current/userguide/component_metadata_rules.html#basics_of_writing_a_component_metadata_rule)

HTH

zigzago avatar May 19 '23 06:05 zigzago

I still get an error, but somewhat different?

* What went wrong:
Execution failed for task ':backend:run'.
> Could not resolve all files for configuration ':backend:runtimeClasspath'.
   > Could not resolve com.github.jershell:kbson:0.5.0.
     Required by:
         project :backend > org.litote.kmongo:kmongo-coroutine-serialization:4.9.0 > org.litote.kmongo:kmongo-serialization-mapping:4.9.0
      > No matching variant of com.github.jershell:kbson:0.5.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'apiElements' capability com.github.jershell:kbson:0.5.0 declares a library compatible with Java 8, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares an API of a component and the consumer needed a runtime of a component
          - Variant 'runtimeElements' capability com.github.jershell:kbson:0.5.0 declares a runtime of a library, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8
@CacheableRule
abstract class TargetJvmVersionRule implements ComponentMetadataRule {
    @Inject abstract ObjectFactory getObjects()

    final Integer jvmVersion
    @Inject TargetJvmVersionRule(Integer jvmVersion) {
    this.jvmVersion = jvmVersion
    }

    void execute(ComponentMetadataContext context) {
        context.details.withVariant("apiElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
                attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_API))
            }
        }
    }
}

dependencies {
    components {
        withModule("com.github.jershell:kbson", TargetJvmVersionRule) {
            params(8)
        }
    }

gigitalz avatar May 26 '23 07:05 gigitalz

I tried issuing bot apiElements and runtimeElements, still no luck

* What went wrong:
Execution failed for task ':backend:compileKotlin'.
> Could not resolve all files for configuration ':backend:compileClasspath'.
   > Could not resolve com.github.jershell:kbson:0.5.0.
     Required by:
         project :backend > org.litote.kmongo:kmongo-coroutine-serialization:4.9.0 > org.litote.kmongo:kmongo-serialization-mapping:4.9.0
      > The consumer was configured to find an API of a library compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm'. However we cannot choose between the following variants of com.github.jershell:kbson:0.5.0:
          - apiElements
          - runtimeElements
        All of them match the consumer attributes:
          - Variant 'apiElements' capability com.github.jershell:kbson:0.5.0 declares an API of a library compatible with Java 8, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Unmatched attribute:
                  - Provides release status but the consumer didn't ask for it
          - Variant 'runtimeElements' capability com.github.jershell:kbson:0.5.0 declares an API of a library compatible with Java 8, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Unmatched attribute:
                  - Provides release status but the consumer didn't ask for it
                  
                  

code:

@CacheableRule
abstract class TargetJvmVersionRule implements ComponentMetadataRule
    {
    @Inject
    abstract ObjectFactory getObjects()

    final int jvmVersion
    @Inject
    TargetJvmVersionRule(int jvmVersion)
    {
    this.jvmVersion = jvmVersion
    }

    void execute(ComponentMetadataContext context)
        {
        context.details.withVariant("apiElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
                attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_API))
            }
        }

        context.details.withVariant("runtimeElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
                attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_API))
            }
        }
    }
}

dependencies {
    components {
        withModule("com.github.jershell:kbson", TargetJvmVersionRule) {
            params(8)
        }
    }

gigitalz avatar May 26 '23 08:05 gigitalz

Looks like it's complaining about an attribute it didn't ask for so I removed it and it went through, code:

@CacheableRule
abstract class TargetJvmVersionRule implements ComponentMetadataRule
    {
    @Inject
    abstract ObjectFactory getObjects()

    final int jvmVersion
    @Inject
    TargetJvmVersionRule(int jvmVersion)
    {
    this.jvmVersion = jvmVersion
    }

    void execute(ComponentMetadataContext context)
        {
        context.details.withVariant("apiElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
            }
        }

        context.details.withVariant("runtimeElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
            }
        }
    }
}

gigitalz avatar May 26 '23 08:05 gigitalz

What is your gradle version ? Tested on my side with gradle 8.1.1

zigzago avatar May 26 '23 20:05 zigzago

This fixed my issue. Running with gradle 8.2.1

Looks like it's complaining about an attribute it didn't ask for so I removed it and it went through, code:

@CacheableRule
abstract class TargetJvmVersionRule implements ComponentMetadataRule
    {
    @Inject
    abstract ObjectFactory getObjects()

    final int jvmVersion
    @Inject
    TargetJvmVersionRule(int jvmVersion)
    {
    this.jvmVersion = jvmVersion
    }

    void execute(ComponentMetadataContext context)
        {
        context.details.withVariant("apiElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
            }
        }

        context.details.withVariant("runtimeElements") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, jvmVersion)
            }
        }
    }
}

sarhatabaot avatar Aug 30 '23 11:08 sarhatabaot