Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does mixedJavaRelease work in a library? #164

Closed
tobiasdiez opened this issue Jul 28, 2020 · 4 comments
Closed

How does mixedJavaRelease work in a library? #164

tobiasdiez opened this issue Jul 28, 2020 · 4 comments

Comments

@tobiasdiez
Copy link

I tried to add modularity.mixedJavaRelease 8 to my gradle.build file, but outgoingVariants still reports org.gradle.jvm.version = 14, and indeed the compiled version is not usable in JDK < 14. What I'm doing wrong?
See https://github.com/tobiasdiez/EasyBind/blob/45106f173f638a74320bf9a9df26d527a11f133a/build.gradle#L18-L23

Setting source/targetCompatibility to jdk 9 instead of using mixedJavaRelease works as expected (but of course doesn't provide jdk 8 support). Both cannot be combined since then I get the error "sourceCompatibility should not be set together with --release option".

@siordache
Copy link
Collaborator

I suppose that outgoingVariants is not able to detect the compile flags added or changed by the gradle-modules-plugin, so the reported versions are inaccurate. To check the versions of the classes in the generated JAR, I added the following task to build.gradle:

task printClassVersion(dependsOn: jar) {
    doLast {
        copy {
            from zipTree(tasks.jar.archiveFile.asFile)
            into "$buildDir/exploded"
        }
        file("$buildDir/exploded").eachFileRecurse(groovy.io.FileType.FILES) { f ->
            if(f.name.endsWith(".class")) {
                def dis = new DataInputStream(new FileInputStream(f))
                dis.skipBytes(6)
                def majorVersion = dis.readUnsignedShort()
                println "JavaVersion: ${majorVersion - 44} - $f"
            }
        }
    }
}

When using modularity.mixedJavaRelease 8, the above task reports JavaVersion: 8 for all classes in the JAR except module-info.class, which is 9. Therefore, I think that mixedJavaRelease works as expected.

However, you said that the compiled version is not usable in JDK < 14. Could this be because your library depends on JavaFX 14?

@tobiasdiez
Copy link
Author

Thanks for looking into this.

When the library is used with JDK 12, the following error message is displayed

- Incompatible attribute:
                  - Required org.gradle.jvm.version '12' and found incompatible value '14'.

So this looks like gradle really thinks the library was compiled for jdk 14.

@siordache
Copy link
Collaborator

Not sure if it helps, but put this in your library's build.gradle:

configurations.each { cfg ->
    cfg.attributes {
        attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
    }
}

@weisJ
Copy link

weisJ commented Jul 10, 2021

I too ran into this issue. This also confuses my IDE as it forces the language level to be set to the newest supported version of the compiler (not very helpful when trying to stay compatible with the chosen target).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants