From 55f4a5b6dfb8adcd1573dab7e543bbd65e34168e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B3bert=20Papp?= Date: Fri, 14 Jul 2023 20:35:07 +0100 Subject: [PATCH] Fix #228: reduce parallelism in task and increase parallelism between tasks (#246) * Enable configuration cache * Remove parallelism inside compatTest tasks. * Remove kotlin-dsl and only use what is used in this project (kotlin-dsl dependency and sam with receiver). This prevents us from needing to fight with kotlin-dsl overriding language versions. * Move to new KGP DSL for setting up versions. (kotlinOptions to compilerOptions) * Make compatTest past without kotlin-dsl * Revert to kotlin-dsl, it does too many things apart from the forceful version. * Fix kotlin versioning * Simplify * Un-nest * Make a change to trigger rerun of tests --- build.gradle.kts | 59 ++++++++++++------- gradle.properties | 1 + .../publishplugin/TestExtensions.kt | 8 +-- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 714d7e78..c47bc67c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,7 @@ import org.gradle.initialization.IGradlePropertiesLoader.ENV_PROJECT_PROPERTIES_PREFIX import org.gradle.initialization.IGradlePropertiesLoader.SYSTEM_PROJECT_PROPERTIES_PREFIX -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion plugins { `kotlin-dsl` @@ -161,28 +162,44 @@ sourceSets { } } -tasks { - afterEvaluate { - // This needs to be in an afterEvaluate block, - // because otherwise KotlinDslCompilerPlugins would win, and override what we've set to Kotlin 1.8. - withType().configureEach { - kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString() - // Supporting Gradle 6.0+ needs to use Kotlin 1.3. - // See https://docs.gradle.org/current/userguide/compatibility.html - kotlinOptions.apiVersion = "1.3" - // Theoretically we could use newer language version here, - // but sadly the @kotlin.Metadata created on the classes would be incompatible with Kotlin 1.3 consumers. - kotlinOptions.languageVersion = "1.3" - doFirst { - if (kotlinOptions.apiVersion == "1.3") { - // Suppress "Language version 1.3 is deprecated and its support will be removed in a future version of Kotlin". - kotlinOptions.freeCompilerArgs += "-Xsuppress-version-warnings" - } else { - TODO("Remove -Xsuppress-version-warnings suppression, or change the condition to ${kotlinOptions.languageVersion}") - } +kotlin.target.compilations.configureEach { + // Supporting Gradle 6.0+ needs to use Kotlin 1.3. + // See https://docs.gradle.org/current/userguide/compatibility.html + // For future maintainer: Kotlin 1.9.0 dropped support for Kotlin 1.3, it'll only support 1.4+. + // This means Gradle 7.0 will be the lowest supportable version for plugins. + val usedKotlinVersion = @Suppress("DEPRECATION") KotlinVersion.KOTLIN_1_3 + + compilerOptions.configure { + // Gradle fully supports running on Java 8: https://docs.gradle.org/current/userguide/compatibility.html, + // so we should allow users to do that too. + jvmTarget = JvmTarget.fromTarget(JavaVersion.VERSION_1_8.toString()) + + // Suppress "Language version 1.3 is deprecated and its support will be removed in a future version of Kotlin". + freeCompilerArgs.add("-Xsuppress-version-warnings") + } + compileTaskProvider.configure { + // These two (api & lang) needs to be here instead of in compilations.compilerOptions.configure { }, + // to prevent KotlinDslCompilerPlugins overriding to Kotlin 1.8. + compilerOptions.apiVersion = usedKotlinVersion + // Theoretically we could use newer language version here, + // but sadly the @kotlin.Metadata created on the classes would be incompatible with older consumers. + compilerOptions.languageVersion = usedKotlinVersion + + // Validate that we're using the right version. + doFirst { + val api = compilerOptions.apiVersion.get() + val language = compilerOptions.languageVersion.get() + if (api != usedKotlinVersion || language != usedKotlinVersion) { + TODO( + "There's mismatch between configured and actual versions:\n" + + "apiVersion=${api}, languageVersion=${language}, configured=${usedKotlinVersion}." + ) } } } +} + +tasks { shadowJar { exclude("META-INF/maven/**", "META-INF/proguard/**", "META-INF/*.kotlin_module") manifest { @@ -228,7 +245,7 @@ tasks { withType().configureEach { dependsOn(shadowJar) useJUnitPlatform() - maxParallelForks = 8 + maxParallelForks = if (name.startsWith("compatTest")) 1 else 8 } withType().matching { it.name.startsWith("compatTest") }.configureEach { systemProperty("plugin.version", project.version) diff --git a/gradle.properties b/gradle.properties index aec0a559..8c5fc5cf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,2 +1,3 @@ org.gradle.caching=true org.gradle.parallel=true +org.gradle.configuration-cache=true diff --git a/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/TestExtensions.kt b/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/TestExtensions.kt index 0d39361e..3f3d9a8b 100644 --- a/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/TestExtensions.kt +++ b/src/compatTest/kotlin/io/github/gradlenexus/publishplugin/TestExtensions.kt @@ -20,13 +20,13 @@ import java.nio.file.Files import java.nio.file.Path fun Path.write(text: String): Path { - Files.createDirectories(parent) - toFile().writeText(text) + Files.createDirectories(this.parent) + this.toFile().writeText(text) return this } fun Path.append(text: String): Path { - Files.createDirectories(parent) - toFile().appendText(text) + Files.createDirectories(this.parent) + this.toFile().appendText(text) return this }