1
+ package io.github.petertrr
2
+
3
+ import org.gradle.api.Project
4
+ import io.github.gradlenexus.publishplugin.NexusPublishPlugin
5
+ import io.github.gradlenexus.publishplugin.NexusPublishExtension
6
+ import org.gradle.api.publish.PublishingExtension
7
+ import org.gradle.api.publish.maven.MavenPublication
8
+ import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
9
+ import org.gradle.api.publish.maven.tasks.AbstractPublishToMaven
10
+ import org.gradle.api.tasks.bundling.Jar
11
+ import org.gradle.kotlin.dsl.*
12
+ import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform
13
+ import org.gradle.plugins.signing.SigningExtension
14
+ import org.gradle.plugins.signing.SigningPlugin
15
+
16
+ fun Project.configurePublishing () {
17
+ apply<MavenPublishPlugin >()
18
+ apply<SigningPlugin >()
19
+ apply<NexusPublishPlugin >()
20
+
21
+ // If present, set properties from env variables. If any are absent, release will fail.
22
+ System .getenv(" OSSRH_USERNAME" )?.let {
23
+ extra.set(" sonatypeUsername" , it)
24
+ }
25
+ System .getenv(" OSSRH_PASSWORD" )?.let {
26
+ extra.set(" sonatypePassword" , it)
27
+ }
28
+ System .getenv(" GPG_SEC" )?.let {
29
+ extra.set(" signingKey" , it)
30
+ }
31
+ System .getenv(" GPG_PASSWORD" )?.let {
32
+ extra.set(" signingPassword" , it)
33
+ }
34
+
35
+ configurePublications()
36
+ // https://kotlinlang.org/docs/mpp-publish-lib.html#avoid-duplicate-publications
37
+ val publicationsFromMainHost = listOf (" jvm" , " js" , " kotlinMultiplatform" )
38
+ configure<PublishingExtension > {
39
+ publications {
40
+ matching { it.name in publicationsFromMainHost }.all {
41
+ val targetPublication = this @all
42
+ tasks.withType<AbstractPublishToMaven >()
43
+ .matching { it.publication == targetPublication }
44
+ .configureEach {
45
+ onlyIf {
46
+ // main publishing CI job is executed on Linux host
47
+ DefaultNativePlatform .getCurrentOperatingSystem().isLinux
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ if (hasProperty(" signingKey" )) {
55
+ configureSigning()
56
+ }
57
+ if (hasProperty(" sonatypeUsername" )) {
58
+ configureNexusPublishing()
59
+ }
60
+ }
61
+
62
+ private fun Project.configurePublications () {
63
+ val dokkaJar = tasks.create<Jar >(" dokkaJar" ) {
64
+ group = " documentation"
65
+ archiveClassifier.set(" javadoc" )
66
+ from(tasks.findByName(" dokkaHtml" ))
67
+ }
68
+ configure<PublishingExtension > {
69
+ repositories {
70
+ mavenLocal()
71
+ }
72
+ publications.withType<MavenPublication >().forEach { publication ->
73
+ publication.artifact(dokkaJar)
74
+ publication.pom {
75
+ name.set(project.name)
76
+ description.set(project.description ? : project.name)
77
+ url.set(" https://github.com/petertrr/kotlin-multiplatform-diff" )
78
+ licenses {
79
+ license {
80
+ name.set(" The Apache Software License, Version 2.0" )
81
+ url.set(" http://www.apache.org/licenses/LICENSE-2.0.txt" )
82
+ distribution.set(" repo" )
83
+ }
84
+ }
85
+ developers {
86
+ developer {
87
+ id.set(" petertrr" )
88
+ name.set(" Petr Trifanov" )
89
+
90
+ }
91
+ }
92
+ scm {
93
+ url.set(" https://github.com/petertrr/kotlin-multiplatform-diff" )
94
+ connection.set(" scm:git:git://github.com/petertrr/kotlin-multiplatform-diff.git" )
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ private fun Project.configureSigning () {
102
+ configure<SigningExtension > {
103
+ useInMemoryPgpKeys(property(" signingKey" ) as String? , property(" signingPassword" ) as String? )
104
+ logger.lifecycle(" The following publications are getting signed: ${extensions.getByType<PublishingExtension >().publications.map { it.name }} " )
105
+ sign(* extensions.getByType<PublishingExtension >().publications.toTypedArray())
106
+ }
107
+ }
108
+
109
+ private fun Project.configureNexusPublishing () {
110
+ configure<NexusPublishExtension > {
111
+ repositories {
112
+ sonatype { // only for users registered in Sonatype after 24 Feb 2021
113
+ nexusUrl.set(uri(" https://s01.oss.sonatype.org/service/local/" ))
114
+ snapshotRepositoryUrl.set(uri(" https://s01.oss.sonatype.org/content/repositories/snapshots/" ))
115
+ username.set(property(" sonatypeUsername" ) as String )
116
+ password.set(property(" sonatypePassword" ) as String )
117
+ }
118
+ }
119
+ }
120
+ }
0 commit comments