Skip to content

Commit f575f2a

Browse files
committed
Publishing the java part of Kotlin multiplatform jars is now supported.
1 parent ebb0e1c commit f575f2a

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878

7979
# Changelog
8080
## [Unreleased]
81+
### Added
82+
- Publishing the java part of Kotlin multiplatform jars is now supported.
8183

8284
## [7.1.1] - 2023-12-11
8385
### Changed

src/main/resources/base/changelog.gradle

+16-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def requireEnablePublishing(Task task) {
1717
}
1818
}
1919

20+
def List<String> pubNames() {
21+
if (plugins.hasPlugin('java-gradle-plugin')) {
22+
return ['PluginMaven']
23+
} else if (plugins.hasPlugin('org.jetbrains.kotlin.multiplatform')) {
24+
return ['KotlinMultiplatform', 'Jvm']
25+
} else {
26+
return ['MavenJava']
27+
}
28+
}
29+
2030
if (tasks.names.contains('changelogCheck')) {
2131
spotlessChangelog {
2232
branch 'release'
@@ -36,8 +46,9 @@ if (tasks.names.contains('changelogCheck')) {
3646
if (requireEnablePublishing(it)) {
3747
if (plugins.hasPlugin('maven-publish')) {
3848
if (plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) {
39-
String pubName = plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava'
40-
dependsOn "publish${pubName}PublicationToSonatypeRepository"
49+
for (pubName in pubNames()) {
50+
dependsOn "publish${pubName}PublicationToSonatypeRepository"
51+
}
4152
// only the root project has closeSonatypeStagingRepository, and it finalizes all publishToSonatype tasks
4253
// https://github.com/gradle-nexus/publish-plugin
4354
dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository')
@@ -69,8 +80,9 @@ if (tasks.names.contains('changelogCheck')) {
6980
if (requireEnablePublishing(it)) {
7081
if (childProject.plugins.hasPlugin('maven-publish')) {
7182
if (rootProject.plugins.hasPlugin('io.github.gradle-nexus.publish-plugin')) {
72-
String pubName = childProject.plugins.hasPlugin('java-gradle-plugin') ? 'PluginMaven' : 'MavenJava'
73-
dependsOn childProject.tasks.named("publish${pubName}PublicationToSonatypeRepository")
83+
for (pubName in pubNames()) {
84+
dependsOn childProject.tasks.named("publish${pubName}PublicationToSonatypeRepository")
85+
}
7486
dependsOn rootProject.tasks.named('closeAndReleaseSonatypeStagingRepository')
7587
} else {
7688
dependsOn childProject.tasks.named("publishAllPublicationsToGitLabRepository")

src/main/resources/base/maven.gradle

+24-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def license_url = 干.prop('base/maven', "${license}_url")
2020
def license_comments =.prop('base/maven', "${license}_comments")
2121

2222
boolean isKMP = project.pluginManager.hasPlugin('org.jetbrains.kotlin.multiplatform')
23+
boolean isPlugin = plugins.hasPlugin('java-gradle-plugin')
24+
2325
if (!isKMP) {
2426
java {
2527
withJavadocJar()
@@ -52,9 +54,27 @@ if (!isKMP) {
5254
}
5355

5456

55-
boolean isPlugin = plugins.hasPlugin('java-gradle-plugin')
56-
publishing.publications.register(isPlugin ? 'pluginMaven' : 'mavenJava', MavenPublication, {
57-
artifactId = maven_artifact
57+
if (publishing.publications.isEmpty()) {
58+
publishing.publications.register(isPlugin ? 'pluginMaven' : 'mavenJava', MavenPublication, {
59+
})
60+
}
61+
62+
publishing.publications.withType(MavenPublication) {
63+
// KMP-aware artifactId
64+
if (artifactId.endsWith('-jvm')) {
65+
artifactId = maven_artifact + '-jvm'
66+
// create javadoc manually to keep MavenCentral happy
67+
def javadocJar = tasks.register('dokkaJavadoc', Jar.class) {
68+
dependsOn('dokkatooGenerate')
69+
archiveClassifier.set("javadoc")
70+
from('build/dokka-module/html')
71+
}
72+
artifact javadocJar
73+
} else if (artifactId.endsWith("-js")) {
74+
artifactId = maven_artifact + '-js'
75+
} else {
76+
artifactId = maven_artifact
77+
}
5878
if (!isPlugin && !isKMP) {
5979
from components.java
6080
}
@@ -85,4 +105,4 @@ publishing.publications.register(isPlugin ? 'pluginMaven' : 'mavenJava', MavenPu
85105
url = "https://$git_url"
86106
}
87107
}
88-
})
108+
}

0 commit comments

Comments
 (0)