feat(build): use JVM toolchains to enforce Java 17 usage

This simplifies project setup in both CI and for individual contributors
This commit is contained in:
Harsh Shandilya 2023-05-24 01:53:12 +05:30
parent 5f1a429029
commit bd464556b6
No known key found for this signature in database
7 changed files with 33 additions and 32 deletions

View file

@ -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<JavaCompile>().configureEach {
sourceCompatibility = JavaVersion.VERSION_17.toString()
targetCompatibility = JavaVersion.VERSION_17.toString()
private val jvmToolchainAction = Action<JavaToolchainSpec> {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.ADOPTIUM)
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
freeCompilerArgs =
freeCompilerArgs +
listOf(
"-opt-in=kotlin.RequiresOptIn",
)
}
}
java.toolchain(jvmToolchainAction)
kotlin.jvmToolchain(jvmToolchainAction)
gradlePlugin {
plugins {

View file

@ -6,6 +6,10 @@
rootProject.name = "build-logic"
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
}
dependencyResolutionManagement {
repositories {
exclusiveContent {

View file

@ -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

View file

@ -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<Project> {
apply(KotlinAndroidPluginWrapper::class)
apply(KotlinCommonPlugin::class)
}
project.extensions.getByType<JavaPluginExtension>().toolchain(JVM_TOOLCHAIN_ACTION)
project.extensions.getByType<KotlinProjectExtension>().jvmToolchain(JVM_TOOLCHAIN_ACTION)
}
}

View file

@ -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<Project> {
override fun apply(project: Project) {
val isAppModule = project.pluginManager.hasPlugin("com.android.application")
project.tasks.run {
withType<JavaCompile>().configureEach {
sourceCompatibility = JavaVersion.VERSION_17.toString()
targetCompatibility = JavaVersion.VERSION_17.toString()
}
withType<KotlinCompile>().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<Project> {
}
}
private companion object {
companion object {
private val ADDITIONAL_COMPILER_ARGS =
listOf(
"-opt-in=kotlin.RequiresOptIn",
"-Xsuppress-version-warnings",
)
val JVM_TOOLCHAIN_ACTION =
Action<JavaToolchainSpec> {
languageVersion.set(JavaLanguageVersion.of(17))
vendor.set(JvmVendorSpec.ADOPTIUM)
}
}
}

View file

@ -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<Project> {
apply(KotlinCommonPlugin::class)
}
project.extensions.configure<Lint> { configureLint(project, isJVM = true) }
project.extensions.getByType<JavaPluginExtension>().toolchain(JVM_TOOLCHAIN_ACTION)
project.extensions.getByType<KotlinProjectExtension>().jvmToolchain(JVM_TOOLCHAIN_ACTION)
}
}

View file

@ -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 {