badass-runtime-plugin icon indicating copy to clipboard operation
badass-runtime-plugin copied to clipboard

Specify Different Java Toolchain Only for Runtime task

Open DJViking opened this issue 3 years ago • 1 comments

If you want to build with Java 11, and use jpackage, prior to Java Toolchain support in Gradle, you had to specify jpackageHome with a newer version of Java that has the jpackage tool. jpackageHome = '/usr/java/jdk-16'

With Toolchain support in gradle, you can specify a different Toolchain for a single Gradle task. https://docs.gradle.org/current/userguide/toolchains.html#specify_custom_toolchains_for_individual_tasks

This plugin has support now for Toolchain, but to specify a different than the main toolchain does not seem possible. Otherwise something like this would be possible:

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(11)
        vendor = JvmVendorSpec.ADOPTOPENJDK
    }
}

tasks.withType(RuntimeTask).configureEach {
    javaCompiler = javaToolchains.compilerFor {
        languageVersion = JavaLanguageVersion.of(16)
        vendor = JvmVendorSpec.ADOPTOPENJDK
    }
}

runtime {}

Though there is a workaround. If not perhaps the proper solution?

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(16)
        vendor = JvmVendorSpec.ADOPTOPENJDK
    }
}

def java16Home = javaToolchains.compilerFor {
    languageVersion = JavaLanguageVersion.of(16)
    vendor = JvmVendorSpec.ADOPTOPENJDK
}.get().metadata.installationPath.asFile.absolutePath

runtime {
    jpackage {
        jpackageHome = java16Home
    }
}

DJViking avatar May 11 '21 10:05 DJViking

I think that your workaround is the proper solution.

Using

tasks.withType(RuntimeTask).configureEach {
    javaCompiler = javaToolchains.compilerFor {
        languageVersion = JavaLanguageVersion.of(16)
        vendor = JvmVendorSpec.ADOPTOPENJDK
    }
}

doesn't work because javaCompiler is a property of JavaCompile and RuntimeTask doesn't extend JavaCompile.

siordache avatar May 19 '21 16:05 siordache