Skip to content

Commit

Permalink
Shade kotlinx metadata to prevent conflict with other libraries (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman authored May 10, 2023
1 parent 8641944 commit 933353e
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
19 changes: 17 additions & 2 deletions compiler/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("published-library")
kotlin("kapt")
id("com.github.johnrengelman.shadow")
}

tasks.compileKotlin {
Expand All @@ -11,6 +12,9 @@ tasks.compileKotlin {
}
}

val shade by configurations.named("compileShaded")

@Suppress("UnstableApiUsage")
dependencies {
implementation(projects.api)
implementation(libs.auto.service.api)
Expand All @@ -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")
}
1 change: 1 addition & 0 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
1 change: 1 addition & 0 deletions gradle-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pluginManagement {
dependencyResolutionManagement {
@Suppress("UnstableApiUsage")
repositories {
gradlePluginPortal()
mavenCentral()
}
versionCatalogs {
Expand Down
39 changes: 37 additions & 2 deletions gradle-plugin/src/main/kotlin/published-library.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -81,7 +84,11 @@ val publication = with(the<PublishingExtension>()) {
}

publications.register<MavenPublication>("kotshi") {
from(project.components.getByName("java"))
if (pluginManager.hasPlugin("com.github.johnrengelman.shadow")) {
the<ShadowExtension>().component(this)
} else {
from(components["java"])
}
artifact(sourcesJar)
artifact(dokkaJavadocJar)

Expand Down Expand Up @@ -112,8 +119,30 @@ val publication = with(the<PublishingExtension>()) {
}
}

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>("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<SigningExtension> {
gradle.taskGraph.whenReady {
if (hasTask("${path}:sign${publication.name.replaceFirstChar(Char::uppercase)}Publication")) {
Expand All @@ -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")
}
}
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
asm = "org.ow2.asm:asm:9.5"

shadow = "com.github.johnrengelman:shadow:8.1.1"
4 changes: 4 additions & 0 deletions publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

./gradlew clean
./gradlew publishAllPublicationsToMavenCentralRepository -PsignArtifacts=true --no-parallel

0 comments on commit 933353e

Please sign in to comment.