From 3963223d2a9a5fd98a91c3965135215823a6ae55 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 29 Oct 2021 20:01:50 +0300 Subject: [PATCH 1/2] Setup JDK 8 provisioning with java toolchains Remove commented out JVM 6 target. --- README.md | 8 ++++--- build.gradle.kts | 9 ------- core/build.gradle.kts | 44 ++++++---------------------------- gradle.properties | 1 + serialization/build.gradle.kts | 12 ++++------ 5 files changed, 17 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index c422c6642..b0434b0cf 100644 --- a/README.md +++ b/README.md @@ -362,8 +362,10 @@ git submodule init git submodule update ``` -The path to JDK 8 must be specified either with the environment variable `JDK_8` or -with the gradle property `JDK_8`. For local builds, you can use a later version of JDK if you don't have that -version installed. +The project requires JDK 8 to build classes and to run tests. +Gradle will try to find it among the installed JDKs or [provision](https://docs.gradle.org/current/userguide/toolchains.html#sec:provisioning) it automatically if it couldn't be found. +The path to JDK 8 can be additionally specified with the environment variable `JDK_8`. + After that, the project can be opened in IDEA and built with Gradle. diff --git a/build.gradle.kts b/build.gradle.kts index 9719093cd..98490ef14 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,15 +14,6 @@ infra { } } -fun jdkPath(version: Int): String { - fun envOrProperty(name: String): String? = System.getenv(name) ?: findProperty(name) as String? - - return envOrProperty("JDK_$version") ?: - version.takeIf { it < 9 }?.let { envOrProperty("JDK_1$version") } ?: - error("Specify path to JDK $version in JDK_$version environment variable or Gradle property") -} -//val JDK_6 by ext(jdkPath(6)) -val JDK_8 by ext(jdkPath(8)) allprojects { repositories { diff --git a/core/build.gradle.kts b/core/build.gradle.kts index c37686c49..8aeaab480 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -17,10 +17,13 @@ base { archivesBaseName = "kotlinx-datetime" // doesn't work } -//val JDK_6: String by project -val JDK_8: String by project val serializationVersion: String by project +java { + toolchain { languageVersion.set(JavaLanguageVersion.of(8)) } +} +logger.info("Using JDK 8 toolchain installed in ${javaToolchains.launcherFor(java.toolchain).get().metadata.installationPath}") + kotlin { explicitApi() @@ -51,29 +54,11 @@ kotlin { attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8) } compilations.all { - kotlinOptions { - jvmTarget = "1.8" - jdkHome = JDK_8 - } + // Set compilation options for JVM target here } } - /* - jvm("jvm6") { - this.withJava() - attributes { - attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 6) - } - compilations.all { - kotlinOptions { - jvmTarget = "1.6" - jdkHome = JDK_6 - } - } - } - */ - js { nodejs { // testTask { } @@ -168,26 +153,12 @@ kotlin { } } - /* - val jvm6Main by getting { - dependencies { - api("org.jetbrains.kotlin:kotlin-stdlib") - api("org.threeten:threetenbp:1.4.0") - - } - } - val jvm6Test by getting { - dependencies { - api("org.jetbrains.kotlin:kotlin-test-junit") - } - } - */ - val jvmMain by getting { dependencies { api("org.jetbrains.kotlin:kotlin-stdlib") } } + val jvmTest by getting { dependencies { api("org.jetbrains.kotlin:kotlin-test-junit") @@ -230,7 +201,6 @@ kotlin { tasks { named("jvmTest", Test::class) { // maxHeapSize = "1024m" -// executable = "$JDK_6/bin/java" } } diff --git a/gradle.properties b/gradle.properties index 9e60ab776..948853f4d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,5 @@ org.gradle.jvmargs=-Xmx1G -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 +org.gradle.java.installations.fromEnv=JDK_8 group=org.jetbrains.kotlinx version=0.3.1 diff --git a/serialization/build.gradle.kts b/serialization/build.gradle.kts index 5d4493a32..1855625d3 100644 --- a/serialization/build.gradle.kts +++ b/serialization/build.gradle.kts @@ -5,9 +5,12 @@ plugins { kotlin("plugin.serialization") } -val JDK_8: String by project val serializationVersion: String by project +java { + toolchain { languageVersion.set(JavaLanguageVersion.of(8)) } +} + kotlin { infra { target("linuxX64") @@ -32,13 +35,6 @@ kotlin { attributes { attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8) } - compilations.all { - kotlinOptions { - jvmTarget = "1.8" - jdkHome = JDK_8 - } - } - } js { From acfd891e03a1dbc31729278fddf877c37d052635 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 10 Nov 2021 04:35:26 +0300 Subject: [PATCH 2/2] Allow overriding Java toolchain version with a gradle property --- README.md | 4 ++-- build.gradle.kts | 1 + core/build.gradle.kts | 7 +++++-- gradle.properties | 2 ++ serialization/build.gradle.kts | 3 ++- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b0434b0cf..358feb443 100644 --- a/README.md +++ b/README.md @@ -365,7 +365,7 @@ git submodule update The project requires JDK 8 to build classes and to run tests. Gradle will try to find it among the installed JDKs or [provision](https://docs.gradle.org/current/userguide/toolchains.html#sec:provisioning) it automatically if it couldn't be found. The path to JDK 8 can be additionally specified with the environment variable `JDK_8`. - +For local builds, you can use a later version of JDK if you don't have that +version installed. Specify the version of this JDK with the `java.mainToolchainVersion` Gradle property. After that, the project can be opened in IDEA and built with Gradle. diff --git a/build.gradle.kts b/build.gradle.kts index 98490ef14..ea7ce1d9b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,6 +14,7 @@ infra { } } +val mainJavaToolchainVersion by ext(project.property("java.mainToolchainVersion")) allprojects { repositories { diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 8aeaab480..ed80b5c8d 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -17,12 +17,15 @@ base { archivesBaseName = "kotlinx-datetime" // doesn't work } +val mainJavaToolchainVersion: String by project val serializationVersion: String by project java { - toolchain { languageVersion.set(JavaLanguageVersion.of(8)) } + toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) } + with(javaToolchains.launcherFor(toolchain).get().metadata) { + logger.info("Using JDK $languageVersion toolchain installed in $installationPath") + } } -logger.info("Using JDK 8 toolchain installed in ${javaToolchains.launcherFor(java.toolchain).get().metadata.installationPath}") kotlin { explicitApi() diff --git a/gradle.properties b/gradle.properties index 948853f4d..f0fe4b521 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,6 +8,8 @@ versionSuffix=SNAPSHOT kotlinVersion=1.5.30 serializationVersion=1.3.0 +java.mainToolchainVersion=8 + kotlin.mpp.enableGranularSourceSetsMetadata=true kotlin.mpp.enableCompatibilityMetadataVariant=true kotlin.js.compiler=both diff --git a/serialization/build.gradle.kts b/serialization/build.gradle.kts index 1855625d3..89207ece8 100644 --- a/serialization/build.gradle.kts +++ b/serialization/build.gradle.kts @@ -5,10 +5,11 @@ plugins { kotlin("plugin.serialization") } +val mainJavaToolchainVersion: String by project val serializationVersion: String by project java { - toolchain { languageVersion.set(JavaLanguageVersion.of(8)) } + toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) } } kotlin {