Skip to content

Commit f076c6d

Browse files
build: Switch to maven publish plugin for publishing (#96)
* Update to Gradle 8.7 * Switch to develocity * Switch to maven publish plugin for publishing * Update CI * Doesn't work with CC yet * Remove comments * test: update test task dependency * fix: get publishing working. --------- Co-authored-by: Tony Robalik <trobalik@squareup.com>
1 parent 5e9c969 commit f076c6d

25 files changed

+78
-438
lines changed

.github/workflows/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
- name: Publish artifacts
3737
uses: gradle/gradle-build-action@v2
3838
with:
39-
arguments: publishToMavenCentral -s
39+
arguments: publishAllPublicationsToMavenCentralRepository --no-configuration-cache
4040
env:
4141
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
4242
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_NEXUS_PASSWORD }}

app/build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
22

33
plugins {
44
id 'com.squareup.app'
5+
alias(libs.plugins.mavenPublish)
56
}
67

78
application {
@@ -54,10 +55,6 @@ publishing {
5455
artifactId = 'sort-gradle-dependencies-dist'
5556
artifact shadowDistZip
5657
}
57-
app(MavenPublication) {
58-
artifactId = 'sort-gradle-dependencies-app'
59-
from components['java']
60-
}
6158
}
6259
repositories {
6360
maven {

app/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
POM_ARTIFACT_ID=sort-gradle-dependencies-app
2+
POM_NAME=Gradle Dependencies Sorter
3+
POM_DESCRIPTION=Sorts Gradle dependencies

build-logic/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ gradlePlugin {
2323
dependencies {
2424
implementation libs.kotlin.gradle.plugin
2525
implementation libs.shadow.gradle.plugin
26+
implementation libs.maven.publish.plugin
2627

2728
implementation(libs.okhttp3) {
2829
because('Closing and releasing Sonatype Nexus staging repo')

build-logic/src/main/kotlin/com/squareup/convention/BaseConvention.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ class BaseConvention : Plugin<Project> {
1414
with(pluginManager) {
1515
apply("org.jetbrains.kotlin.jvm")
1616
apply("groovy")
17-
apply("maven-publish")
18-
apply("org.gradle.signing")
1917
}
2018

2119
// These are set in the base project's gradle.properties
2220
group = providers.gradleProperty("GROUP").get()
23-
version = providers.gradleProperty("VERSION").get()
21+
version = providers.gradleProperty("VERSION_NAME").get()
2422

2523
val versionCatalog = extensions.getByType(VersionCatalogsExtension::class.java).named("libs")
2624
val javaVersion = versionCatalog.findVersion("java").orElseThrow().requiredVersion
@@ -55,4 +53,4 @@ class BaseConvention : Plugin<Project> {
5553
add("testImplementation", versionCatalog.findLibrary("truth").orElseThrow())
5654
}
5755
}
58-
}
56+
}

build-logic/src/main/kotlin/com/squareup/convention/LibConvention.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,5 @@ class LibConvention : Plugin<Project> {
1616

1717
override fun apply(target: Project): Unit = target.run {
1818
pluginManager.apply(BaseConvention::class.java)
19-
20-
extensions.configure(JavaPluginExtension::class.java) { j ->
21-
// We specifically don't want either of these artifacts on the app project
22-
j.withJavadocJar()
23-
j.withSourcesJar()
24-
}
2519
}
2620
}

build-logic/src/main/kotlin/com/squareup/convention/PluginConvention.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ class PluginConvention : Plugin<Project> {
3131

3232
Publishing.setup(this)
3333
}
34-
}
34+
}
Lines changed: 3 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,21 @@
11
package com.squareup.convention.publishing
22

3-
import nexus.Credentials
4-
import nexus.NexusPublishTask
3+
import com.vanniktech.maven.publish.MavenPublishBaseExtension
54
import org.gradle.api.Project
6-
import org.gradle.api.publish.PublishingExtension
75
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
126

13-
@Suppress("UnstableApiUsage")
147
internal object Publishing {
158

169
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)
10612
}
10713
}
10814

10915
private fun setupPom(pom: MavenPom): Unit = pom.run {
11016
name.set("Gradle Dependencies Sorter")
11117
description.set("Sorts Gradle dependencies")
112-
url.set("https://github.com/square/gradle-dependencies-sorter")
113-
inceptionYear.set("2022")
11418

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-
}
12119
developers {
12220
it.developer { d ->
12321
d.id.set("autonomousapps")
@@ -132,10 +30,5 @@ internal object Publishing {
13230
d.name.set("Jason Holmes")
13331
}
13432
}
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-
}
14033
}
14134
}

build-logic/src/main/kotlin/nexus/Credentials.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

build-logic/src/main/kotlin/nexus/Nexus.kt

Lines changed: 0 additions & 157 deletions
This file was deleted.

0 commit comments

Comments
 (0)