1
1
package com.squareup.convention.publishing
2
2
3
- import nexus.Credentials
4
- import nexus.NexusPublishTask
3
+ import com.vanniktech.maven.publish.MavenPublishBaseExtension
5
4
import org.gradle.api.Project
6
- import org.gradle.api.publish.PublishingExtension
7
5
import org.gradle.api.publish.maven.MavenPom
8
- import org.gradle.api.publish.maven.MavenPublication
9
- import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
10
- import org.gradle.plugins.signing.Sign
11
- import org.gradle.plugins.signing.SigningExtension
12
6
13
- @Suppress(" UnstableApiUsage" )
14
7
internal object Publishing {
15
8
16
9
fun setup (project : Project ): Unit = project.run {
17
- val isSnapshot = version.toString().endsWith(" SNAPSHOT" )
18
- val isRelease = ! isSnapshot
19
- val publishing = extensions.getByType(PublishingExtension ::class .java)
20
- val signing = extensions.getByType(SigningExtension ::class .java)
21
- val credentials = Credentials (this )
22
-
23
- publishing.repositories { r ->
24
- if (credentials.isValid()) {
25
- r.maven { a ->
26
- a.name = " sonatype"
27
-
28
- val releasesRepoUrl = " https://s01.oss.sonatype.org/service/local/staging/deploy/maven2"
29
- val snapshotsRepoUrl = " https://s01.oss.sonatype.org/content/repositories/snapshots"
30
- a.url = project.uri(if (isSnapshot) snapshotsRepoUrl else releasesRepoUrl)
31
-
32
- a.credentials {
33
- it.username = credentials.username()
34
- it.password = credentials.password()
35
- }
36
- }
37
- }
38
- }
39
-
40
- afterEvaluate {
41
- publishing.publications.all { pub ->
42
- // https://github.com/vanniktech/gradle-maven-publish-plugin/blob/462c3b13579929a1ca92343099659866f8081600/plugin/src/main/kotlin/com/vanniktech/maven/publish/MavenPublishBaseExtension.kt#L141C42-L147
43
- val key = providers.gradleProperty(" signingInMemoryKey" ).orNull
44
- if (key != null ) {
45
- val keyId = providers.gradleProperty(" signingInMemoryKeyId" ).orNull
46
- val pw = providers.gradleProperty(" signingInMemoryKeyPassword" ).getOrElse(" " )
47
- signing.useInMemoryPgpKeys(keyId, key, pw)
48
- }
49
-
50
- signing.sign(pub)
51
-
52
- signing.isRequired = isRelease && gradle.taskGraph.allTasks.any { it is PublishToMavenRepository }
53
-
54
- if (pub is MavenPublication ) {
55
- setupPom(pub.pom)
56
- }
57
- }
58
- }
59
-
60
- val promoteTask = tasks.register(" promote" , NexusPublishTask ::class .java) {
61
- with (it) {
62
- // only promote non-snapshots
63
- onlyIf { ! isSnapshot }
64
- inputs.property(" version" , version)
65
- configureWith(credentials)
66
- }
67
- }
68
-
69
- tasks.withType(Sign ::class .java).configureEach {
70
- with (it) {
71
- notCompatibleWithConfigurationCache(" https://github.com/gradle/gradle/issues/13470" )
72
- // only sign non-snapshots
73
- onlyIf { ! isSnapshot }
74
- inputs.property(" version" , version)
75
- }
76
- }
77
-
78
- tasks.withType(PublishToMavenRepository ::class .java).configureEach {
79
- with (it) {
80
- notCompatibleWithConfigurationCache(" https://github.com/gradle/gradle/issues/13470" )
81
-
82
- // Releases require the checks to pass
83
- if (! isSnapshot) {
84
- dependsOn(" check" )
85
- }
86
- }
87
- }
88
-
89
- tasks.register(" publishToMavenCentral" ) {
90
- with (it) {
91
- notCompatibleWithConfigurationCache(" Publishing is not compatible" )
92
- finalizedBy(promoteTask)
93
- dependsOn(tasks.withType(PublishToMavenRepository ::class .java))
94
-
95
- group = " publishing"
96
- description = " Publishes final artifacts to Maven Central"
97
-
98
- doLast {
99
- if (isSnapshot) {
100
- logger.quiet(" Browse files at https://s01.oss.sonatype.org/content/repositories/snapshots/com/squareup/" )
101
- } else {
102
- logger.quiet(" If 'promote' task failed: after publishing to Sonatype, visit https://s01.oss.sonatype.org to close and release from staging" )
103
- }
104
- }
105
- }
10
+ project.pluginManager.withPlugin(" com.vanniktech.maven.publish" ) {
11
+ extensions.getByType(MavenPublishBaseExtension ::class .java).pom(::setupPom)
106
12
}
107
13
}
108
14
109
15
private fun setupPom (pom : MavenPom ): Unit = pom.run {
110
16
name.set(" Gradle Dependencies Sorter" )
111
17
description.set(" Sorts Gradle dependencies" )
112
- url.set(" https://github.com/square/gradle-dependencies-sorter" )
113
- inceptionYear.set(" 2022" )
114
18
115
- licenses {
116
- it.license { l ->
117
- l.name.set(" The Apache License, Version 2.0" )
118
- l.url.set(" https://www.apache.org/licenses/LICENSE-2.0.txt" )
119
- }
120
- }
121
19
developers {
122
20
it.developer { d ->
123
21
d.id.set(" autonomousapps" )
@@ -132,10 +30,5 @@ internal object Publishing {
132
30
d.name.set(" Jason Holmes" )
133
31
}
134
32
}
135
- scm {
136
- it.connection.set(" scm:git:git://github.com/square/gradle-dependencies-sorter.git" )
137
- it.developerConnection.set(" scm:git:ssh://github.com/square/gradle-dependencies-sorter.git" )
138
- it.url.set(" https://github.com/square/gradle-dependencies-sorter" )
139
- }
140
33
}
141
34
}
0 commit comments