Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed HOCON publication #2723

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ plugins {
id("org.jetbrains.kotlinx.binary-compatibility-validator")
id("org.jetbrains.dokka")
id("benchmark-conventions")
id("publishing-check-conventions")

alias(libs.plugins.serialization) apply false
}
Expand Down Expand Up @@ -111,23 +112,33 @@ subprojects {
apply(plugin = "publishing-conventions")
}

// == publishing setup ==

val mergeProject = project

subprojects {
if (name in unpublishedProjects) return@subprojects
apply(plugin = "publishing-conventions")
mergeProject.dependencies.add(Publishing_check_conventions_gradle.TestPublishing.configurationName, this)
}

// == animalsniffer setup ==
subprojects {
// Can't be applied to BOM
if (excludedFromBomProjects.contains(project.name)) return@subprojects
if (project.name in excludedFromBomProjects) return@subprojects
apply(plugin = "animalsniffer-conventions")
}

// == BOM setup ==
subprojects {
// Can't be applied to BOM
if (excludedFromBomProjects.contains(project.name)) return@subprojects
if (project.name in excludedFromBomProjects) return@subprojects
apply(plugin = "bom-conventions")
}

// == Kover setup ==
subprojects {
if (uncoveredProjects.contains(project.name)) return@subprojects
if (project.name in uncoveredProjects) return@subprojects
apply(plugin = "kover-conventions")
}

Expand Down
65 changes: 65 additions & 0 deletions buildSrc/src/main/kotlin/publishing-check-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
object TestPublishing {
const val configurationName = "testRepository"
}

val testRepositoryDependency = configurations.create(TestPublishing.configurationName) {
isVisible = true
isCanBeResolved = false
isCanBeConsumed = false
}


val testRepositories = configurations.create("testRepositories") {
isVisible = false
isCanBeResolved = true
// this config consumes modules from OTHER projects, and cannot be consumed by other projects
isCanBeConsumed = false

attributes {
attribute(Attribute.of("kotlinx.serialization.repository", String::class.java), "test")
}
extendsFrom(testRepositoryDependency)
}

tasks.register<ArtifactsCheckTask>("checkArtifacts") {
repositories.from(testRepositories)
}

