From 573aac7252ed441a344fd8c452e3722fc3e1879a Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 27 Jan 2021 02:58:46 +0300 Subject: [PATCH] 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 --- .../plugins/KotlinBuildPublishingPlugin.kt | 136 ++++++++++-------- libraries/kotlin.test/build.gradle.kts | 33 ++++- 2 files changed, 104 insertions(+), 65 deletions(-) diff --git a/buildSrc/src/main/kotlin/plugins/KotlinBuildPublishingPlugin.kt b/buildSrc/src/main/kotlin/plugins/KotlinBuildPublishingPlugin.kt index 00ff6701d4f..022e49d7f12 100644 --- a/buildSrc/src/main/kotlin/plugins/KotlinBuildPublishingPlugin.kt +++ b/buildSrc/src/main/kotlin/plugins/KotlinBuildPublishingPlugin.kt @@ -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. */ @@ -71,62 +71,11 @@ class KotlinBuildPublishingPlugin @Inject constructor( create(PUBLICATION_NAME) { from(kotlinLibraryComponent) - pom { - packaging = "jar" - name.set(humanReadableName(project)) - description.set(project.description ?: humanReadableName(project)) - url.set("https://kotlinlang.org/") - licenses { - license { - name.set("The Apache License, Version 2.0") - url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") - } - } - scm { - url.set("https://github.com/JetBrains/kotlin") - connection.set("scm:git:https://github.com/JetBrains/kotlin.git") - developerConnection.set("scm:git:https://github.com/JetBrains/kotlin.git") - } - developers { - developer { - name.set("Kotlin Team") - organization.set("JetBrains") - organizationUrl.set("https://www.jetbrains.com") - } - } - } - } - } - - repositories { - maven { - name = REPOSITORY_NAME - url = file("${project.rootDir}/build/repo").toURI() + configureKotlinPomAttributes(project) } } } - - val signingRequired = provider { - project.findProperty("signingRequired")?.toString()?.toBoolean() - ?: project.property("isSonatypeRelease") as Boolean - } - - configure { - setRequired(signingRequired) - sign(extensions.getByType().publications[PUBLICATION_NAME]) - useGpgCmd() - } - - tasks.withType().configureEach { - setOnlyIf { signingRequired.get() } - } - - tasks.register("install") { - dependsOn(tasks.named("publishToMavenLocal")) - } - - tasks.named("publish${PUBLICATION_NAME}PublicationTo${REPOSITORY_NAME}Repository") - .configureRepository() + configureDefaultPublishing() } companion object { @@ -137,13 +86,84 @@ class KotlinBuildPublishingPlugin @Inject constructor( 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.configureRepository() = configure { +@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 { + packaging = "jar" + name.set(humanReadableName(publication.artifactId)) + description.set(explicitDescription ?: project.description ?: humanReadableName(publication.artifactId)) + url.set("https://kotlinlang.org/") + licenses { + license { + name.set("The Apache License, Version 2.0") + url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") + } + } + scm { + url.set("https://github.com/JetBrains/kotlin") + connection.set("scm:git:https://github.com/JetBrains/kotlin.git") + developerConnection.set("scm:git:https://github.com/JetBrains/kotlin.git") + } + developers { + developer { + name.set("Kotlin Team") + organization.set("JetBrains") + organizationUrl.set("https://www.jetbrains.com") + } + } + } +} + + +fun Project.configureDefaultPublishing() { + configure { + repositories { + maven { + name = KotlinBuildPublishingPlugin.REPOSITORY_NAME + url = file("${project.rootDir}/build/repo").toURI() + } + } + } + + configureSigning() + + tasks.register("install") { + dependsOn(tasks.named("publishToMavenLocal")) + } + + tasks.withType() + .matching { it.name.endsWith("PublicationTo${KotlinBuildPublishingPlugin.REPOSITORY_NAME}Repository") } + .all { configureRepository() } +} + +private fun Project.configureSigning() { + val signingRequired = provider { + project.findProperty("signingRequired")?.toString()?.toBoolean() + ?: project.property("isSonatypeRelease") as Boolean + } + + configure { + setRequired(signingRequired) + sign(extensions.getByType().publications) // all publications + useGpgCmd() + } + + tasks.withType().configureEach { + setOnlyIf { signingRequired.get() } + } +} + +fun TaskProvider.configureRepository() = + configure { configureRepository() } + +private fun PublishToMavenRepository.configureRepository() { dependsOn(project.rootProject.tasks.named("preparePublication")) doFirst { val preparePublication = project.rootProject.tasks.named("preparePublication").get() diff --git a/libraries/kotlin.test/build.gradle.kts b/libraries/kotlin.test/build.gradle.kts index 253f916d798..7bcc959afff 100644 --- a/libraries/kotlin.test/build.gradle.kts +++ b/libraries/kotlin.test/build.gradle.kts @@ -1,9 +1,14 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType +import plugins.configureDefaultPublishing +import plugins.configureKotlinPomAttributes +import groovy.util.Node +import groovy.util.NodeList plugins { `kotlin-multiplatform` apply false base `maven-publish` + signing } 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 { publications { create("main", MavenPublication::class) { from(rootComponent) 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().single() + val optionalDependencies = (dependenciesNode.get("dependency") as NodeList).filterIsInstance().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 -> create(framework, MavenPublication::class) { artifactId = "kotlin-test-$framework" from(components[framework]) artifact(tasks.getByPath(":kotlin-test:kotlin-test-$framework:sourcesJar") as Jar) + configureKotlinPomAttributes(project, "Kotlin Test Support for $framework") } } create("js", MavenPublication::class) { artifactId = "kotlin-test-js" from(jsComponent) artifact(tasks.getByPath(":kotlin-test:kotlin-test-js:sourcesJar") as Jar) + configureKotlinPomAttributes(project, "Kotlin Test for JS") } create("common", MavenPublication::class) { artifactId = "kotlin-test-common" from(commonMetadataComponent) artifact(tasks.getByPath(":kotlin-test:kotlin-test-common:sourcesJar") as Jar) + configureKotlinPomAttributes(project, "Kotlin Test Common") } create("annotationsCommon", MavenPublication::class) { artifactId = "kotlin-test-annotations-common" from(annotationsMetadataComponent) artifact(tasks.getByPath(":kotlin-test:kotlin-test-annotations-common:sourcesJar") as Jar) + configureKotlinPomAttributes(project, "Kotlin Test Common") + } + withType { + suppressAllPomMetadataWarnings() + artifact(emptyJavadocJar) } } } - -tasks { - val install by creating { - dependsOn(publishToMavenLocal) - } -} \ No newline at end of file