xref: /aosp_15_r20/external/kotlinx.serialization/buildSrc/src/main/kotlin/Publishing.kt (revision 57b5a4a64c534cf7f27ac9427ceab07f3d8ed3d8)
1 /*
2  * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 @file:Suppress("UnstableApiUsage")
6 
7 import org.gradle.api.*
8 import org.gradle.api.artifacts.dsl.*
9 import org.gradle.api.provider.*
10 import org.gradle.api.publish.maven.*
11 import org.gradle.plugins.signing.*
12 import java.net.*
13 
14 // Pom configuration
15 
bynull16 infix fun <T> Property<T>.by(value: T) {
17     set(value)
18 }
19 
configureMavenCentralMetadatanull20 fun MavenPom.configureMavenCentralMetadata(project: Project) {
21     name by project.name
22     description by "Kotlin multiplatform serialization runtime library"
23     url by "https://github.com/Kotlin/kotlinx.serialization"
24 
25     licenses {
26         license {
27             name by "The Apache Software License, Version 2.0"
28             url by "https://www.apache.org/licenses/LICENSE-2.0.txt"
29             distribution by "repo"
30         }
31     }
32 
33     developers {
34         developer {
35             id by "JetBrains"
36             name by "JetBrains Team"
37             organization by "JetBrains"
38             organizationUrl by "https://www.jetbrains.com"
39         }
40     }
41 
42     scm {
43         url by "https://github.com/Kotlin/kotlinx.serialization"
44     }
45 }
46 
mavenRepositoryUrinull47 fun mavenRepositoryUri(): URI {
48     // TODO -SNAPSHOT detection can be made here as well
49     val repositoryId: String? = System.getenv("libs.repository.id")
50     return if (repositoryId == null) {
51         URI("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
52     } else {
53         URI("https://oss.sonatype.org/service/local/staging/deployByRepositoryId/$repositoryId")
54     }
55 }
56 
configureMavenPublicationnull57 fun configureMavenPublication(rh: RepositoryHandler, project: Project) {
58     rh.maven {
59         url = mavenRepositoryUri()
60         credentials {
61             username = project.getSensitiveProperty("libs.sonatype.user")
62             password = project.getSensitiveProperty("libs.sonatype.password")
63         }
64     }
65 }
66 
signPublicationIfKeyPresentnull67 fun signPublicationIfKeyPresent(project: Project, publication: MavenPublication) {
68     val keyId = project.getSensitiveProperty("libs.sign.key.id")
69     val signingKey = project.getSensitiveProperty("libs.sign.key.private")
70     val signingKeyPassphrase = project.getSensitiveProperty("libs.sign.passphrase")
71     if (!signingKey.isNullOrBlank()) {
72         project.extensions.configure<SigningExtension>("signing") {
73             useInMemoryPgpKeys(keyId, signingKey, signingKeyPassphrase)
74             sign(publication)
75         }
76     }
77 }
78 
getSensitivePropertynull79 private fun Project.getSensitiveProperty(name: String): String? {
80     return project.findProperty(name) as? String ?: System.getenv(name)
81 }
82