From 301e4efa2493261cea814164d3e88a452087f39f Mon Sep 17 00:00:00 2001 From: Xiaodong Wang Date: Sat, 16 Jul 2022 12:21:52 -0700 Subject: [PATCH] build: move all dependencies into version catalog. --- build.gradle.kts | 12 ++--- buildSrc/src/main/kotlin/build/Common.kt | 13 +++++ buildSrc/src/main/kotlin/build/Deps.kt | 11 ----- buildSrc/src/main/kotlin/build/Library.kt | 11 +++-- buildSrc/src/main/kotlin/build/Plugin.kt | 7 +-- gradle/libs.versions.toml | 58 +++++++++++++++++++++++ kert-graphql/build.gradle.kts | 15 +++--- kert-grpc-compiler/build.gradle.kts | 4 +- kert-grpc/build.gradle.kts | 18 +++---- kert-http/build.gradle.kts | 16 +++---- 10 files changed, 110 insertions(+), 55 deletions(-) create mode 100644 buildSrc/src/main/kotlin/build/Common.kt delete mode 100644 buildSrc/src/main/kotlin/build/Deps.kt create mode 100644 gradle/libs.versions.toml diff --git a/build.gradle.kts b/build.gradle.kts index 25c7c91..f2e410d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,3 @@ -import build.* import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.jetbrains.kotlin.gradle.tasks.KotlinCompile @@ -36,13 +35,12 @@ allprojects { } dependencies { - implementation("io.github.microutils:kotlin-logging:2.1.21") + // "libs" not working, must use "rootProject.libs" here + // https://github.com/gradle/gradle/issues/16634 + implementation(rootProject.libs.kotlin.logging) - testImplementation("io.kotest:kotest-framework-engine-jvm:${Deps.kotestVersion}") - testImplementation("io.kotest:kotest-runner-junit5-jvm:${Deps.kotestVersion}") - testImplementation("io.kotest:kotest-assertions-core-jvm:${Deps.kotestVersion}") - testImplementation("ch.qos.logback:logback-classic:1.2.3") - // testImplementation("org.slf4j:slf4j-simple:1.7.25") + testImplementation(rootProject.libs.bundles.kotest) + testImplementation(rootProject.libs.logback.classic) } // set target jvm version, otherwise gradle will use the jdk version during compiling for "org.gradle.jvm.version" in module file diff --git a/buildSrc/src/main/kotlin/build/Common.kt b/buildSrc/src/main/kotlin/build/Common.kt new file mode 100644 index 0000000..bf18c52 --- /dev/null +++ b/buildSrc/src/main/kotlin/build/Common.kt @@ -0,0 +1,13 @@ +package build + +import org.gradle.api.Project +import org.gradle.api.artifacts.VersionCatalog +import org.gradle.api.artifacts.VersionCatalogsExtension +import org.gradle.kotlin.dsl.getByType + +fun Project.versionCatalog(name: String): VersionCatalog { + return extensions.getByType().named(name) +} + +fun VersionCatalog.library(name: String) = findLibrary(name).get() +fun VersionCatalog.version(name: String) = findVersion(name).get() diff --git a/buildSrc/src/main/kotlin/build/Deps.kt b/buildSrc/src/main/kotlin/build/Deps.kt deleted file mode 100644 index 5f3f1f6..0000000 --- a/buildSrc/src/main/kotlin/build/Deps.kt +++ /dev/null @@ -1,11 +0,0 @@ -package build - -@Suppress("unused") -object Deps { - const val kotlinVersion = "1.6.10" - const val kotlinCoroutineVersion = "1.6.0" - const val protobufVersion = "3.19.4" - const val grpcJavaVersion = "1.44.0" - const val kotestVersion = "5.1.0" - const val vertxVersion = "4.2.4" -} diff --git a/buildSrc/src/main/kotlin/build/Library.kt b/buildSrc/src/main/kotlin/build/Library.kt index 342f1f7..e54ec41 100644 --- a/buildSrc/src/main/kotlin/build/Library.kt +++ b/buildSrc/src/main/kotlin/build/Library.kt @@ -13,17 +13,18 @@ import org.gradle.plugins.signing.SigningExtension * Config the project as a library. * It adds Kotlin dependency, and publishing support. */ -fun Project.librarySupport() { +fun Project.configureLibrary() { val api by configurations val implementation by configurations val dokkaHtmlPlugin by configurations + val libs = versionCatalog("libs") dependencies { - api(kotlin("stdlib-jdk8")) - api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:${Deps.kotlinCoroutineVersion}") - implementation("org.slf4j:slf4j-api:1.7.25") + api(libs.library("kotlin.stdlib")) + api(libs.library("kotlinx.coroutines")) + implementation(libs.library("slf4j.api")) - dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.6.10") + dokkaHtmlPlugin(libs.library("dokka.kotlin.as.java.plugin")) } val sourceSets = extensions.getByName("sourceSets") as SourceSetContainer diff --git a/buildSrc/src/main/kotlin/build/Plugin.kt b/buildSrc/src/main/kotlin/build/Plugin.kt index a862619..bd3ac63 100644 --- a/buildSrc/src/main/kotlin/build/Plugin.kt +++ b/buildSrc/src/main/kotlin/build/Plugin.kt @@ -30,10 +30,11 @@ private fun goOs(os: String): String { * Configure the project as a GRPC plugin. * It adds support for compiling the plugin to Linux, Windows and MacOS, and publishing support. */ -fun Project.grpcPluginSupport(pluginName: String) { +fun Project.configureGrpcPlugin(pluginName: String) { val testImplementation by configurations + val libs = versionCatalog("libs") dependencies { - testImplementation(kotlin("stdlib-jdk8")) + testImplementation(libs.library("kotlin-stdlib")) } // overwrite os & arch if specified by command line @@ -67,7 +68,7 @@ fun Project.grpcPluginSupport(pluginName: String) { protobuf { generatedFilesBaseDir = "$projectDir/gen" protoc { - artifact = "com.google.protobuf:protoc:${Deps.protobufVersion}" + artifact = "com.google.protobuf:protoc:${libs.version("protobuf")}" } plugins { id("grpc-kert") { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..1c22a27 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,58 @@ +# https://docs.gradle.org/current/userguide/platforms.html#sub:conventional-dependencies-toml + +[versions] +kotlin = "1.6.10" +kotlinx-coroutines = "1.6.0" +vertx = "4.2.4" +protobuf = "3.19.4" +grpc = "1.44.0" +kotest = "5.1.0" +jackson = "2.13.1" +graphql-kotlin = "5.3.2" +kotlin-logging = "2.1.21" +logback = "1.2.3" +slf4j = "1.7.30" + +[libraries] +kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } +kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" } +kotlin-script-runtime = { module = "org.jetbrains.kotlin:kotlin-script-runtime", version.ref = "kotlin" } +kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-jdk8", version.ref = "kotlinx-coroutines" } +kotlinx-coroutines-slf4j = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-slf4j", version.ref = "kotlinx-coroutines" } +vertx-web = { module = "io.vertx:vertx-web", version.ref = "vertx" } +vertx-lang-kotlin-coroutines = { module = "io.vertx:vertx-lang-kotlin-coroutines", version.ref = "vertx" } +vertx-web-client = { module = "io.vertx:vertx-web-client", version.ref = "vertx" } +protobuf-kotlin = { module = "com.google.protobuf:protobuf-kotlin", version.ref = "protobuf" } +protobuf-java = { module = "com.google.protobuf:protobuf-java", version.ref = "protobuf" } +grpc-protobuf = { module = "io.grpc:grpc-protobuf", version.ref = "grpc" } +grpc-stub = { module = "io.grpc:grpc-stub", version.ref = "grpc" } +grpc-netty = { module = "io.grpc:grpc-netty", version.ref = "grpc" } +javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version = "1.3.2" } +protoc-gen-grpc-java = { module = "io.grpc:protoc-gen-grpc-java", version.ref = "grpc" } +protoc = { module = "com.google.protobuf:protoc", version.ref = "protobuf" } +graphql-kotlin-server = { module = "com.expediagroup:graphql-kotlin-server", version.ref = "graphql-kotlin" } +graphql-kotlin-client = { module = "com.expediagroup:graphql-kotlin-client", version.ref = "graphql-kotlin" } +jackson-module-kotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" } +jackson-databind = { module = "com.fasterxml.jackson.core:jackson-databind", version.ref = "jackson" } +jackson-module-afterburner = { module = "com.fasterxml.jackson.module:jackson-module-afterburner", version.ref = "jackson" } +slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" } +dokka-kotlin-as-java-plugin = { module = "org.jetbrains.dokka:kotlin-as-java-plugin", version.ref = "kotlin" } +kotlin-logging = { module = "io.github.microutils:kotlin-logging", version.ref = "kotlin-logging" } +kotest-framework-engine-jvm = { module = "io.kotest:kotest-framework-engine-jvm", version.ref = "kotest" } +kotest-runner-junit5 = { module = "io.kotest:kotest-runner-junit5", version.ref = "kotest" } +kotest-assertions-core = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } +logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } + +[bundles] +kotlin = [ + "kotlin-stdlib", + "kotlin-reflect", + "kotlin-script-runtime" +] +kotest = [ + "kotest-framework-engine-jvm", + "kotest-runner-junit5", + "kotest-assertions-core" +] + +[plugins] diff --git a/kert-graphql/build.gradle.kts b/kert-graphql/build.gradle.kts index f348b22..3518cde 100644 --- a/kert-graphql/build.gradle.kts +++ b/kert-graphql/build.gradle.kts @@ -6,19 +6,16 @@ plugins { description = "Kert GraphQL support" -librarySupport() +configureLibrary() dependencies { - val graphqlKotlinVersion = "5.3.2" - val jacksonVersion = "2.13.1" - api(project(":kert-http")) - api("com.expediagroup:graphql-kotlin-server:${graphqlKotlinVersion}") - api("com.expediagroup:graphql-kotlin-client:${graphqlKotlinVersion}") + api(libs.graphql.kotlin.server) + api(libs.graphql.kotlin.client) - implementation("com.fasterxml.jackson.module:jackson-module-kotlin:${jacksonVersion}") - implementation("com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}") - implementation("com.fasterxml.jackson.module:jackson-module-afterburner:${jacksonVersion}") + implementation(libs.jackson.databind) + implementation(libs.jackson.module.kotlin) + implementation(libs.jackson.module.afterburner) } //graphql { diff --git a/kert-grpc-compiler/build.gradle.kts b/kert-grpc-compiler/build.gradle.kts index 5c0a904..7b2bc1e 100644 --- a/kert-grpc-compiler/build.gradle.kts +++ b/kert-grpc-compiler/build.gradle.kts @@ -2,11 +2,11 @@ import build.* description = "The protoc plugin for Kert" -grpcPluginSupport("protoc-gen-grpc-kert") +configureGrpcPlugin("protoc-gen-grpc-kert") dependencies { testImplementation(project(":kert-grpc")) - testImplementation("com.google.protobuf:protobuf-kotlin:${Deps.protobufVersion}") + testImplementation(libs.protobuf.kotlin) } tasks { diff --git a/kert-grpc/build.gradle.kts b/kert-grpc/build.gradle.kts index f1e7717..3f18ed3 100644 --- a/kert-grpc/build.gradle.kts +++ b/kert-grpc/build.gradle.kts @@ -3,27 +3,27 @@ import com.google.protobuf.gradle.* description = "Kert GRPC support" -librarySupport() +configureLibrary() dependencies { api(project(":kert-http")) - api("com.google.protobuf:protobuf-java:${Deps.protobufVersion}") - api("io.grpc:grpc-protobuf:${Deps.grpcJavaVersion}") - api("com.google.protobuf:protobuf-kotlin:${Deps.protobufVersion}") + api(libs.protobuf.java) + api(libs.protobuf.kotlin) + api(libs.grpc.protobuf) - api("javax.annotation:javax.annotation-api:1.3.2") + api(libs.javax.annotation.api) // generateTestProto needs compiler binary compileOnly(project(":kert-grpc-compiler")) - testImplementation("io.grpc:grpc-stub:${Deps.grpcJavaVersion}") - testImplementation("io.grpc:grpc-netty:${Deps.grpcJavaVersion}") + testImplementation(libs.grpc.stub) + testImplementation(libs.grpc.netty) } protobuf { generatedFilesBaseDir = "$projectDir/gen" protoc { - artifact = "com.google.protobuf:protoc:${Deps.protobufVersion}" + artifact = "com.google.protobuf:protoc:${libs.versions.protobuf.get()}" } plugins { id("grpc-kert") { @@ -31,7 +31,7 @@ protobuf { } // generate java version for performance comparison id("grpc-java") { - artifact = "io.grpc:protoc-gen-grpc-java:${Deps.grpcJavaVersion}" + artifact = "io.grpc:protoc-gen-grpc-java:${libs.versions.grpc.get()}" } } generateProtoTasks { diff --git a/kert-http/build.gradle.kts b/kert-http/build.gradle.kts index d6a5ecc..cfb1a79 100644 --- a/kert-http/build.gradle.kts +++ b/kert-http/build.gradle.kts @@ -2,18 +2,16 @@ import build.* description = "Kert HTTP support" -librarySupport() +configureLibrary() dependencies { - api("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Deps.kotlinVersion}") - api("org.jetbrains.kotlin:kotlin-reflect:${Deps.kotlinVersion}") - api("org.jetbrains.kotlin:kotlin-script-runtime:${Deps.kotlinVersion}") - api("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:${Deps.kotlinCoroutineVersion}") + api(libs.bundles.kotlin) - api("io.vertx:vertx-web:${Deps.vertxVersion}") - api("io.vertx:vertx-lang-kotlin-coroutines:${Deps.vertxVersion}") + api(libs.kotlinx.coroutines) + api(libs.kotlinx.coroutines.slf4j) - api("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:${Deps.kotlinCoroutineVersion}") + api(libs.vertx.web) + api(libs.vertx.lang.kotlin.coroutines) - testImplementation("io.vertx:vertx-web-client:${Deps.vertxVersion}") + testImplementation(libs.vertx.web.client) }