custom-java-home.gradle 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // -----------------------------------------------------------------------------
  2. //
  3. // This script adds support for the following two JVM system properties
  4. // that control the build for alternative JDKs (i.e., a JDK other than
  5. // the one used to launch the Gradle process).
  6. //
  7. // - customJavaHome: absolute path to the alternate JDK installation to
  8. // use to compile Java code and execute tests. This system property
  9. // is also used in spring-oxm.gradle to determine whether JiBX is
  10. // supported.
  11. //
  12. // - customJavaSourceVersion: Java version supplied to the `--release`
  13. // command line flag to control the Java source and target
  14. // compatibility version. Supported versions include 9 or higher.
  15. // Do not set this system property if Java 8 should be used.
  16. //
  17. // Examples:
  18. //
  19. // ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test
  20. //
  21. // ./gradlew --no-build-cache -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home test
  22. //
  23. // ./gradlew -DcustomJavaHome=/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home -DcustomJavaSourceVersion=14 test
  24. //
  25. //
  26. // Credits: inspired by work from Marc Philipp and Stephane Nicoll
  27. //
  28. // -----------------------------------------------------------------------------
  29. import org.gradle.internal.os.OperatingSystem
  30. // import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile
  31. def customJavaHome = System.getProperty("customJavaHome")
  32. if (customJavaHome) {
  33. def customJavaHomeDir = new File(customJavaHome)
  34. def customJavaSourceVersion = System.getProperty("customJavaSourceVersion")
  35. tasks.withType(JavaCompile) {
  36. logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir)
  37. options.forkOptions.javaHome = customJavaHomeDir
  38. inputs.property("customJavaHome", customJavaHome)
  39. if (customJavaSourceVersion) {
  40. options.compilerArgs += [ "--release", customJavaSourceVersion]
  41. inputs.property("customJavaSourceVersion", customJavaSourceVersion)
  42. }
  43. }
  44. tasks.withType(GroovyCompile) {
  45. logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHomeDir)
  46. options.forkOptions.javaHome = customJavaHomeDir
  47. inputs.property("customJavaHome", customJavaHome)
  48. if (customJavaSourceVersion) {
  49. options.compilerArgs += [ "--release", customJavaSourceVersion]
  50. inputs.property("customJavaSourceVersion", customJavaSourceVersion)
  51. }
  52. }
  53. /*
  54. tasks.withType(KotlinJvmCompile) {
  55. logger.info("Java home for " + it.name + " task in " + project.name + ": " + customJavaHome)
  56. kotlinOptions.jdkHome = customJavaHomeDir
  57. inputs.property("customJavaHome", customJavaHome)
  58. }
  59. */
  60. tasks.withType(Test) {
  61. def javaExecutable = customJavaHome + "/bin/java"
  62. if (OperatingSystem.current().isWindows()) {
  63. javaExecutable += ".exe"
  64. }
  65. logger.info("Java executable for " + it.name + " task in " + project.name + ": " + javaExecutable)
  66. executable = javaExecutable
  67. inputs.property("customJavaHome", customJavaHome)
  68. if (customJavaSourceVersion) {
  69. inputs.property("customJavaSourceVersion", customJavaSourceVersion)
  70. }
  71. }
  72. }