Apply common configuration to custom publications in kotlin-test

To do that, extract parts of publishing setup into reusable functions.
This change makes signing and repository configuration applied to all
project publications, not just to the Main one.

Also:
- Get rid of dependencies from non-default variants in the root pom
- Add an empty javadoc jar

KT-40225
This commit is contained in:
Ilya Gorbunov
2021-01-27 02:58:46 +03:00
parent a16aaa3824
commit 573aac7252
2 changed files with 104 additions and 65 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -71,10 +71,34 @@ class KotlinBuildPublishingPlugin @Inject constructor(
create<MavenPublication>(PUBLICATION_NAME) { create<MavenPublication>(PUBLICATION_NAME) {
from(kotlinLibraryComponent) from(kotlinLibraryComponent)
configureKotlinPomAttributes(project)
}
}
}
configureDefaultPublishing()
}
companion object {
const val PUBLICATION_NAME = "Main"
const val REPOSITORY_NAME = "Maven"
const val ADHOC_COMPONENT_NAME = "kotlinLibrary"
const val COMPILE_CONFIGURATION = "publishedCompile"
const val RUNTIME_CONFIGURATION = "publishedRuntime"
}
}
@OptIn(ExperimentalStdlibApi::class)
private fun humanReadableName(name: String) =
name.split("-").joinToString(separator = " ") { it.capitalize(Locale.ROOT) }
fun MavenPublication.configureKotlinPomAttributes(project: Project, explicitDescription: String? = null) {
val publication = this
pom { pom {
packaging = "jar" packaging = "jar"
name.set(humanReadableName(project)) name.set(humanReadableName(publication.artifactId))
description.set(project.description ?: humanReadableName(project)) description.set(explicitDescription ?: project.description ?: humanReadableName(publication.artifactId))
url.set("https://kotlinlang.org/") url.set("https://kotlinlang.org/")
licenses { licenses {
license { license {
@@ -96,16 +120,30 @@ class KotlinBuildPublishingPlugin @Inject constructor(
} }
} }
} }
}
fun Project.configureDefaultPublishing() {
configure<PublishingExtension> {
repositories { repositories {
maven { maven {
name = REPOSITORY_NAME name = KotlinBuildPublishingPlugin.REPOSITORY_NAME
url = file("${project.rootDir}/build/repo").toURI() url = file("${project.rootDir}/build/repo").toURI()
} }
} }
} }
configureSigning()
tasks.register("install") {
dependsOn(tasks.named("publishToMavenLocal"))
}
tasks.withType<PublishToMavenRepository>()
.matching { it.name.endsWith("PublicationTo${KotlinBuildPublishingPlugin.REPOSITORY_NAME}Repository") }
.all { configureRepository() }
}
private fun Project.configureSigning() {
val signingRequired = provider { val signingRequired = provider {
project.findProperty("signingRequired")?.toString()?.toBoolean() project.findProperty("signingRequired")?.toString()?.toBoolean()
?: project.property("isSonatypeRelease") as Boolean ?: project.property("isSonatypeRelease") as Boolean
@@ -113,37 +151,19 @@ class KotlinBuildPublishingPlugin @Inject constructor(
configure<SigningExtension> { configure<SigningExtension> {
setRequired(signingRequired) setRequired(signingRequired)
sign(extensions.getByType<PublishingExtension>().publications[PUBLICATION_NAME]) sign(extensions.getByType<PublishingExtension>().publications) // all publications
useGpgCmd() useGpgCmd()
} }
tasks.withType<Sign>().configureEach { tasks.withType<Sign>().configureEach {
setOnlyIf { signingRequired.get() } setOnlyIf { signingRequired.get() }
} }
tasks.register("install") {
dependsOn(tasks.named("publishToMavenLocal"))
} }
tasks.named<PublishToMavenRepository>("publish${PUBLICATION_NAME}PublicationTo${REPOSITORY_NAME}Repository") fun TaskProvider<PublishToMavenRepository>.configureRepository() =
.configureRepository() configure { configureRepository() }
}
companion object { private fun PublishToMavenRepository.configureRepository() {
const val PUBLICATION_NAME = "Main"
const val REPOSITORY_NAME = "Maven"
const val ADHOC_COMPONENT_NAME = "kotlinLibrary"
const val COMPILE_CONFIGURATION = "publishedCompile"
const val RUNTIME_CONFIGURATION = "publishedRuntime"
@OptIn(ExperimentalStdlibApi::class)
fun humanReadableName(project: Project) =
project.name.split("-").joinToString(separator = " ") { it.capitalize(Locale.ROOT) }
}
}
fun TaskProvider<PublishToMavenRepository>.configureRepository() = configure {
dependsOn(project.rootProject.tasks.named("preparePublication")) dependsOn(project.rootProject.tasks.named("preparePublication"))
doFirst { doFirst {
val preparePublication = project.rootProject.tasks.named("preparePublication").get() val preparePublication = project.rootProject.tasks.named("preparePublication").get()
+26 -7
View File
@@ -1,9 +1,14 @@
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import plugins.configureDefaultPublishing
import plugins.configureKotlinPomAttributes
import groovy.util.Node
import groovy.util.NodeList
plugins { plugins {
`kotlin-multiplatform` apply false `kotlin-multiplatform` apply false
base base
`maven-publish` `maven-publish`
signing
} }
open class ComponentsFactoryAccess open class ComponentsFactoryAccess
@@ -248,42 +253,56 @@ val annotationsMetadataComponent = componentFactory.adhoc("annotations-common").
} }
} }
val emptyJavadocJar by tasks.creating(Jar::class) {
archiveClassifier.set("javadoc")
}
configureDefaultPublishing()
publishing { publishing {
publications { publications {
create("main", MavenPublication::class) { create("main", MavenPublication::class) {
from(rootComponent) from(rootComponent)
artifact(combinedSourcesJar) artifact(combinedSourcesJar)
// TODO: Remove all optional dependencies from root pom // Remove all optional dependencies from the root pom
pom.withXml {
val dependenciesNode = (asNode().get("dependencies") as NodeList).filterIsInstance<Node>().single()
val optionalDependencies = (dependenciesNode.get("dependency") as NodeList).filterIsInstance<Node>().filter {
((it.get("optional") as NodeList).singleOrNull() as Node?)?.text() == "true"
}
optionalDependencies.forEach { dependenciesNode.remove(it) }
}
configureKotlinPomAttributes(project, "Kotlin Test Multiplatform library")
} }
jvmTestFrameworks.forEach { framework -> jvmTestFrameworks.forEach { framework ->
create(framework, MavenPublication::class) { create(framework, MavenPublication::class) {
artifactId = "kotlin-test-$framework" artifactId = "kotlin-test-$framework"
from(components[framework]) from(components[framework])
artifact(tasks.getByPath(":kotlin-test:kotlin-test-$framework:sourcesJar") as Jar) artifact(tasks.getByPath(":kotlin-test:kotlin-test-$framework:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Support for $framework")
} }
} }
create("js", MavenPublication::class) { create("js", MavenPublication::class) {
artifactId = "kotlin-test-js" artifactId = "kotlin-test-js"
from(jsComponent) from(jsComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-js:sourcesJar") as Jar) artifact(tasks.getByPath(":kotlin-test:kotlin-test-js:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test for JS")
} }
create("common", MavenPublication::class) { create("common", MavenPublication::class) {
artifactId = "kotlin-test-common" artifactId = "kotlin-test-common"
from(commonMetadataComponent) from(commonMetadataComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-common:sourcesJar") as Jar) artifact(tasks.getByPath(":kotlin-test:kotlin-test-common:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Common")
} }
create("annotationsCommon", MavenPublication::class) { create("annotationsCommon", MavenPublication::class) {
artifactId = "kotlin-test-annotations-common" artifactId = "kotlin-test-annotations-common"
from(annotationsMetadataComponent) from(annotationsMetadataComponent)
artifact(tasks.getByPath(":kotlin-test:kotlin-test-annotations-common:sourcesJar") as Jar) artifact(tasks.getByPath(":kotlin-test:kotlin-test-annotations-common:sourcesJar") as Jar)
configureKotlinPomAttributes(project, "Kotlin Test Common")
}
withType<MavenPublication> {
suppressAllPomMetadataWarnings()
artifact(emptyJavadocJar)
} }
} }
} }
tasks {
val install by creating {
dependsOn(publishToMavenLocal)
}
}