Skip to content

Commit 5c75e7a

Browse files
authored
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
1 parent 76fbfc9 commit 5c75e7a

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

build.gradle

+34
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,40 @@ configure(subprojects.findAll { !unpublished.contains(it.name) }) {
233233
}
234234
}
235235
}
236+
237+
def thisProject = it
238+
if (thisProject.name in sourceless) {
239+
return
240+
}
241+
242+
def versionFileTask = thisProject.tasks.register("versionFileTask") {
243+
def name = thisProject.name.replace("-", "_")
244+
def versionFile = thisProject.layout.buildDirectory.file("${name}.version")
245+
it.outputs.file(versionFile)
246+
247+
it.doLast {
248+
versionFile.get().asFile.text = version.toString()
249+
}
250+
}
251+
252+
List<String> jarTasks
253+
if (it.name == "kotlinx-coroutines-core") {
254+
jarTasks = ["jvmJar", "metadataJar"]
255+
} else if (it.name == "kotlinx-coroutines-debug") {
256+
// We shadow debug module instead of just packaging it
257+
jarTasks = ["shadowJar"]
258+
} else {
259+
jarTasks = ["jar"]
260+
}
261+
262+
for (name in jarTasks) {
263+
thisProject.tasks.named(name, Jar) {
264+
it.dependsOn versionFileTask
265+
it.from(versionFileTask) {
266+
into("META-INF")
267+
}
268+
}
269+
}
236270
}
237271

238272
// Report Kotlin compiler version when building project

integration-testing/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ task npmTest(type: Test) {
5858
}
5959

6060
task mavenTest(type: Test) {
61+
environment "version", version
6162
def sourceSet = sourceSets.mavenTest
6263
dependsOn(project(':').getTasksByName("publishToMavenLocal", true))
6364
testClassesDirs = sourceSet.output.classesDirs

integration-testing/src/mavenTest/kotlin/MavenPublicationValidator.kt renamed to integration-testing/src/mavenTest/kotlin/MavenPublicationAtomicfuValidator.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import org.junit.*
88
import org.junit.Assert.assertTrue
99
import java.util.jar.*
1010

11-
class MavenPublicationValidator {
11+
class MavenPublicationAtomicfuValidator {
1212
private val ATOMIC_FU_REF = "Lkotlinx/atomicfu/".toByteArray()
1313

1414
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.validator
6+
7+
import org.junit.*
8+
import org.junit.Test
9+
import java.util.jar.*
10+
import kotlin.test.*
11+
12+
class MavenPublicationVersionValidator {
13+
14+
@Test
15+
fun testMppJar() {
16+
val clazz = Class.forName("kotlinx.coroutines.Job")
17+
JarFile(clazz.protectionDomain.codeSource.location.file).checkForVersion("kotlinx_coroutines_core.version")
18+
}
19+
20+
@Test
21+
fun testAndroidJar() {
22+
val clazz = Class.forName("kotlinx.coroutines.android.HandlerDispatcher")
23+
JarFile(clazz.protectionDomain.codeSource.location.file).checkForVersion("kotlinx_coroutines_android.version")
24+
}
25+
26+
private fun JarFile.checkForVersion(file: String) {
27+
val actualFile = "META-INF/$file"
28+
val version = System.getenv("version")
29+
use {
30+
for (e in entries()) {
31+
if (e.name == actualFile) {
32+
val string = getInputStream(e).readAllBytes().decodeToString()
33+
assertEquals(version, string)
34+
return
35+
}
36+
}
37+
error("File $file not found")
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)