From 933353e1ba5d55c5cb605020b38205fbcd3d9d0a Mon Sep 17 00:00:00 2001 From: Nicklas Ansman Date: Tue, 9 May 2023 20:15:33 -0400 Subject: [PATCH] Shade kotlinx metadata to prevent conflict with other libraries (#225) --- .idea/compiler.xml | 3 ++ RELEASING.md | 2 +- build.gradle.kts | 6 +++ compiler/build.gradle.kts | 19 ++++++++- gradle-plugin/build.gradle.kts | 1 + gradle-plugin/settings.gradle.kts | 1 + .../main/kotlin/published-library.gradle.kts | 39 ++++++++++++++++++- gradle/libs.versions.toml | 4 +- publish.sh | 4 ++ 9 files changed, 73 insertions(+), 6 deletions(-) create mode 100755 publish.sh diff --git a/.idea/compiler.xml b/.idea/compiler.xml index b46ad1d..b396656 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -2,6 +2,9 @@ + + + diff --git a/RELEASING.md b/RELEASING.md index 3ec4d98..f1a05e8 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -2,7 +2,7 @@ 2. Change the version in `gradle.properties` to a non-snapshot version. 3. Update the `README.md` with the new version. 4. `git commit -am "Prepare for release X.Y.Z"` (where X.Y.Z is the new version) -5. `./gradlew clean publishAllPublicationsToMavenCentralRepository --no-parallel`. +5. `./publish.sh`. 6. Close and release on [Sonatype](https://oss.sonatype.org/#stagingRepositories). 7. Update the `gradle.properties` to the next SNAPSHOT version. 8. `git commit -am "Prepare next development version"` diff --git a/build.gradle.kts b/build.gradle.kts index 8699fe7..051239c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,12 @@ plugins { alias(libs.plugins.kotlinx.binaryCompatibilityValidator) } +buildscript { + dependencies { + classpath(libs.shadow) + } +} + apiValidation { allprojects.filterNot { it.path == ":api" }.mapTo(ignoredProjects) { it.name } } \ No newline at end of file diff --git a/compiler/build.gradle.kts b/compiler/build.gradle.kts index 78d542e..d9ab33f 100644 --- a/compiler/build.gradle.kts +++ b/compiler/build.gradle.kts @@ -1,6 +1,7 @@ plugins { id("published-library") kotlin("kapt") + id("com.github.johnrengelman.shadow") } tasks.compileKotlin { @@ -11,6 +12,9 @@ tasks.compileKotlin { } } +val shade by configurations.named("compileShaded") + +@Suppress("UnstableApiUsage") dependencies { implementation(projects.api) implementation(libs.auto.service.api) @@ -19,10 +23,21 @@ dependencies { kapt(libs.incap.compiler) implementation(libs.auto.common) implementation(libs.kotlinpoet.core) - implementation(libs.kotlinpoet.metadata) + shade(libs.kotlinpoet.metadata) { + exclude("org.jetbrains.kotlin") + exclude("com.squareup", "kotlinpoet") + exclude("com.google.guava") + exclude("com.google.auto", "auto-common") + } implementation(libs.kotlinpoet.ksp) - implementation(libs.kotlinx.metadata) + shade(libs.kotlinx.metadata) { + exclude("org.jetbrains.kotlin", "kotlin-stdlib") + } implementation(libs.moshi.oldestSupported) implementation(libs.ksp.api) implementation(libs.asm) } + +tasks.shadowJar { + relocate("com.squareup.kotlinpoet.metadata", "se.ansman.kotshi.compiler.kotlinpoet.metadata") +} \ No newline at end of file diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index b2353e2..6ae537e 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -8,6 +8,7 @@ dependencies { implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) api(libs.kotlin.gradlePlugin) api(libs.dokka.gradlePlugin) + api(libs.shadow) implementation(gradleKotlinDsl()) } diff --git a/gradle-plugin/settings.gradle.kts b/gradle-plugin/settings.gradle.kts index c14f82a..2860d2c 100644 --- a/gradle-plugin/settings.gradle.kts +++ b/gradle-plugin/settings.gradle.kts @@ -9,6 +9,7 @@ pluginManagement { dependencyResolutionManagement { @Suppress("UnstableApiUsage") repositories { + gradlePluginPortal() mavenCentral() } versionCatalogs { diff --git a/gradle-plugin/src/main/kotlin/published-library.gradle.kts b/gradle-plugin/src/main/kotlin/published-library.gradle.kts index c965012..8e6288f 100644 --- a/gradle-plugin/src/main/kotlin/published-library.gradle.kts +++ b/gradle-plugin/src/main/kotlin/published-library.gradle.kts @@ -1,4 +1,7 @@ +import com.github.jengelman.gradle.plugins.shadow.ShadowExtension +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import com.github.jengelman.gradle.plugins.shadow.transformers.ServiceFileTransformer import org.gradle.api.internal.tasks.userinput.UserInputHandler import org.gradle.configurationcache.extensions.serviceOf import org.gradle.jvm.tasks.Jar @@ -81,7 +84,11 @@ val publication = with(the()) { } publications.register("kotshi") { - from(project.components.getByName("java")) + if (pluginManager.hasPlugin("com.github.johnrengelman.shadow")) { + the().component(this) + } else { + from(components["java"]) + } artifact(sourcesJar) artifact(dokkaJavadocJar) @@ -112,8 +119,30 @@ val publication = with(the()) { } } +pluginManager.withPlugin("com.github.johnrengelman.shadow") { + val shade: Configuration = configurations.create("compileShaded") + configurations.named("compileOnly") { + extendsFrom(shade) + } + configurations.named("testRuntimeOnly") { + extendsFrom(shade) + } + + val shadowJar = tasks.named("shadowJar") { + archiveClassifier.set("") + configurations = listOf(shade) + isEnableRelocation = true + relocationPrefix = "se.ansman.kotshi${project.path.replace(':', '.').replace('-', '_')}" + transformers.add(ServiceFileTransformer()) + } -if (System.getenv("CI") == null) { + artifacts { + runtimeOnly(shadowJar) + archives(shadowJar) + } +} + +if (providers.gradleProperty("signArtifacts").orNull?.toBooleanStrict() == true) { configure { gradle.taskGraph.whenReady { if (hasTask("${path}:sign${publication.name.replaceFirstChar(Char::uppercase)}Publication")) { @@ -131,4 +160,10 @@ if (System.getenv("CI") == null) { tasks.register("publishSnapshot") { enabled = version.toString().endsWith("-SNAPSHOT") dependsOn("publishAllPublicationsToSonatypeSnapshotsRepository") +} + +pluginManager.withPlugin("org.jetbrains.kotlin.kapt") { + tasks.named("dokkaJavadoc") { + dependsOn("compileKotlin") + } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2783652..b494039 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -45,4 +45,6 @@ incap-compiler = { module = "net.ltgt.gradle.incap:incap-processor", version.ref ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" } -asm = "org.ow2.asm:asm:9.5" \ No newline at end of file +asm = "org.ow2.asm:asm:9.5" + +shadow = "com.github.johnrengelman:shadow:8.1.1" \ No newline at end of file diff --git a/publish.sh b/publish.sh new file mode 100755 index 0000000..575263a --- /dev/null +++ b/publish.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +./gradlew clean +./gradlew publishAllPublicationsToMavenCentralRepository -PsignArtifacts=true --no-parallel \ No newline at end of file