Skip to content

Commit 19560a2

Browse files
committed
build: set kotlin compiler targets for user compatibility
This re-introduces compatibility for consuming users dependant on earlier versions of the Kotlin compiler, while still allowing usage of latest Kotlin toolchains within the project itself. We've set the minimum supported version to 1.9.0 as of this commit, which should revert the breaking change introduced with version [2.1.1](https://github.com/navikt/mock-oauth2-server/releases/tag/2.1.11), where the compiler minor version was incremented. Huge thanks to this massively useful blog post: https://blog.alllex.me/posts/2023-11-03-liberal-library-tooling/
1 parent e2e3e67 commit 19560a2

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

build.gradle.kts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java.time.Duration
22
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
33
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
4+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
45

56
val assertjVersion = "3.27.3"
67
val kotlinLoggingVersion = "3.0.5"
@@ -10,7 +11,6 @@ val mockWebServerVersion = "4.12.0"
1011
val jacksonVersion = "2.19.0"
1112
val nettyVersion = "4.2.2.Final"
1213
val junitJupiterVersion = "5.13.0"
13-
val kotlinVersion = "2.1.21"
1414
val freemarkerVersion = "2.3.34"
1515
val kotestVersion = "5.9.1"
1616
val bouncyCastleVersion = "1.81"
@@ -24,7 +24,7 @@ val mainClassKt = "no.nav.security.mock.oauth2.StandaloneMockOAuth2ServerKt"
2424

2525
plugins {
2626
application
27-
kotlin("jvm") version "2.1.21"
27+
alias(libs.plugins.kotlin.jvm) // refers to plugin declared in gradle/libs.versions.toml
2828
id("se.patrikerdes.use-latest-versions") version "0.2.18"
2929
id("com.github.ben-manes.versions") version "0.52.0"
3030
id("org.jmailen.kotlinter") version "5.1.0"
@@ -49,6 +49,28 @@ java {
4949
withSourcesJar()
5050
}
5151

52+
kotlin {
53+
val kotlinTarget = libs.versions.kotlinTarget
54+
val kotlinTargetVersion = kotlinTarget.map {
55+
KotlinVersion.fromVersion(it.toKotlinMinor())
56+
}
57+
58+
compilerOptions {
59+
languageVersion = kotlinTargetVersion
60+
apiVersion = kotlinTargetVersion
61+
// Syncing Kotlin JVM target with Java plugin JVM target
62+
jvmTarget = JvmTarget.JVM_17
63+
}
64+
65+
// Setting core libraries version to manage compile and runtime dependencies exposed in the published artifact metadata
66+
// These will become transitive dependencies for our users.
67+
// Core libraries for JVM are kotlin-stdlib and kotlin-test.
68+
coreLibrariesVersion = kotlinTarget.get()
69+
}
70+
71+
// 1.7.21 => 1.7, 1.9 => 1.9
72+
fun String.toKotlinMinor() = split(".").take(2).joinToString(".")
73+
5274
apply(plugin = "org.jmailen.kotlinter")
5375

5476
repositories {
@@ -73,7 +95,7 @@ dependencies {
7395
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitJupiterVersion")
7496
testImplementation("io.kotest:kotest-runner-junit5-jvm:$kotestVersion") // for kotest framework
7597
testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion") // for kotest core jvm assertions
76-
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5:$kotlinVersion")
98+
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5") // uses version matching kotlin-jvm plugin
7799
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion")
78100
// example use with different frameworks
79101
testImplementation("org.springframework.boot:spring-boot-starter-webflux:$springBootVersion")
@@ -289,12 +311,6 @@ tasks {
289311
}
290312
}
291313

292-
withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
293-
compilerOptions {
294-
jvmTarget.set(JvmTarget.JVM_17)
295-
}
296-
}
297-
298314
withType<Test> {
299315
jvmArgs("--add-opens=java.base/java.util=ALL-UNNAMED")
300316
useJUnitPlatform()

gradle/libs.versions.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[versions]
2+
kotlinTarget = "1.9.0" # Minimum supported Kotlin version for consumers of the library
3+
kotlinToolchain = "2.1.21" # Actual version used by tooling within this project
4+
5+
[plugins]
6+
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlinToolchain" }

0 commit comments

Comments
 (0)