|
1 | 1 | import java.io.File
|
| 2 | +import java.util.* |
2 | 3 |
|
3 | 4 | plugins {
|
4 | 5 | // Apply the application plugin to add support for building a CLI application in Java.
|
5 | 6 | id("buildlogic.java-library-conventions")
|
6 | 7 | id("maven-publish")
|
| 8 | + signing |
7 | 9 | }
|
8 | 10 |
|
9 | 11 | repositories {
|
@@ -102,12 +104,32 @@ publishing {
|
102 | 104 | repositories {
|
103 | 105 | // Publish to GitHub Packages
|
104 | 106 | // https://docs.github.com/en/actions/use-cases-and-examples/publishing-packages/publishing-java-packages-with-gradle
|
105 |
| - maven { |
106 |
| - name = "GitHubPackages" |
107 |
| - url = uri("https://maven.pkg.github.com/briancorbinxyz/overengineering-tictactoe") |
108 |
| - credentials { |
109 |
| - username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR") |
110 |
| - password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") |
| 107 | + val targetRepo: String? = findProperty("repo") as String? |
| 108 | + if (targetRepo == null || targetRepo == "Sonatype") { |
| 109 | + maven { |
| 110 | + name = "Sonatype" |
| 111 | + url = uri( |
| 112 | + if (version.toString().endsWith("SNAPSHOT")) |
| 113 | + //"https://s01.oss.sonatype.org/content/repositories/snapshots/" |
| 114 | + "https://central.sonatype.com/repository/maven-snapshots/" |
| 115 | + else |
| 116 | + // "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" |
| 117 | + "https://ossrh-staging-api.central.sonatype.com/service/local/staging/deploy/maven2" |
| 118 | + ) |
| 119 | + credentials { |
| 120 | + username = project.findProperty("sonatype.user") as String? ?: System.getenv("SONATYPE_USER") |
| 121 | + password = project.findProperty("sonatype.key") as String? ?: System.getenv("SONATYPE_TOKEN") |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + if (targetRepo == null || targetRepo == "GitHubPackages") { |
| 126 | + maven { |
| 127 | + name = "GitHubPackages" |
| 128 | + url = uri("https://maven.pkg.github.com/briancorbinxyz/overengineering-tictactoe") |
| 129 | + credentials { |
| 130 | + username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR") |
| 131 | + password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") |
| 132 | + } |
111 | 133 | }
|
112 | 134 | }
|
113 | 135 | }
|
@@ -142,3 +164,35 @@ publishing {
|
142 | 164 | }
|
143 | 165 | }
|
144 | 166 | }
|
| 167 | +fun decodeKey(raw: String): String = |
| 168 | + if (raw.contains("-----BEGIN PGP PRIVATE KEY BLOCK-----")) { |
| 169 | + raw |
| 170 | + } else { |
| 171 | + String(Base64.getDecoder().decode(raw)) |
| 172 | + } |
| 173 | + |
| 174 | +val rawSigningKey = System.getenv("SIGNING_KEY") ?: findProperty("signing.key") as String? |
| 175 | +val signingKey = rawSigningKey?.let(::decodeKey) |
| 176 | + |
| 177 | +val signingPassword = System.getenv("SIGNING_PASSWORD") ?: findProperty("signing.password") as String? |
| 178 | +val signingKeyId = System.getenv("SIGNING_KEY_ID") ?: findProperty("signing.keyId") as String? |
| 179 | + |
| 180 | +val isPublishing = gradle.startParameter.taskNames.any { it.contains("publish", ignoreCase = true) } |
| 181 | + |
| 182 | +val shouldSign = signingKey != null && signingPassword != null |
| 183 | + |
| 184 | +logger.lifecycle("🔐 Signing check:") |
| 185 | +logger.lifecycle(" • isPublishing: $isPublishing") |
| 186 | +logger.lifecycle(" • signingKeyId: ${signingKeyId != null}") |
| 187 | +logger.lifecycle(" • signingKey present: ${signingKey != null}") |
| 188 | +logger.lifecycle(" • signingPassword present: ${signingPassword != null}") |
| 189 | +logger.lifecycle(" • shouldSign: $shouldSign") |
| 190 | +if (isPublishing && shouldSign) { |
| 191 | + signing { |
| 192 | + useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword) |
| 193 | + sign(configurations.runtimeElements.get()) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | + |
0 commit comments