|
| 1 | +// Shared Maven Central publishing configuration for Acorn |
| 2 | +// Applies to subprojects that define `groupId` and `artifactId` in their gradle.properties. |
| 3 | + |
| 4 | +ext.isAndroidLibrary = { project.plugins.hasPlugin("com.android.library") } |
| 5 | +ext.isJvmLibrary = { project.plugins.hasPlugin("org.gradle.java-library") || project.plugins.hasPlugin("org.jetbrains.kotlin.jvm") } |
| 6 | + |
| 7 | +// Apply plugins if not already applied |
| 8 | +plugins.withId("org.gradle.maven-publish") {} |
| 9 | +if (!plugins.hasPlugin("org.gradle.maven-publish")) { |
| 10 | + apply plugin: "maven-publish" |
| 11 | +} |
| 12 | +if (!plugins.hasPlugin("signing")) { |
| 13 | + apply plugin: "signing" |
| 14 | +} |
| 15 | + |
| 16 | +// Set group and artifact from module gradle.properties |
| 17 | +if (project.hasProperty("groupId")) { |
| 18 | + group = project.property("groupId") |
| 19 | +} |
| 20 | + |
| 21 | +// Ensure sources and javadoc artifacts exist |
| 22 | +if (isJvmLibrary()) { |
| 23 | + // Ensure Java convention is present to register sources/javadoc jars |
| 24 | + plugins.withId("org.gradle.java") { |
| 25 | + java { |
| 26 | + withSourcesJar() |
| 27 | + withJavadocJar() |
| 28 | + } |
| 29 | + } |
| 30 | + plugins.withId("org.gradle.java-library") { |
| 31 | + java { |
| 32 | + withSourcesJar() |
| 33 | + withJavadocJar() |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// For Android libraries, use AGP's publishing DSL to configure the release variant with sources |
| 39 | +plugins.withId("com.android.library") { |
| 40 | + android { |
| 41 | + publishing { |
| 42 | + singleVariant("release") { |
| 43 | + withSourcesJar() |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Provide a minimal javadoc jar to satisfy Maven Central requirements |
| 49 | + // This creates an empty javadoc jar, which is acceptable for Android/Kotlin libraries |
| 50 | + tasks.register("androidJavadocJar", Jar) { |
| 51 | + archiveClassifier.set("javadoc") |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +publishing { |
| 56 | + publications { |
| 57 | + def pub = publications.findByName("maven") as MavenPublication |
| 58 | + if (pub == null) { |
| 59 | + maven(MavenPublication) { |
| 60 | + if (isAndroidLibrary()) { |
| 61 | + // Publish the release variant for Android libraries |
| 62 | + afterEvaluate { |
| 63 | + from components.findByName("release") |
| 64 | + } |
| 65 | + // Sources are added via AGP publishing DSL (singleVariant + withSourcesJar) |
| 66 | + // Provide a minimal javadoc jar as required by Maven Central |
| 67 | + artifact tasks.named("androidJavadocJar") |
| 68 | + } else { |
| 69 | + // JVM libraries |
| 70 | + if (components.findByName("java") != null) { |
| 71 | + from components.java |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (project.hasProperty("artifactId")) { |
| 76 | + artifactId = project.property("artifactId") |
| 77 | + } |
| 78 | + |
| 79 | + pom { |
| 80 | + name = "Acorn" |
| 81 | + description = "Mastering Android Navigation" |
| 82 | + url = "https://github.com/nhaarman/Acorn" |
| 83 | + |
| 84 | + licenses { |
| 85 | + license { |
| 86 | + name = "The Apache License, Version 2.0" |
| 87 | + url = "http://www.apache.org/licenses/LICENSE-2.0.txt" |
| 88 | + distribution = "repo" |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + developers { |
| 93 | + developer { |
| 94 | + id = "nhaarman" |
| 95 | + name = "Niek Haarman" |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + scm { |
| 100 | + url = "https://github.com/nhaarman/Acorn" |
| 101 | + connection = "scm:git:git://github.com/nhaarman/Acorn.git" |
| 102 | + developerConnection = "scm:git:ssh://github.com/nhaarman/Acorn.git" |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + repositories { |
| 110 | + // Prefer Central Publishing Portal if configured; otherwise fall back to OSSRH |
| 111 | + def isSnapshot = project.version?.toString()?.endsWith("-SNAPSHOT") |
| 112 | + def useCentral = (findProperty("centralPortal")?.toString()?.toBoolean() ?: false) |
| 113 | + || (System.getenv("CENTRAL_PORTAL")?.toBoolean() ?: false) |
| 114 | + |
| 115 | + if (useCentral) { |
| 116 | + if (isSnapshot) { |
| 117 | + throw new GradleException("Central Publishing Portal does not accept SNAPSHOT versions. Remove -SNAPSHOT or disable centralPortal.") |
| 118 | + } |
| 119 | + maven { |
| 120 | + name = "CentralPortal" |
| 121 | + def centralUrl = findProperty("centralPortalUrl") ?: System.getenv("CENTRAL_PORTAL_URL") |
| 122 | + if (!centralUrl) { |
| 123 | + // Default upload endpoint for Central Portal (token-based) |
| 124 | + centralUrl = "https://central.sonatype.com/api/v1/publisher/deploy/maven2" |
| 125 | + } |
| 126 | + url = uri(centralUrl) |
| 127 | + credentials { |
| 128 | + username = findProperty("centralPortalUsername") ?: System.getenv("CENTRAL_PORTAL_USERNAME") |
| 129 | + password = findProperty("centralPortalPassword") ?: System.getenv("CENTRAL_PORTAL_PASSWORD") |
| 130 | + } |
| 131 | + } |
| 132 | + } else { |
| 133 | + // Legacy OSSRH repositories for releases and snapshots |
| 134 | + maven { |
| 135 | + name = "OSSRH" |
| 136 | + def releasesUrl = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/") |
| 137 | + def snapshotsUrl = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") |
| 138 | + url = isSnapshot ? snapshotsUrl : releasesUrl |
| 139 | + |
| 140 | + credentials { |
| 141 | + // Support multiple property names and environment variables |
| 142 | + username = findProperty("ossrhUsername") ?: findProperty("mavenCentralUsername") |
| 143 | + ?: System.getenv("OSSRH_USERNAME") ?: System.getenv("MAVEN_CENTRAL_USERNAME") |
| 144 | + password = findProperty("ossrhPassword") ?: findProperty("mavenCentralPassword") |
| 145 | + ?: System.getenv("OSSRH_PASSWORD") ?: System.getenv("MAVEN_CENTRAL_PASSWORD") |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Configure signing (only if keys are available) |
| 153 | +signing { |
| 154 | + def signingKey = findProperty("signingKey") ?: System.getenv("SIGNING_KEY") |
| 155 | + def signingPassword = findProperty("signingPassword") ?: System.getenv("SIGNING_PASSWORD") |
| 156 | + |
| 157 | + if (signingKey && signingPassword) { |
| 158 | + useInMemoryPgpKeys(signingKey, signingPassword) |
| 159 | + sign publishing.publications |
| 160 | + } else if (findProperty("signing.secretKeyRingFile") || findProperty("signing.keyId")) { |
| 161 | + // Fall back to Gradle's default signing properties if configured via file-based keys |
| 162 | + sign publishing.publications |
| 163 | + } else { |
| 164 | + logger.lifecycle("[publishing] Signing is not configured; publications will not be signed.") |
| 165 | + } |
| 166 | +} |
0 commit comments