Skip to content

Commit

Permalink
Add version file to each module resources (Kotlin#2950)
Browse files Browse the repository at this point in the history
* 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 Kotlin#2941
  • Loading branch information
qwwdfsad authored and yorickhenning committed Oct 14, 2021
1 parent 6a99a6d commit 94c6fb7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
34 changes: 34 additions & 0 deletions build.gradle
Expand Up @@ -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<String> 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
Expand Down
1 change: 1 addition & 0 deletions integration-testing/build.gradle
Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
@@ -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")
}
}
}

0 comments on commit 94c6fb7

Please sign in to comment.