vanniktech / gradle-maven-publish-plugin

A Gradle plugin that publishes your Android and Kotlin libraries, including sources and javadoc, to Maven Central or any other Nexus instance.
https://vanniktech.github.io/gradle-maven-publish-plugin
Apache License 2.0
1.3k stars 119 forks source link

Kotlin project targeting JVM 1.8 fails to build while working fine with JDK 11 #518

Closed 4ig closed 1 year ago

4ig commented 1 year ago

Thank you for implementing this plugin! I have an issue with the following use case. I am working on a Kotlin library that I want to function on Android as well. At the moment, it targets JVM 11 and it gets published without issues. But I need to target JVM 1.8 in order to work on Android. I have the following config now.

kotlin {
    jvmToolchain(11)
}

If I change 11 here to 8 I and run the build using JDK 8 installed I get the following error:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'sol4k'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.vanniktech:gradle-maven-publish-plugin:0.24.0.
     Required by:
         project : > com.vanniktech.maven.publish:com.vanniktech.maven.publish.gradle.plugin:0.24.0
      > No matching variant of com.vanniktech:gradle-maven-publish-plugin:0.24.0 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.4.2' but:
          - Variant 'apiElements' capability com.vanniktech:gradle-maven-publish-plugin:0.24.0 declares a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2')
          - Variant 'runtimeElements' capability com.vanniktech:gradle-maven-publish-plugin:0.24.0 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 11 and the consumer needed a component compatible with Java 8
              - Other compatible attribute:
                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.4.2')

Here is my build.gradle configuration.

Do you know how I can overcome this issue?

I can also set the toolchain version to 11 and the target version to 1.8 separately and run the build using JDK 11, like this:

compileKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

kotlin {
    jvmToolchain(11)
}

This way I would get the warning:

> Task :compileKotlin
'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
By default will become an error since Gradle 8.0+! Read more: https://kotl.in/gradle/jvm/target-validation
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

I am not sure if it would produce bytecode compatible with Java 8.

gabrielittner commented 1 year ago

What is the output of ./gradlew -v? The error message looks like you are using jdk 8 to run Gradle. Setting the toolchain to 11 will only use JDK 11 for certain tasks but not for Gradle itself. So the solution would be to use JDK 11 or newer to run Gradle (this can usually be configured with the JAVA_HOME environment variable) and then setting target and jvmToolchain to 8.

4ig commented 1 year ago

Thank you for your response!

The error message looks like you are using jdk 8 to run Gradle

That's correct. I tried using Java 8 as a default as well as updating jvmToolchain to 8. Executing ./gradlew -v would give me JVM: 1.8.0_362 (Temurin 25.362-b09). I set kotlinOptions.jvmTarget = '1.8' and jvmToolchain(8). All of these used together would give me the error from the first comment.

So the solution would be to use JDK 11 or newer to run Gradle (this can usually be configured with the JAVA_HOME environment variable) and then setting target and jvmToolchain to 8.

This worked perfectly. Thank you. I am wondering why I didn't try it earlier. Just to confirm, this will produce proper JVM 8 bytecode, right?

gabrielittner commented 1 year ago

Just to confirm, this will produce proper JVM 8 bytecode, right?

The jvmToolchain(8) will make it so that the JavaCompile and KotlinCompile tasks run with jdk 8 even though Gradle itself runs in a different jdk.