Skip to content

Commit 819f126

Browse files
authored
Add Kotlin version override (#245)
- Add a dependency constraint in the root project to allow overriding the Kotlin version. - The Kotlin version is not overridden in the Benchmark Gradle plugin - this should use the version of Kotlin embedded into Gradle. - Add a check to ensure that the version is overridden. - add some comments to explain Kotlin version override
1 parent 0764551 commit 819f126

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

build.gradle

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt
12
import tasks.CheckReadmeTask
23

34
buildscript {
@@ -10,6 +11,16 @@ buildscript {
1011

1112
dependencies {
1213
classpath(libs.kotlinx.teamInfraGradlePlugin)
14+
15+
String kotlinVersion = providers.gradleProperty("kotlin_version").orNull
16+
if (kotlinVersion != null && !kotlinVersion.isBlank()) {
17+
// In addition to overriding the Kotlin version in the Version Catalog,
18+
// also enforce the KGP version using a dependency constraint.
19+
// Constraints are stricter than Version Catalog.
20+
constraints {
21+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
22+
}
23+
}
1324
}
1425
}
1526

@@ -54,8 +65,22 @@ afterEvaluate {
5465
}
5566
//endregion
5667

68+
String currentKgpVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project)
69+
logger.info("Using Kotlin Gradle Plugin ${currentKgpVersion}")
70+
71+
String kotlinVersionOverride = providers.gradleProperty("kotlin_version").getOrNull()
72+
73+
if (kotlinVersionOverride != null) {
74+
String versionCatalogKotlinVersion = libs.versions.kotlin.get()
75+
if (kotlinVersionOverride != versionCatalogKotlinVersion) {
76+
throw new IllegalStateException("Kotlin version in Version Catalog was not overridden. Expected:$kotlinVersionOverride, actual:$versionCatalogKotlinVersion.")
77+
}
78+
if (kotlinVersionOverride != currentKgpVersion) {
79+
throw new IllegalStateException("Kotlin Gradle Plugin version was not overridden. Expected:$kotlinVersionOverride, actual:$currentKgpVersion.")
80+
}
81+
}
82+
5783
allprojects {
58-
logger.info("Using Kotlin ${libs.versions.kotlin.get()} for project $it")
5984
repositories {
6085
KotlinCommunity.addDevRepositoryIfEnabled(delegate, project)
6186
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[versions]
22

3+
# Note: Kotlin version can be overridden by passing `-Pkotlin_version=<version>`
34
kotlin = "1.9.21"
45
kotlinx-binaryCompatibilityValidator = "0.15.0-Beta.1"
56
kotlinx-teamInfra = "0.4.0-dev-81"

plugin/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ buildscript {
1313

1414
dependencies {
1515
classpath(libs.kotlinx.teamInfraGradlePlugin)
16+
// Note: unlike the root project, don't override KGP version in this project.
17+
// Gradle plugins should only use the embedded-kotlin version.
18+
// kotlinx-benchmark uses an external KGP the moment... but that should be fixed
19+
// https://github.com/Kotlin/kotlinx-benchmark/issues/244
1620
}
1721
}
1822

settings.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ pluginManagement {
77
}
88
}
99

10+
dependencyResolutionManagement {
11+
versionCatalogs {
12+
create("libs") {
13+
String kotlinVersion = providers.gradleProperty("kotlin_version").orNull
14+
if (kotlinVersion != null && !kotlinVersion.isBlank()) {
15+
// Override the default Kotlin version.
16+
// The only intended use-case is for testing dev Kotlin builds using kotlinx-benchmark.
17+
// The Kotlin version should not be overridden during regular development.
18+
version("kotlin", kotlinVersion)
19+
}
20+
}
21+
}
22+
}
23+
1024
rootProject.name = 'kotlinx-benchmark'
1125

1226
includeBuild("plugin")

0 commit comments

Comments
 (0)