|
1 | 1 | package mill.javalib |
2 | 2 |
|
3 | 3 | import com.lihaoyi.unroll |
4 | | -import com.lumidion.sonatype.central.client.core.PublishingType |
5 | | -import com.lumidion.sonatype.central.client.core.SonatypeCredentials |
| 4 | +import com.lumidion.sonatype.central.client.core.{PublishingType, SonatypeCredentials} |
6 | 5 | import mill.* |
7 | | -import mill.api.BuildCtx |
| 6 | +import javalib.* |
| 7 | +import mill.api.{ExternalModule, Task} |
| 8 | +import mill.util.Tasks |
8 | 9 | import mill.api.DefaultTaskModule |
9 | | -import mill.api.ExternalModule |
10 | 10 | import mill.api.Result |
11 | | -import mill.api.Task |
| 11 | +import mill.javalib.SonatypeCentralPublishModule.{ |
| 12 | + defaultAwaitTimeout, |
| 13 | + defaultConnectTimeout, |
| 14 | + defaultCredentials, |
| 15 | + defaultReadTimeout, |
| 16 | + getPublishingTypeFromReleaseFlag, |
| 17 | + getSonatypeCredentials |
| 18 | +} |
| 19 | +import mill.javalib.publish.Artifact |
| 20 | +import mill.javalib.publish.SonatypeHelpers.{PASSWORD_ENV_VARIABLE_NAME, USERNAME_ENV_VARIABLE_NAME} |
| 21 | +import mill.api.BuildCtx |
12 | 22 | import mill.api.daemon.Logger |
13 | 23 | import mill.javalib.PublishModule.PublishData |
14 | | -import mill.javalib.SonatypeCentralPublishModule.defaultAwaitTimeout |
15 | | -import mill.javalib.SonatypeCentralPublishModule.defaultConnectTimeout |
16 | | -import mill.javalib.SonatypeCentralPublishModule.defaultCredentials |
17 | | -import mill.javalib.SonatypeCentralPublishModule.defaultReadTimeout |
18 | | -import mill.javalib.SonatypeCentralPublishModule.getPublishingTypeFromReleaseFlag |
19 | 24 | import mill.javalib.internal.PublishModule.GpgArgs |
20 | | -import mill.javalib.publish.Artifact |
21 | | -import mill.javalib.publish.SonatypeHelpers.CREDENTIALS_ENV_VARIABLE_PREFIX |
22 | | -import mill.util.Tasks |
23 | 25 |
|
24 | | -import javalib.* |
25 | | - |
26 | | -trait SonatypeCentralPublishModule extends PublishModule, MavenWorkerSupport, |
27 | | - PublishCredentialsModule { |
| 26 | +trait SonatypeCentralPublishModule extends PublishModule, MavenWorkerSupport { |
28 | 27 |
|
29 | 28 | @deprecated("Use `sonatypeCentralGpgArgsForKey` instead.", "Mill 1.0.1") |
30 | 29 | def sonatypeCentralGpgArgs: T[String] = |
@@ -64,7 +63,7 @@ trait SonatypeCentralPublishModule extends PublishModule, MavenWorkerSupport, |
64 | 63 | @unroll docs: Boolean = true |
65 | 64 | ): Task.Command[Unit] = Task.Command { |
66 | 65 | val artifact = artifactMetadata() |
67 | | - val credentials = getPublishCredentials(CREDENTIALS_ENV_VARIABLE_PREFIX, username, password)() |
| 66 | + val credentials = getSonatypeCredentials(username, password)() |
68 | 67 | val publishData = publishArtifactsPayload(sources = sources, docs = docs)() |
69 | 68 | val publishingType = getPublishingTypeFromReleaseFlag(sonatypeCentralShouldRelease()) |
70 | 69 |
|
@@ -98,8 +97,8 @@ trait SonatypeCentralPublishModule extends PublishModule, MavenWorkerSupport, |
98 | 97 | /** |
99 | 98 | * External module to publish artifacts to `central.sonatype.org` |
100 | 99 | */ |
101 | | -object SonatypeCentralPublishModule extends ExternalModule, DefaultTaskModule, MavenWorkerSupport, |
102 | | - PublishCredentialsModule, MavenPublish { |
| 100 | +object SonatypeCentralPublishModule extends ExternalModule with DefaultTaskModule |
| 101 | + with MavenWorkerSupport { |
103 | 102 | private final val sonatypeCentralGpgArgsSentinelValue = "<user did not override this method>" |
104 | 103 |
|
105 | 104 | def self = this |
@@ -128,7 +127,7 @@ object SonatypeCentralPublishModule extends ExternalModule, DefaultTaskModule, M |
128 | 127 | val artifacts = Task.sequence(publishArtifacts.value)() |
129 | 128 |
|
130 | 129 | val finalBundleName = if (bundleName.isEmpty) None else Some(bundleName) |
131 | | - val credentials = getPublishCredentials(CREDENTIALS_ENV_VARIABLE_PREFIX, username, password)() |
| 130 | + val credentials = getSonatypeCredentials(username, password)() |
132 | 131 | def makeGpgArgs() = internal.PublishModule.pgpImportSecretIfProvidedAndMakeGpgArgs( |
133 | 132 | Task.env, |
134 | 133 | GpgArgs.fromUserProvided(gpgArgs) |
@@ -170,17 +169,37 @@ object SonatypeCentralPublishModule extends ExternalModule, DefaultTaskModule, M |
170 | 169 | val dryRun = env.get("MILL_TESTS_PUBLISH_DRY_RUN").contains("1") |
171 | 170 |
|
172 | 171 | def publishSnapshot(publishData: PublishData): Unit = { |
173 | | - mavenPublishData( |
174 | | - dryRun = dryRun, |
175 | | - publishData = publishData, |
176 | | - isSnapshot = true, |
177 | | - credentials = credentials, |
178 | | - releaseUri = sonatypeCentralSnapshotUri, |
179 | | - snapshotUri = sonatypeCentralSnapshotUri, |
180 | | - taskDest = taskDest, |
181 | | - log = log, |
182 | | - worker = worker |
| 172 | + val uri = sonatypeCentralSnapshotUri |
| 173 | + val artifacts = MavenWorkerSupport.RemoteM2Publisher.asM2ArtifactsFromPublishDatas( |
| 174 | + publishData.meta, |
| 175 | + publishData.payloadAsMap |
183 | 176 | ) |
| 177 | + |
| 178 | + log.info( |
| 179 | + s"Detected a 'SNAPSHOT' version for ${publishData.meta}, publishing to Sonatype Central Snapshots at '$uri'" |
| 180 | + ) |
| 181 | + |
| 182 | + /** Maven uses this as a workspace for file manipulation. */ |
| 183 | + val mavenWorkspace = taskDest / "maven" |
| 184 | + |
| 185 | + if (dryRun) { |
| 186 | + val publishTo = taskDest / "repository" |
| 187 | + val result = worker.publishToLocal( |
| 188 | + publishTo = publishTo, |
| 189 | + workspace = mavenWorkspace, |
| 190 | + artifacts |
| 191 | + ) |
| 192 | + log.info(s"Dry-run publishing to '$publishTo' finished with result: $result") |
| 193 | + } else { |
| 194 | + val result = worker.publishToRemote( |
| 195 | + uri = uri, |
| 196 | + workspace = mavenWorkspace, |
| 197 | + username = credentials.username, |
| 198 | + password = credentials.password, |
| 199 | + artifacts |
| 200 | + ) |
| 201 | + log.info(s"Publishing to '$uri' finished with result: $result") |
| 202 | + } |
184 | 203 | } |
185 | 204 |
|
186 | 205 | def publishReleases(artifacts: Seq[PublishData], gpgArgs: GpgArgs): Unit = { |
@@ -239,5 +258,36 @@ object SonatypeCentralPublishModule extends ExternalModule, DefaultTaskModule, M |
239 | 258 | } |
240 | 259 | } |
241 | 260 |
|
| 261 | + private def getSonatypeCredential( |
| 262 | + credentialParameterValue: String, |
| 263 | + credentialName: String, |
| 264 | + envVariableName: String |
| 265 | + ): Task[String] = Task.Anon { |
| 266 | + if (credentialParameterValue.nonEmpty) { |
| 267 | + Result.Success(credentialParameterValue) |
| 268 | + } else { |
| 269 | + (for { |
| 270 | + credential <- Task.env.get(envVariableName) |
| 271 | + } yield { |
| 272 | + Result.Success(credential) |
| 273 | + }).getOrElse( |
| 274 | + Result.Failure( |
| 275 | + s"No $credentialName set. Consider using the $envVariableName environment variable or passing `$credentialName` argument" |
| 276 | + ) |
| 277 | + ) |
| 278 | + } |
| 279 | + } |
| 280 | + |
| 281 | + private def getSonatypeCredentials( |
| 282 | + usernameParameterValue: String, |
| 283 | + passwordParameterValue: String |
| 284 | + ): Task[SonatypeCredentials] = Task.Anon { |
| 285 | + val username = |
| 286 | + getSonatypeCredential(usernameParameterValue, "username", USERNAME_ENV_VARIABLE_NAME)() |
| 287 | + val password = |
| 288 | + getSonatypeCredential(passwordParameterValue, "password", PASSWORD_ENV_VARIABLE_NAME)() |
| 289 | + Result.Success(SonatypeCredentials(username, password)) |
| 290 | + } |
| 291 | + |
242 | 292 | lazy val millDiscover: mill.api.Discover = mill.api.Discover[this.type] |
243 | 293 | } |
0 commit comments