Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-debug/build.gradle.kts (Kotlin)

Code before:
// There doesn't seem to be a way to ask the shadow jar to contain the `module-info.class` provided by us and not
// the one provided by bytebuddy. So, this is done in two steps: `shadowJarTask` excludes the module-info from
// bytebuddy, and this task creates another jar file, this time one that has the correct module-info.
val shadowJarWithCorrectModuleInfo by tasks.registering(Jar::class) {
    group = "shadow"
    from(zipTree(shadowJar.archiveFile))
    from(tasks.compileModuleInfoJava) {
        // Include **only** file we are interested in as JavaCompile output also contains some tmp files
        include("module-info.class")
        into("META-INF/versions/9/")
    }
    duplicatesStrategy = DuplicatesStrategy.FAIL
    archiveBaseName.set(jar.archiveBaseName)
    archiveVersion.set(jar.archiveVersion)
    archiveClassifier.set(jar.archiveClassifier)
    manifest {
        attributes(mapOf(
            "Premain-Class" to "kotlinx.coroutines.debug.AgentPremain",
            "Can-Redefine-Classes" to "true",
            "Multi-Release" to "true"
        ))
    }
    VersionFile.fromVersionFile(this, versionFileTask)
}

tasks.getByName("publishMavenPublicationToMavenLocal") {
    dependsOn(shadowJarWithCorrectModuleInfo)
}

configurations.all {
    // we're not interested at all in the output of the shadowJar task; it's just an intermediate stage
    outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(shadowJar) }
    // if something wants a `jar` to be published, we want to publish the `shadowJarWithCorrectModuleInfo` instead
    if (outgoing.artifacts.buildDependencies.getDependencies(null).contains(jar)) {
        outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
        outgoing.artifact(shadowJarWithCorrectModuleInfo)
    }
}

koverReport {
    filters {
        excludes {
            // Never used, safety mechanism
            classes("kotlinx.coroutines.debug.internal.NoOpProbesKt")
        }
    }
}


Review comment (suggestion):
```suggestion
tasks.named("publishMavenPublicationToMavenLocal") {
```

[Configuration avoidance](https://docs.gradle.org/current/userguide/task_configuration_avoidance.html), `getByName` will force-create the task, while `named` won't.

Code after:
    artifact from the `jar` task and the one from `shadowJar`. Without this, Gradle complains that the artifact
    from the `jar` task is not present when the compilaton finishes, even if the file with this name exists. */
    archiveClassifier.convention(null as String?)
    archiveClassifier.set(null as String?)
    archiveBaseName.set(jar.flatMap { it.archiveBaseName })
    archiveVersion.set(jar.flatMap { it.archiveVersion })
    manifest {
        attributes(
            mapOf(
                "Premain-Class" to "kotlinx.coroutines.debug.AgentPremain",
                "Can-Redefine-Classes" to "true",
                "Multi-Release" to "true"
            )
        )
    }
    // add module-info.class to the META-INF/versions/9/ directory.
    dependsOn(tasks.compileModuleInfoJava)
    doLast {
        // We can't do that directly with the shadowJar task because it doesn't support replacing existing files.
        val zipPath = this@existing.outputs.files.singleFile.toPath()
        val zipUri = URI.create("jar:${zipPath.toUri()}")
        val moduleInfoFilePath = tasks.compileModuleInfoJava.get().outputs.files.asFileTree.matching {
            include("module-info.class")
        }.singleFile.toPath()
        FileSystems.newFileSystem(zipUri, emptyMap<String, String>()).use { fs ->
            val moduleInfoFile = fs.getPath("META-INF/versions/9/module-info.class")
            Files.copy(moduleInfoFilePath, moduleInfoFile, StandardCopyOption.REPLACE_EXISTING)
        }
    }
}

configurations {
    // shadowJar is already part of the `shadowRuntimeElements` and `shadowApiElements`, but the other subprojects
    // that depend on `kotlinx-coroutines-debug` look at `runtimeElements` and `apiElements`.
    artifacts {
        add("apiElements", shadowJar)
        add("runtimeElements", shadowJar)
    }
}

koverReport {
    filters {
        excludes {
            // Never used, safety mechanism
            classes("kotlinx.coroutines.debug.internal.NoOpProbesKt")
        }
    }
}