From bd464556b6ac43e57a3a3c13c79683916f3280fc Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 24 May 2023 01:53:12 +0530 Subject: [PATCH] feat(build): use JVM toolchains to enforce Java 17 usage This simplifies project setup in both CI and for individual contributors --- build-logic/build.gradle.kts | 22 +++++-------------- build-logic/settings.gradle.kts | 4 ++++ .../app/passwordstore/gradle/AndroidCommon.kt | 6 ----- .../gradle/KotlinAndroidPlugin.kt | 6 +++++ .../gradle/KotlinCommonPlugin.kt | 20 +++++++++-------- .../passwordstore/gradle/KotlinJVMLibrary.kt | 6 +++++ settings.gradle.kts | 1 + 7 files changed, 33 insertions(+), 32 deletions(-) diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 0f3d9342..6f7a78c0 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -2,28 +2,16 @@ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * SPDX-License-Identifier: GPL-3.0-only */ -import org.gradle.api.JavaVersion -import org.gradle.api.tasks.compile.JavaCompile -import org.gradle.kotlin.dsl.withType -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { `kotlin-dsl` } -tasks.withType().configureEach { - sourceCompatibility = JavaVersion.VERSION_17.toString() - targetCompatibility = JavaVersion.VERSION_17.toString() +private val jvmToolchainAction = Action { + languageVersion.set(JavaLanguageVersion.of(17)) + vendor.set(JvmVendorSpec.ADOPTIUM) } -tasks.withType().configureEach { - kotlinOptions { - jvmTarget = JavaVersion.VERSION_17.toString() - freeCompilerArgs = - freeCompilerArgs + - listOf( - "-opt-in=kotlin.RequiresOptIn", - ) - } -} +java.toolchain(jvmToolchainAction) +kotlin.jvmToolchain(jvmToolchainAction) gradlePlugin { plugins { diff --git a/build-logic/settings.gradle.kts b/build-logic/settings.gradle.kts index 0c7a0f8c..9345c8b5 100644 --- a/build-logic/settings.gradle.kts +++ b/build-logic/settings.gradle.kts @@ -6,6 +6,10 @@ rootProject.name = "build-logic" +plugins { + id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" +} + dependencyResolutionManagement { repositories { exclusiveContent { diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/AndroidCommon.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/AndroidCommon.kt index 82211f40..d73239c2 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/AndroidCommon.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/AndroidCommon.kt @@ -5,7 +5,6 @@ import app.passwordstore.gradle.flavors.configureSlimTests import com.android.build.api.dsl.ApplicationExtension import com.android.build.api.dsl.LibraryExtension import com.android.build.gradle.TestedExtension -import org.gradle.api.JavaVersion import org.gradle.api.Project import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.api.tasks.testing.Test @@ -32,11 +31,6 @@ object AndroidCommon { resources.excludes.add("**/META-INF/LGPL2.1") } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } - testOptions { animationsDisabled = true unitTests.isReturnDefaultValues = true diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinAndroidPlugin.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinAndroidPlugin.kt index 49f207a1..553e8acd 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinAndroidPlugin.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinAndroidPlugin.kt @@ -5,9 +5,13 @@ package app.passwordstore.gradle +import app.passwordstore.gradle.KotlinCommonPlugin.Companion.JVM_TOOLCHAIN_ACTION import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.getByType +import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension import org.jetbrains.kotlin.gradle.plugin.KotlinAndroidPluginWrapper @Suppress("Unused") @@ -18,5 +22,7 @@ class KotlinAndroidPlugin : Plugin { apply(KotlinAndroidPluginWrapper::class) apply(KotlinCommonPlugin::class) } + project.extensions.getByType().toolchain(JVM_TOOLCHAIN_ACTION) + project.extensions.getByType().jvmToolchain(JVM_TOOLCHAIN_ACTION) } } diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinCommonPlugin.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinCommonPlugin.kt index 5052be3c..4b69abf7 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinCommonPlugin.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinCommonPlugin.kt @@ -5,14 +5,15 @@ package app.passwordstore.gradle -import org.gradle.api.JavaVersion +import org.gradle.api.Action import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.logging.TestLogEvent +import org.gradle.jvm.toolchain.JavaLanguageVersion +import org.gradle.jvm.toolchain.JavaToolchainSpec +import org.gradle.jvm.toolchain.JvmVendorSpec import org.gradle.kotlin.dsl.withType -import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.KotlinVersion import org.jetbrains.kotlin.gradle.tasks.KotlinCompile @@ -22,13 +23,8 @@ class KotlinCommonPlugin : Plugin { override fun apply(project: Project) { val isAppModule = project.pluginManager.hasPlugin("com.android.application") project.tasks.run { - withType().configureEach { - sourceCompatibility = JavaVersion.VERSION_17.toString() - targetCompatibility = JavaVersion.VERSION_17.toString() - } withType().configureEach task@{ compilerOptions { - jvmTarget.set(JvmTarget.JVM_17) allWarningsAsErrors.set(true) languageVersion.set(KotlinVersion.KOTLIN_1_8) freeCompilerArgs.addAll(ADDITIONAL_COMPILER_ARGS) @@ -44,11 +40,17 @@ class KotlinCommonPlugin : Plugin { } } - private companion object { + companion object { private val ADDITIONAL_COMPILER_ARGS = listOf( "-opt-in=kotlin.RequiresOptIn", "-Xsuppress-version-warnings", ) + + val JVM_TOOLCHAIN_ACTION = + Action { + languageVersion.set(JavaLanguageVersion.of(17)) + vendor.set(JvmVendorSpec.ADOPTIUM) + } } } diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinJVMLibrary.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinJVMLibrary.kt index c5cf84a0..2c55796b 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinJVMLibrary.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/KotlinJVMLibrary.kt @@ -5,13 +5,17 @@ package app.passwordstore.gradle +import app.passwordstore.gradle.KotlinCommonPlugin.Companion.JVM_TOOLCHAIN_ACTION import app.passwordstore.gradle.LintConfig.configureLint import com.android.build.api.dsl.Lint import com.android.build.gradle.LintPlugin import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.getByType +import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper @Suppress("Unused") @@ -24,5 +28,7 @@ class KotlinJVMLibrary : Plugin { apply(KotlinCommonPlugin::class) } project.extensions.configure { configureLint(project, isJVM = true) } + project.extensions.getByType().toolchain(JVM_TOOLCHAIN_ACTION) + project.extensions.getByType().jvmToolchain(JVM_TOOLCHAIN_ACTION) } } diff --git a/settings.gradle.kts b/settings.gradle.kts index eee3fc5e..c67725dd 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -8,6 +8,7 @@ rootProject.name = "APS" // Plugin repositories pluginManagement { + plugins { id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0" } repositories { includeBuild("build-logic") exclusiveContent {