beryx / badass-runtime-plugin

Create a custom runtime image of your non-modular application
https://badass-runtime-plugin.beryx.org
Apache License 2.0
161 stars 21 forks source link

Specify Different Java Toolchain Only for Runtime task #85

Open DJViking opened 3 years ago

DJViking commented 3 years ago

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
    }
}
siordache commented 3 years ago

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.