|
| 1 | +/* |
| 2 | + * Copyright 2016-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +import com.github.jengelman.gradle.plugins.shadow.tasks.* |
| 6 | +import java.net.* |
| 7 | +import java.nio.file.* |
| 8 | + |
| 9 | +plugins { |
| 10 | + id("com.github.johnrengelman.shadow") |
| 11 | + id("org.jetbrains.kotlinx.kover") // apply plugin to use autocomplete for Kover DSL |
| 12 | +} |
| 13 | + |
| 14 | +configurations { |
| 15 | + val shadowDeps by creating |
| 16 | + compileOnly.configure { |
| 17 | + extendsFrom(shadowDeps) |
| 18 | + } |
| 19 | + runtimeOnly.configure { |
| 20 | + extendsFrom(shadowDeps) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +val junit_version by properties |
| 25 | +val junit5_version by properties |
| 26 | +val byte_buddy_version by properties |
| 27 | +val blockhound_version by properties |
| 28 | +val jna_version by properties |
| 29 | + |
| 30 | +dependencies { |
| 31 | + compileOnly("junit:junit:$junit_version") |
| 32 | + compileOnly("org.junit.jupiter:junit-jupiter-api:$junit5_version") |
| 33 | + testImplementation("org.junit.jupiter:junit-jupiter-engine:$junit5_version") |
| 34 | + testImplementation("org.junit.platform:junit-platform-testkit:1.7.0") |
| 35 | + add("shadowDeps", "net.bytebuddy:byte-buddy:$byte_buddy_version") |
| 36 | + add("shadowDeps", "net.bytebuddy:byte-buddy-agent:$byte_buddy_version") |
| 37 | + compileOnly("io.projectreactor.tools:blockhound:$blockhound_version") |
| 38 | + testImplementation("io.projectreactor.tools:blockhound:$blockhound_version") |
| 39 | + testImplementation("com.google.code.gson:gson:2.8.6") |
| 40 | + api("net.java.dev.jna:jna:$jna_version") |
| 41 | + api("net.java.dev.jna:jna-platform:$jna_version") |
| 42 | +} |
| 43 | + |
| 44 | +java { |
| 45 | + /* This is needed to be able to run JUnit5 tests. Otherwise, Gradle complains that it can't find the |
| 46 | + JVM1.6-compatible version of the `junit-jupiter-api` artifact. */ |
| 47 | + disableAutoTargetJvm() |
| 48 | +} |
| 49 | + |
| 50 | +// This is required for BlockHound tests to work, see https://github.com/Kotlin/kotlinx.coroutines/issues/3701 |
| 51 | +tasks.withType<Test>().configureEach { |
| 52 | + if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_13)) { |
| 53 | + jvmArgs("-XX:+AllowRedefinitionToAddDeleteMethods") |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +val jar by tasks.existing(Jar::class) { |
| 58 | + enabled = false |
| 59 | +} |
| 60 | + |
| 61 | +val shadowJar by tasks.existing(ShadowJar::class) { |
| 62 | + // Shadow only byte buddy, do not package kotlin stdlib |
| 63 | + configurations = listOf(project.configurations["shadowDeps"]) |
| 64 | + relocate("net.bytebuddy", "kotlinx.coroutines.repackaged.net.bytebuddy") |
| 65 | + /* These classifiers are both set to `null` to trick Gradle into thinking that this jar file is both the |
| 66 | + artifact from the `jar` task and the one from `shadowJar`. Without this, Gradle complains that the artifact |
| 67 | + from the `jar` task is not present when the compilaton finishes, even if the file with this name exists. */ |
| 68 | + archiveClassifier.convention(null as String?) |
| 69 | + archiveClassifier.set(null as String?) |
| 70 | + archiveBaseName.set(jar.flatMap { it.archiveBaseName }) |
| 71 | + archiveVersion.set(jar.flatMap { it.archiveVersion }) |
| 72 | + manifest { |
| 73 | + attributes( |
| 74 | + mapOf( |
| 75 | + "Premain-Class" to "kotlinx.coroutines.debug.AgentPremain", |
| 76 | + "Can-Redefine-Classes" to "true", |
| 77 | + "Multi-Release" to "true" |
| 78 | + ) |
| 79 | + ) |
| 80 | + } |
| 81 | + // add module-info.class to the META-INF/versions/9/ directory. |
| 82 | + dependsOn(tasks.compileModuleInfoJava) |
| 83 | + doLast { |
| 84 | + // We can't do that directly with the shadowJar task because it doesn't support replacing existing files. |
| 85 | + val zipPath = this@existing.outputs.files.singleFile.toPath() |
| 86 | + val zipUri = URI.create("jar:${zipPath.toUri()}") |
| 87 | + val moduleInfoFilePath = tasks.compileModuleInfoJava.get().outputs.files.asFileTree.matching { |
| 88 | + include("module-info.class") |
| 89 | + }.singleFile.toPath() |
| 90 | + FileSystems.newFileSystem(zipUri, emptyMap<String, String>()).use { fs -> |
| 91 | + val moduleInfoFile = fs.getPath("META-INF/versions/9/module-info.class") |
| 92 | + Files.copy(moduleInfoFilePath, moduleInfoFile, StandardCopyOption.REPLACE_EXISTING) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +configurations { |
| 98 | + // shadowJar is already part of the `shadowRuntimeElements` and `shadowApiElements`, but the other subprojects |
| 99 | + // that depend on `kotlinx-coroutines-debug` look at `runtimeElements` and `apiElements`. |
| 100 | + artifacts { |
| 101 | + add("apiElements", shadowJar) |
| 102 | + add("runtimeElements", shadowJar) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +koverReport { |
| 107 | + filters { |
| 108 | + excludes { |
| 109 | + // Never used, safety mechanism |
| 110 | + classes("kotlinx.coroutines.debug.internal.NoOpProbesKt") |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments