publish.gradle 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2019-2020 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/master/LICENSE
  8. */
  9. // 部分源码来自 kotlinx.coroutines
  10. // Source code from kotlinx.coroutines
  11. tasks.register("ensureBintrayAvailable") {
  12. doLast {
  13. if (!upload.Bintray.isBintrayAvailable(project)) {
  14. throw new IllegalStateException("bintray isn't available. ")
  15. }
  16. }
  17. }
  18. def vcs = "https://github.com/mamoe/mirai"
  19. def pomConfig = {
  20. licenses {
  21. license {
  22. name "AGPLv3 with Mamoe Exceptions"
  23. url "https://github.com/mamoe/mirai/blob/master/LICENSE"
  24. distribution "repo"
  25. }
  26. }
  27. developers {
  28. developer {
  29. id "mamoe"
  30. name "Mamoe Technologies"
  31. email "[email protected]"
  32. }
  33. }
  34. scm {
  35. url vcs
  36. }
  37. }
  38. project.ext.configureMavenCentralMetadata = { pom ->
  39. def root = asNode()
  40. root.appendNode('name', project.name)
  41. root.appendNode('description', project.description)
  42. root.appendNode('url', vcs)
  43. root.children().last() + pomConfig
  44. }
  45. try {
  46. // empty xxx-javadoc.jar
  47. task javadocJar(type: Jar) {
  48. archiveClassifier = 'javadoc'
  49. }
  50. } catch (Exception ignored) {
  51. }
  52. try {
  53. task stubJavadoc(type: Jar) {
  54. archiveClassifier = 'javadoc'
  55. }
  56. } catch (Exception ignored) {
  57. }
  58. /**
  59. * Publish the platform JAR and POM so that consumers who depend on this module and can't read Gradle module
  60. * metadata can still get the platform artifact and transitive dependencies from the POM
  61. * (see details in https://youtrack.jetbrains.com/issue/KT-39184#focus=streamItem-27-4115233.0-0)
  62. */
  63. project.ext.publishPlatformArtifactsInRootModule = { platformPublication ->
  64. afterEvaluate {
  65. def platformPomBuilder = null
  66. platformPublication.pom.withXml { platformPomBuilder = asString() }
  67. publishing.publications.kotlinMultiplatform {
  68. platformPublication.artifacts.forEach {
  69. artifact(it)
  70. }
  71. pom.withXml {
  72. def pomStringBuilder = asString()
  73. pomStringBuilder.setLength(0)
  74. // The platform POM needs its artifact ID replaced with the artifact ID of the root module:
  75. def platformPomString = platformPomBuilder.toString()
  76. platformPomString.eachLine { line ->
  77. if (!line.contains("<!--")) { // Remove the Gradle module metadata marker as it will be added anew
  78. pomStringBuilder.append(line.replace(platformPublication.artifactId, artifactId))
  79. pomStringBuilder.append("\n")
  80. }
  81. }
  82. }
  83. }
  84. tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
  85. dependsOn(tasks["generatePomFileFor${platformPublication.name.capitalize()}Publication"])
  86. }
  87. }
  88. }
  89. def isKotlin137x = false
  90. afterEvaluate {
  91. publishing {
  92. def variantName = "${project.name}"
  93. // Rename artifacts for backward compatibility
  94. publications.all {
  95. def type = it.name
  96. logger.info("Configuring $type")
  97. switch (type) {
  98. case 'kotlinMultiplatform':
  99. if (isKotlin137x) {
  100. it.artifactId = "$variantName-native"
  101. it.artifact sourcesJar
  102. } else {
  103. // With Kotlin 1.4.0, the root module ID has no suffix, but for compatibility with
  104. // the consumers who can't read Gradle module metadata, we publish the JVM artifacts in it
  105. it.artifactId = variantName
  106. publishPlatformArtifactsInRootModule(publications["jvm"])
  107. }
  108. break
  109. case 'metadata':
  110. it.artifactId = isKotlin137x ? "$variantName-common" : "$variantName-metadata"
  111. break
  112. case 'jvm':
  113. it.artifactId = isKotlin137x ? "$variantName" : "$variantName-jvm"
  114. /*
  115. def files = tasks.getByName("shadowJarMd5").outputs.files + tasks.getByName("shadowJvmJar").outputs.files
  116. for (f in files) {
  117. artifact f
  118. }
  119. */
  120. break
  121. case 'js':
  122. it.artifactId = "$variantName-$type"
  123. break
  124. }
  125. logger.info("Artifact id = ${it.artifactId}")
  126. pom.withXml(configureMavenCentralMetadata)
  127. // The 'root' module publishes the JVM module's Javadoc JAR as per publishPlatformArtifactsInRootModule, and
  128. if (name != "kotlinMultiplatform")
  129. artifact stubJavadoc
  130. }
  131. if (isKotlin137x) {
  132. disableMetadataPublication()
  133. }
  134. }
  135. }
  136. if (upload.Bintray.isBintrayAvailable(project)) {
  137. apply from: rootProject.file("gradle/bintray.gradle")
  138. }
  139. /*
  140. task bintrayUpload(dependsOn: publish)
  141. // This is required for K/N publishing
  142. bintrayUpload.dependsOn publishToMavenLocal
  143. bintrayUpload.dependsOn generatePomFileForKotlinMultiplatformPublication
  144. */