Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions buildSrc/src/main/kotlin/Java9Modularity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,28 @@ object Java9Modularity {
)
})
}

/**
* For multiplatform Gradle modules, generate and set `Automatic-Module-Name` in metadata JAR's manifest.
*
* Generated automatic (JPMS) module name has the following format:
* `<Gradle module name>. artifact_disambiguating_module`.
*
* For multiplatform projects, a metadata artifact is the one without a platform-specific suffix,
* and it always depends on corresponding `-jvm` artifact (for convenience on build systems other than Gradle).
* For a JVM project depending on such an artifact and using JPMS, it may result in the automatic module name clash
* with a module provided by the `-jvm` artifact. By explicitly setting a non-clashing automatic module name in
* metadata JAR's manifest, we're mitigating this issue.
*/
fun Project.configureMetadataJarAutomaticModuleName() {
val kotlin = extensions.findByType<KotlinMultiplatformExtension>() ?: return
val moduleName = project.name.replace('-', '.') + ".artifact_disambiguating_module"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: Manifest lines are supposed to be 72 chars in length

No line may be longer than 72 bytes (not characters), in its UTF8-encoded form. If a value would make the initial line longer than this, it should be continued on extra lines (each starting with a single SPACE).
https://docs.oracle.com/en/java/javase/25/docs/specs/jar/jar.html

So this gets automatically wrapped to next line:

Manifest-Version: 1.0
Automatic-Module-Name: kotlinx.serialization.core.artifact_disambiguatin
  g_module

While it shouldn't cause any problems, we may want to choose a shorter name to look a bit nicer. WDYT of common_artifact suffix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I picked the same name we agreed to use for coroutines for consistency.

I'm not sure if we should pursue shorter name here, as nobody is expected to import that module. And I hope not so many people have to read manifest files bundled in third-party libraries (to enjoy nice name :)).

If you prefer a shorter name, than kotlin_metadata or just metadata might be a better option compared to common_artifact.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to kotlinx.coroutines name then

tasks.withType<Jar>().named(kotlin.metadata().artifactsTaskName) {
manifest {
attributes(
"Automatic-Module-Name" to moduleName,
)
}
}
}
}
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Java9Modularity.configureJava9ModuleInfo
import Java9Modularity.configureMetadataJarAutomaticModuleName
import org.jetbrains.kotlin.gradle.targets.js.ir.*

/*
Expand Down Expand Up @@ -69,6 +70,7 @@ tasks.withType<Jar>().named(kotlin.jvm().artifactsTaskName) {
}

configureJava9ModuleInfo()
configureMetadataJarAutomaticModuleName()

tasks.withType<KotlinJsIrLink>().configureEach {
compilerOptions.freeCompilerArgs.add("-Xwasm-enable-array-range-checks")
Expand Down
2 changes: 2 additions & 0 deletions formats/cbor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Java9Modularity.configureJava9ModuleInfo
import Java9Modularity.configureMetadataJarAutomaticModuleName

/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Expand Down Expand Up @@ -36,3 +37,4 @@ kotlin {
}

configureJava9ModuleInfo()
configureMetadataJarAutomaticModuleName()
4 changes: 2 additions & 2 deletions formats/json-io/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import Java9Modularity.configureJava9ModuleInfo
import org.jetbrains.dokka.gradle.*
import java.net.*
import Java9Modularity.configureMetadataJarAutomaticModuleName

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -32,6 +31,7 @@ kotlin {
}

project.configureJava9ModuleInfo()
project.configureMetadataJarAutomaticModuleName()

dokka.dokkaSourceSets.configureEach {
externalDocumentationLinks.register("kotlinx-io") {
Expand Down
4 changes: 2 additions & 2 deletions formats/json-okio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Copyright 2017-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import Java9Modularity.configureJava9ModuleInfo
import org.jetbrains.dokka.gradle.*
import java.net.*
import Java9Modularity.configureMetadataJarAutomaticModuleName

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -33,6 +32,7 @@ kotlin {
}

project.configureJava9ModuleInfo()
project.configureMetadataJarAutomaticModuleName()

dokka.dokkaSourceSets.configureEach {
externalDocumentationLinks.register("okio") {
Expand Down
3 changes: 2 additions & 1 deletion formats/json/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Java9Modularity.configureJava9ModuleInfo
import org.jetbrains.kotlin.gradle.tasks.*
import Java9Modularity.configureMetadataJarAutomaticModuleName

/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Expand Down Expand Up @@ -53,3 +53,4 @@ kotlin {
}

configureJava9ModuleInfo()
configureMetadataJarAutomaticModuleName()
2 changes: 2 additions & 0 deletions formats/properties/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Java9Modularity.configureJava9ModuleInfo
import Java9Modularity.configureMetadataJarAutomaticModuleName

/*
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Expand Down Expand Up @@ -36,3 +37,4 @@ kotlin {
}

configureJava9ModuleInfo()
configureMetadataJarAutomaticModuleName()
2 changes: 2 additions & 0 deletions formats/protobuf/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
import Java9Modularity.configureJava9ModuleInfo
import Java9Modularity.configureMetadataJarAutomaticModuleName

plugins {
kotlin("multiplatform")
Expand Down Expand Up @@ -34,3 +35,4 @@ kotlin {
}

configureJava9ModuleInfo()
configureMetadataJarAutomaticModuleName()