abstract class ArtifactsCheckTask: DefaultTask() {

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val repositories: ConfigurableFileCollection

@TaskAction
fun check() {
val artifactsFile = project.rootDir.resolve("gradle/artifacts.txt")

val actualArtifacts = repositories.files.flatMap { file ->
file.resolve("org/jetbrains/kotlinx").list()?.toSet() ?: emptySet()
}.toSortedSet()

if (project.hasProperty("dumpArtifacts")) {
artifactsFile.bufferedWriter().use { writer ->
actualArtifacts.forEach { artifact -> writer.appendLine(artifact) }
}
return
}

val expectedArtifacts = artifactsFile.readLines().toSet()

if (expectedArtifacts == actualArtifacts) {
logger.lifecycle("All artifacts are published")
} else {
val missedArtifacts = expectedArtifacts - actualArtifacts
val unknownArtifacts = actualArtifacts - expectedArtifacts
val message = "The published artifacts differ from the expected ones." +
(if (missedArtifacts.isNotEmpty()) missedArtifacts.joinToString(prefix = "\n\tMissing artifacts: ") else "") +
(if (unknownArtifacts.isNotEmpty()) unknownArtifacts.joinToString(prefix = "\n\tUnknown artifacts: ") else "") +
"\nTo save current list of artifacts as expecting, call 'checkArtifacts -PdumpArtifacts'"

logger.error(message)
throw GradleException("The published artifacts differ from the expected ones")
}
}
}
73 changes: 53 additions & 20 deletions buildSrc/src/main/kotlin/publishing-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ afterEvaluate {

publishing {
if (!isMultiplatform && !isBom) {
publications.withType<MavenPublication>().all {
publications.register<MavenPublication>("maven") {
artifactId = project.name
from(components["java"])
artifact(mainSourcesJar)
artifact(tasks.named("stubJavadoc"))
}
} else {
// Rename artifacts for backward compatibility
publications.withType(MavenPublication::class).all {
publications.withType<MavenPublication>().configureEach {
val type = name
logger.info("Configuring $type")
when (type) {
Expand All @@ -81,16 +81,49 @@ afterEvaluate {
}
}

publications.withType(MavenPublication::class).all {
pom.configureMavenCentralMetadata(project)
signPublicationIfKeyPresent(project, this)
publications.withType<MavenPublication>().configureEach {
pom.configureMavenCentralMetadata()
signPublicationIfKeyPresent()
}
}
}

val testRepositoryDir = project.layout.buildDirectory.dir("testRepository")

publishing {
repositories {
configureMavenPublication(this, project)
addSonatypeRepository()

/**
* Maven repository in build directory to check published artifacts.
*/
maven {
setUrl(testRepositoryDir)
name = "test"
}
}
}

interface LocalArtifactAttr : Named {
companion object {
val ATTRIBUTE = Attribute.of(
"kotlinx.kover.gradle-plugin",
LocalArtifactAttr::class.java
)
}
}

val testPublicationTask: TaskCollection<*> = tasks.named { name -> name == "publishAllPublicationsToTestRepository" }
configurations.register("testPublication") {
isVisible = false
isCanBeResolved = false
// this configuration produces modules that can be consumed by other projects
isCanBeConsumed = true
attributes {
attribute(Attribute.of("kotlinx.serialization.repository", String::class.java), "test")
}
outgoing.artifact(testRepositoryDir) {
builtBy(testPublicationTask)
}
}

Expand Down Expand Up @@ -119,7 +152,7 @@ tasks.register("bintrayUpload") {
dependsOn(tasks.publishToMavenLocal)
}

fun MavenPom.configureMavenCentralMetadata(project: Project) {
fun MavenPom.configureMavenCentralMetadata() {
name = project.name
description = "Kotlin multiplatform serialization runtime library"
url = "https://github.com/Kotlin/kotlinx.serialization"
Expand Down Expand Up @@ -158,7 +191,7 @@ fun MavenPom.configureMavenCentralMetadata(project: Project) {
*/
public fun Project.reconfigureMultiplatformPublication(jvmPublication: MavenPublication) {
val mavenPublications =
extensions.getByType(PublishingExtension::class.java).publications.withType<MavenPublication>()
extensions.getByType<PublishingExtension>().publications.withType<MavenPublication>()
val kmpPublication = mavenPublications.getByName("kotlinMultiplatform")

var jvmPublicationXml: XmlProvider? = null
Expand Down Expand Up @@ -194,24 +227,24 @@ public fun Project.reconfigureMultiplatformPublication(jvmPublication: MavenPubl
}
}

fun signPublicationIfKeyPresent(project: Project, publication: MavenPublication) {
val keyId = project.getSensitiveProperty("libs.sign.key.id")
val signingKey = project.getSensitiveProperty("libs.sign.key.private")
val signingKeyPassphrase = project.getSensitiveProperty("libs.sign.passphrase")
fun MavenPublication.signPublicationIfKeyPresent() {
val keyId = getSensitiveProperty("libs.sign.key.id")
val signingKey = getSensitiveProperty("libs.sign.key.private")
val signingKeyPassphrase = getSensitiveProperty("libs.sign.passphrase")
if (!signingKey.isNullOrBlank()) {
project.extensions.configure<SigningExtension>("signing") {
extensions.configure<SigningExtension>("signing") {
useInMemoryPgpKeys(keyId, signingKey, signingKeyPassphrase)
sign(publication)
sign(this@signPublicationIfKeyPresent)
}
}
}

fun configureMavenPublication(rh: RepositoryHandler, project: Project) {
rh.maven {
fun RepositoryHandler.addSonatypeRepository() {
maven {
url = mavenRepositoryUri()
credentials {
username = project.getSensitiveProperty("libs.sonatype.user")
password = project.getSensitiveProperty("libs.sonatype.password")
username = getSensitiveProperty("libs.sonatype.user")
password = getSensitiveProperty("libs.sonatype.password")
}
}
}
Expand All @@ -226,6 +259,6 @@ fun mavenRepositoryUri(): URI {
}
}

fun Project.getSensitiveProperty(name: String): String? {
return project.findProperty(name) as? String ?: System.getenv(name)
fun getSensitiveProperty(name: String): String? {
return findProperty(name) as? String ?: System.getenv(name)
}
Loading