mirai/gradle/publish.gradle

161 lines
4.8 KiB
Groovy
Raw Normal View History

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 {
2020-08-18 09:24:44 +08:00
if (!upload.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
def vcs = "https://github.com/mamoe/mirai"
def pomConfig = {
licenses {
license {
name "AGPLv3 with Mamoe Exceptions"
url "https://github.com/mamoe/mirai/blob/master/LICENSE"
distribution "repo"
}
}
developers {
developer {
id "mamoe"
name "Mamoe Technologies"
email "support@mamoe.net"
}
}
scm {
url vcs
}
}
project.ext.configureMavenCentralMetadata = { pom ->
def root = asNode()
root.appendNode('name', project.name)
root.appendNode('description', project.description)
root.appendNode('url', vcs)
root.children().last() + pomConfig
}
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-08-18 09:24:44 +08:00
}catch (Exception ignored) {
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 ->
afterEvaluate {
def platformPomBuilder = null
platformPublication.pom.withXml { platformPomBuilder = asString() }
publishing.publications.kotlinMultiplatform {
platformPublication.artifacts.forEach {
artifact(it)
}
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
}
2020-08-18 09:24:44 +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 {
def type = it.name
logger.info("Configuring $type")
switch (type) {
case 'kotlinMultiplatform':
if (isKotlin137x) {
it.artifactId = "$variantName-native"
it.artifact sourcesJar
} else {
// With Kotlin 1.4.0, the root module ID has no suffix, but for compatibility with
// the consumers who can't read Gradle module metadata, we publish the JVM artifacts in it
it.artifactId = variantName
publishPlatformArtifactsInRootModule(publications["jvm"])
}
break
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
case 'metadata':
it.artifactId = isKotlin137x ? "$variantName-common" : "$variantName-metadata"
break
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
case 'jvm':
it.artifactId = isKotlin137x ? "$variantName" : "$variantName-jvm"
break
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
case 'js':
it.artifactId = "$variantName-$type"
break
}
logger.info("Artifact id = ${it.artifactId}")
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
pom.withXml(configureMavenCentralMetadata)
2020-02-16 13:54:42 +08:00
2020-08-18 09:24:44 +08:00
// The 'root' module publishes the JVM module's Javadoc JAR as per publishPlatformArtifactsInRootModule, and
2019-11-23 22:34:57 +08:00
2020-08-18 09:24:44 +08:00
if (name != "kotlinMultiplatform")
artifact stubJavadoc
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
}
2020-08-18 09:24:44 +08:00
if (upload.Bintray.isBintrayAvailable(project)) {
apply from: rootProject.file("gradle/bintray.gradle")
}
/*
task bintrayUpload(dependsOn: publish)
// This is required for K/N publishing
bintrayUpload.dependsOn publishToMavenLocal
bintrayUpload.dependsOn generatePomFileForKotlinMultiplatformPublication
*/