Skip to content

Use java toolchains to setup JDK 8 path #155

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

Merged
merged 2 commits into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
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.
10 changes: 1 addition & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,7 @@ 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))
val mainJavaToolchainVersion by ext(project.property("java.mainToolchainVersion"))

allprojects {
repositories {
Expand Down
47 changes: 10 additions & 37 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ base {
archivesBaseName = "kotlinx-datetime" // doesn't work
}

//val JDK_6: String by project
val JDK_8: String by project
val mainJavaToolchainVersion: String by project
val serializationVersion: String by project

java {
toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) }
with(javaToolchains.launcherFor(toolchain).get().metadata) {
logger.info("Using JDK $languageVersion toolchain installed in $installationPath")
}
}

kotlin {
explicitApi()

Expand Down Expand Up @@ -51,29 +57,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 { }
Expand Down Expand Up @@ -168,26 +156,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")
Expand Down Expand Up @@ -230,7 +204,6 @@ kotlin {
tasks {
named("jvmTest", Test::class) {
// maxHeapSize = "1024m"
// executable = "$JDK_6/bin/java"
}
}

Expand Down
3 changes: 3 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,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
Expand Down
13 changes: 5 additions & 8 deletions serialization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ plugins {
kotlin("plugin.serialization")
}

val JDK_8: String by project
val mainJavaToolchainVersion: String by project
val serializationVersion: String by project

java {
toolchain { languageVersion.set(JavaLanguageVersion.of(mainJavaToolchainVersion)) }
}

kotlin {
infra {
target("linuxX64")
Expand All @@ -32,13 +36,6 @@ kotlin {
attributes {
attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
}
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
jdkHome = JDK_8
}
}

}

js {
Expand Down