2
0

publish.gradle 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2019-2021 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 (!Bintray.isBintrayAvailable(project)) {
  14. throw new IllegalStateException("bintray isn't available. ")
  15. }
  16. }
  17. }
  18. try {
  19. // empty xxx-javadoc.jar
  20. task javadocJar(type: Jar) {
  21. archiveClassifier = 'javadoc'
  22. }
  23. } catch (Exception ignored) {
  24. }
  25. try {
  26. task stubJavadoc(type: Jar) {
  27. archiveClassifier = 'javadoc'
  28. }
  29. } catch (Exception ignored) {
  30. }
  31. /**
  32. * Publish the platform JAR and POM so that consumers who depend on this module and can't read Gradle module
  33. * metadata can still get the platform artifact and transitive dependencies from the POM
  34. * (see details in https://youtrack.jetbrains.com/issue/KT-39184#focus=streamItem-27-4115233.0-0)
  35. */
  36. project.ext.publishPlatformArtifactsInRootModule = { platformPublication ->
  37. def platformPomBuilder = null
  38. platformPublication.pom.withXml { platformPomBuilder = asString() }
  39. publishing.publications.kotlinMultiplatform {
  40. platformPublication.artifacts.forEach {
  41. println("Adding artiface to root: $it")
  42. artifact(it)
  43. }
  44. pom.withXml {
  45. def pomStringBuilder = asString()
  46. pomStringBuilder.setLength(0)
  47. // The platform POM needs its artifact ID replaced with the artifact ID of the root module:
  48. def platformPomString = platformPomBuilder.toString()
  49. platformPomString.eachLine { line ->
  50. if (!line.contains("<!--")) { // Remove the Gradle module metadata marker as it will be added anew
  51. pomStringBuilder.append(line.replace(platformPublication.artifactId, artifactId))
  52. pomStringBuilder.append("\n")
  53. }
  54. }
  55. }
  56. }
  57. tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
  58. dependsOn(tasks["generatePomFileFor${platformPublication.name.capitalize()}Publication"])
  59. }
  60. }
  61. def isKotlin137x = false
  62. afterEvaluate {
  63. publishing {
  64. def variantName = "${project.name}"
  65. // Rename artifacts for backward compatibility
  66. publications.all {
  67. // add empty javadocs
  68. if (it.name != "kotlinMultiplatform") {
  69. it.artifact(javadocJar)
  70. }
  71. // Rename MPP artifacts for backward compatibility
  72. def type = it.name
  73. switch (type) {
  74. case 'kotlinMultiplatform':
  75. // With Kotlin 1.4 & HMPP, the root module should have no suffix in the ID, but for compatibility with
  76. // the consumers who can't read Gradle module metadata, we publish the JVM artifacts in it, too
  77. it.artifactId = isKotlin137x ? "$project.name-native" : project.name
  78. if (!isKotlin137x) {
  79. publishPlatformArtifactsInRootModule(publications["jvm"])
  80. }
  81. break
  82. case 'metadata':
  83. // As the old -common dependencies will fail to resolve with Gradle module metadata, rename the module
  84. // to '*-metadata' so that the resolution failure are more clear
  85. it.artifactId = isKotlin137x ? "$project.name-common" : "$project.name-metadata"
  86. break
  87. case 'jvm':
  88. it.artifactId = isKotlin137x ? project.name : "$project.name-jvm"
  89. break
  90. case 'js':
  91. case 'native':
  92. it.artifactId = "$project.name-$type"
  93. break
  94. }
  95. // Hierarchical project structures are not fully supported in 1.3.7x MPP
  96. if (isKotlin137x) {
  97. // disable metadata everywhere, but in native and js modules
  98. if (type == 'maven' || type == 'metadata' || type == 'jvm') {
  99. moduleDescriptorGenerator = null
  100. }
  101. }
  102. }
  103. if (isKotlin137x) {
  104. disableMetadataPublication()
  105. }
  106. }
  107. }
  108. tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
  109. dependsOn(tasks["generatePomFileForJvmPublication"])
  110. }
  111. if (Bintray.isBintrayAvailable(project)) {
  112. project.configureBintray()
  113. }
  114. /*
  115. task bintrayUpload(dependsOn: publish)
  116. // This is required for K/N publishing
  117. bintrayUpload.dependsOn publishToMavenLocal
  118. bintrayUpload.dependsOn generatePomFileForKotlinMultiplatformPublication
  119. */