/
SonatypeRepositoryBuildService.kt
95 lines (83 loc) · 3.35 KB
/
SonatypeRepositoryBuildService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.vanniktech.maven.publish.sonatype
import com.vanniktech.maven.publish.SonatypeHost
import com.vanniktech.maven.publish.nexus.Nexus
import java.io.IOException
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.services.BuildService
import org.gradle.api.services.BuildServiceParameters
import org.gradle.api.services.BuildServiceRegistry
import org.gradle.tooling.events.FailureResult
import org.gradle.tooling.events.FinishEvent
import org.gradle.tooling.events.OperationCompletionListener
internal abstract class SonatypeRepositoryBuildService : BuildService<SonatypeRepositoryBuildService.Params>, AutoCloseable, OperationCompletionListener {
private val logger: Logger = Logging.getLogger(SonatypeRepositoryBuildService::class.java)
internal interface Params : BuildServiceParameters {
val sonatypeHost: Property<SonatypeHost>
val repositoryUsername: Property<String>
val repositoryPassword: Property<String>
val automaticRelease: Property<Boolean>
}
val nexus by lazy {
Nexus(
baseUrl = parameters.sonatypeHost.get().apiBaseUrl(),
username = parameters.repositoryUsername.get(),
password = parameters.repositoryPassword.get(),
)
}
// should only be accessed from CreateSonatypeRepositoryTask
// for all other use cases use MavenPublishBaseExtension
// the id of the staging repository that was created during this build
var stagingRepositoryId: String? = null
set(value) {
if (field != null) {
throw IllegalStateException("stagingRepositoryId was already set")
}
field = value
}
// should only be accessed from CloseAndReleaseSonatypeRepositoryTask
// indicates whether we already closed a staging repository to avoid doing it more than once in a build
var repositoryClosed: Boolean = false
private var buildIsSuccess: Boolean = true
override fun onFinish(event: FinishEvent) {
if (event.result is FailureResult) {
buildIsSuccess = false
}
}
override fun close() {
val stagingRepositoryId = this.stagingRepositoryId
if (stagingRepositoryId != null) {
if (buildIsSuccess) {
nexus.closeStagingRepository(stagingRepositoryId)
if (parameters.automaticRelease.get()) {
nexus.releaseStagingRepository(stagingRepositoryId)
}
} else {
try {
nexus.dropStagingRepository(stagingRepositoryId)
} catch (e: IOException) {
logger.info("Failed to drop staging repository $stagingRepositoryId", e)
}
}
}
}
companion object {
private const val NAME = "sonatype-repository-build-service"
fun BuildServiceRegistry.registerSonatypeRepositoryBuildService(
sonatypeHost: Provider<SonatypeHost>,
repositoryUsername: Provider<String>,
repositoryPassword: Provider<String>,
automaticRelease: Boolean,
): Provider<SonatypeRepositoryBuildService> {
return registerIfAbsent(NAME, SonatypeRepositoryBuildService::class.java) {
it.maxParallelUsages.set(1)
it.parameters.sonatypeHost.set(sonatypeHost)
it.parameters.repositoryUsername.set(repositoryUsername)
it.parameters.repositoryPassword.set(repositoryPassword)
it.parameters.automaticRelease.set(automaticRelease)
}
}
}
}