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

[Gradle & Actions] Use Java 11 for compiling and checks instead of Java 8 #10

Open
coderbot16 opened this issue Dec 18, 2020 · 1 comment

Comments

@coderbot16
Copy link
Member

We can use the compilerArgs option to still make sure that our code is compatible with Java 8 even though we're compiling on Java 11:

compileJava {
    options.compilerArgs.addAll(['--release', '8')]
}

Unfortunately, that fails on Java 8 because the flag was only added in Java 9. However, this blog post has a good solution:

// If on JDK 9+, verify project cross-compiles on its 'sourceCompatible' JVM version (see https://github.com/melix/mrjar-gradle/blob/master/jdks.gradle)
if (project.hasProperty('crossCompile')) {
    if (JavaVersion.current().java9Compatible) {
        project.afterEvaluate {
            tasks.withType(JavaCompile) {
                def version = compat(sourceCompatibility)
                project.logger.info("Configuring $name to use --release $version")
                options.compilerArgs.addAll(['--release', version])
            }
        }
    } else {
        project.logger.warn("-PcrossCompile not supported prior to JDK 9; using JDK ${JavaVersion.current()}")
    }
}

// This function converts 1.8 -> 8
static String compat(String src) {
    if (src.contains('.')) {
        src.substring(src.lastIndexOf('.')+1)
    } else {
        src
    }
}

While that code supports cross compiling to any version, in reality we only care about cross compiling to Java 8 and/or compiling to Java 8 on Java 8 at the moment so we can just hardcode that part in and remove the warning when cross compiling isn't supported.

@ramidzkh
Copy link
Member

FabricMC/fabric@c2f49c1 is much smaller

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

2 participants