mirai/gradle/publish.gradle

145 lines
4.9 KiB
Groovy
Raw Normal View History

2020-11-01 15:07:32 +08:00
/*
2021-01-21 10:08:21 +08:00
* Copyright 2019-2021 Mamoe Technologies and contributors.
2020-11-01 15:07:32 +08:00
*
* 使 GNU AFFERO GENERAL PUBLIC LICENSE version 3 , .
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
2019-11-23 22:34:57 +08:00
// 部分源码来自 kotlinx.coroutines
2020-02-18 14:28:28 +08:00
// Source code from kotlinx.coroutines
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
tasks.register("ensureBintrayAvailable") {
2020-04-11 00:28:18 +08:00
doLast {
2021-01-21 10:08:21 +08:00
if (!Bintray.isBintrayAvailable(project)) {
2020-04-11 00:28:18 +08:00
throw new IllegalStateException("bintray isn't available. ")
}
2020-04-11 00:21:44 +08:00
}
}
2020-04-11 00:18:14 +08:00
2020-08-18 09:24:44 +08:00
try {
// empty xxx-javadoc.jar
task javadocJar(type: Jar) {
archiveClassifier = 'javadoc'
}
} catch (Exception ignored) {
2020-04-11 00:18:14 +08:00
}
2020-08-18 09:24:44 +08:00
try {
task stubJavadoc(type: Jar) {
archiveClassifier = 'javadoc'
2019-11-23 22:34:57 +08:00
}
2020-09-22 13:23:08 +08:00
} catch (Exception ignored) {
2020-08-18 09:24:44 +08:00
2019-11-23 22:34:57 +08:00
}
2020-08-18 09:24:44 +08:00
/**
* Publish the platform JAR and POM so that consumers who depend on this module and can't read Gradle module
* metadata can still get the platform artifact and transitive dependencies from the POM
* (see details in https://youtrack.jetbrains.com/issue/KT-39184#focus=streamItem-27-4115233.0-0)
*/
project.ext.publishPlatformArtifactsInRootModule = { platformPublication ->
2021-01-21 10:08:21 +08:00
def platformPomBuilder = null
2020-08-18 09:24:44 +08:00
2021-01-21 10:08:21 +08:00
platformPublication.pom.withXml { platformPomBuilder = asString() }
2020-08-18 09:24:44 +08:00
2021-01-21 10:08:21 +08:00
publishing.publications.kotlinMultiplatform {
platformPublication.artifacts.forEach {
println("Adding artiface to root: $it")
artifact(it)
}
2020-08-18 09:24:44 +08:00
2021-01-21 10:08:21 +08:00
pom.withXml {
def pomStringBuilder = asString()
pomStringBuilder.setLength(0)
// The platform POM needs its artifact ID replaced with the artifact ID of the root module:
def platformPomString = platformPomBuilder.toString()
platformPomString.eachLine { line ->
if (!line.contains("<!--")) { // Remove the Gradle module metadata marker as it will be added anew
pomStringBuilder.append(line.replace(platformPublication.artifactId, artifactId))
pomStringBuilder.append("\n")
2020-04-12 00:47:36 +08:00
}
}
2019-11-23 22:34:57 +08:00
}
2021-01-21 10:08:21 +08:00
}
2020-08-18 09:24:44 +08:00
2021-01-21 10:08:21 +08:00
tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
dependsOn(tasks["generatePomFileFor${platformPublication.name.capitalize()}Publication"])
2019-11-23 22:34:57 +08:00
}
}
2020-08-18 09:24:44 +08:00
def isKotlin137x = false
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
afterEvaluate {
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
publishing {
def variantName = "${project.name}"
// Rename artifacts for backward compatibility
publications.all {
2021-01-21 10:08:21 +08:00
// add empty javadocs
if (it.name != "kotlinMultiplatform") {
it.artifact(javadocJar)
}
// Rename MPP artifacts for backward compatibility
2020-08-18 09:24:44 +08:00
def type = it.name
switch (type) {
case 'kotlinMultiplatform':
2021-01-21 10:08:21 +08:00
// With Kotlin 1.4 & HMPP, the root module should have no suffix in the ID, but for compatibility with
// the consumers who can't read Gradle module metadata, we publish the JVM artifacts in it, too
it.artifactId = isKotlin137x ? "$project.name-native" : project.name
if (!isKotlin137x) {
publishPlatformArtifactsInRootModule(publications["jvm"])
2020-08-18 09:24:44 +08:00
}
break
case 'metadata':
2021-01-21 10:08:21 +08:00
// As the old -common dependencies will fail to resolve with Gradle module metadata, rename the module
// to '*-metadata' so that the resolution failure are more clear
it.artifactId = isKotlin137x ? "$project.name-common" : "$project.name-metadata"
2020-08-18 09:24:44 +08:00
break
case 'jvm':
2021-01-21 10:08:21 +08:00
it.artifactId = isKotlin137x ? project.name : "$project.name-jvm"
2020-08-18 09:24:44 +08:00
break
case 'js':
2021-01-21 10:08:21 +08:00
case 'native':
it.artifactId = "$project.name-$type"
2020-08-18 09:24:44 +08:00
break
}
2021-01-21 10:08:21 +08:00
// Hierarchical project structures are not fully supported in 1.3.7x MPP
if (isKotlin137x) {
// disable metadata everywhere, but in native and js modules
if (type == 'maven' || type == 'metadata' || type == 'jvm') {
moduleDescriptorGenerator = null
}
}
2019-11-23 22:34:57 +08:00
}
2020-08-18 09:24:44 +08:00
if (isKotlin137x) {
disableMetadataPublication()
2019-11-23 22:34:57 +08:00
}
}
2020-04-12 00:42:07 +08:00
}
2021-01-21 10:08:21 +08:00
tasks.matching { it.name == "generatePomFileForKotlinMultiplatformPublication" }.configureEach {
dependsOn(tasks["generatePomFileForJvmPublication"])
}
2020-08-18 09:24:44 +08:00
2021-01-21 10:08:21 +08:00
if (Bintray.isBintrayAvailable(project)) {
project.configureBintray()
2020-08-18 09:24:44 +08:00
}
/*
task bintrayUpload(dependsOn: publish)
// This is required for K/N publishing
bintrayUpload.dependsOn publishToMavenLocal
bintrayUpload.dependsOn generatePomFileForKotlinMultiplatformPublication
*/