From 94c6fb787b03f82a9abb601865abb99e1df39c29 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Mon, 27 Sep 2021 11:55:48 +0300 Subject: [PATCH] Add version file to each module resources (#2950) * Add version file to each module resources * The approach with "Specification-Version" in Manifest doesn't work because Android merges all JARs into a single resource, trimming all the manifests Fixes #2941 --- build.gradle | 34 ++++++++++++++++ integration-testing/build.gradle | 1 + ...t => MavenPublicationAtomicfuValidator.kt} | 2 +- .../MavenPublicationVersionValidator.kt | 40 +++++++++++++++++++ 4 files changed, 76 insertions(+), 1 deletion(-) rename integration-testing/src/mavenTest/kotlin/{MavenPublicationValidator.kt => MavenPublicationAtomicfuValidator.kt} (97%) create mode 100644 integration-testing/src/mavenTest/kotlin/MavenPublicationVersionValidator.kt diff --git a/build.gradle b/build.gradle index 6e0b250ea3..f55e6c39d2 100644 --- a/build.gradle +++ b/build.gradle @@ -233,6 +233,40 @@ configure(subprojects.findAll { !unpublished.contains(it.name) }) { } } } + + def thisProject = it + if (thisProject.name in sourceless) { + return + } + + def versionFileTask = thisProject.tasks.register("versionFileTask") { + def name = thisProject.name.replace("-", "_") + def versionFile = thisProject.layout.buildDirectory.file("${name}.version") + it.outputs.file(versionFile) + + it.doLast { + versionFile.get().asFile.text = version.toString() + } + } + + List jarTasks + if (it.name == "kotlinx-coroutines-core") { + jarTasks = ["jvmJar", "metadataJar"] + } else if (it.name == "kotlinx-coroutines-debug") { + // We shadow debug module instead of just packaging it + jarTasks = ["shadowJar"] + } else { + jarTasks = ["jar"] + } + + for (name in jarTasks) { + thisProject.tasks.named(name, Jar) { + it.dependsOn versionFileTask + it.from(versionFileTask) { + into("META-INF") + } + } + } } // Report Kotlin compiler version when building project diff --git a/integration-testing/build.gradle b/integration-testing/build.gradle index 6efa3a14e6..d0286d7d55 100644 --- a/integration-testing/build.gradle +++ b/integration-testing/build.gradle @@ -58,6 +58,7 @@ task npmTest(type: Test) { } task mavenTest(type: Test) { + environment "version", version def sourceSet = sourceSets.mavenTest dependsOn(project(':').getTasksByName("publishToMavenLocal", true)) testClassesDirs = sourceSet.output.classesDirs diff --git a/integration-testing/src/mavenTest/kotlin/MavenPublicationValidator.kt b/integration-testing/src/mavenTest/kotlin/MavenPublicationAtomicfuValidator.kt similarity index 97% rename from integration-testing/src/mavenTest/kotlin/MavenPublicationValidator.kt rename to integration-testing/src/mavenTest/kotlin/MavenPublicationAtomicfuValidator.kt index 39d6598b55..dbb1921d80 100644 --- a/integration-testing/src/mavenTest/kotlin/MavenPublicationValidator.kt +++ b/integration-testing/src/mavenTest/kotlin/MavenPublicationAtomicfuValidator.kt @@ -8,7 +8,7 @@ import org.junit.* import org.junit.Assert.assertTrue import java.util.jar.* -class MavenPublicationValidator { +class MavenPublicationAtomicfuValidator { private val ATOMIC_FU_REF = "Lkotlinx/atomicfu/".toByteArray() @Test diff --git a/integration-testing/src/mavenTest/kotlin/MavenPublicationVersionValidator.kt b/integration-testing/src/mavenTest/kotlin/MavenPublicationVersionValidator.kt new file mode 100644 index 0000000000..da87d4cc59 --- /dev/null +++ b/integration-testing/src/mavenTest/kotlin/MavenPublicationVersionValidator.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.coroutines.validator + +import org.junit.* +import org.junit.Test +import java.util.jar.* +import kotlin.test.* + +class MavenPublicationVersionValidator { + + @Test + fun testMppJar() { + val clazz = Class.forName("kotlinx.coroutines.Job") + JarFile(clazz.protectionDomain.codeSource.location.file).checkForVersion("kotlinx_coroutines_core.version") + } + + @Test + fun testAndroidJar() { + val clazz = Class.forName("kotlinx.coroutines.android.HandlerDispatcher") + JarFile(clazz.protectionDomain.codeSource.location.file).checkForVersion("kotlinx_coroutines_android.version") + } + + private fun JarFile.checkForVersion(file: String) { + val actualFile = "META-INF/$file" + val version = System.getenv("version") + use { + for (e in entries()) { + if (e.name == actualFile) { + val string = getInputStream(e).readAllBytes().decodeToString() + assertEquals(version, string) + return + } + } + error("File $file not found") + } + } +}