Skip to content

Commit

Permalink
chore(deps)!: update plugin kotlin-qa to v0.75.0, use SCREAMING_SNAKE…
Browse files Browse the repository at this point in the history
…_CASE for `MAVEN_CENTRAL_NAME`, `MAVEN_CENTRAL_URL`, and `MAVEN_CENTRAL_NEXUS_URL`
  • Loading branch information
DanySK committed Dec 2, 2024
1 parent d368b83 commit 1bdc5c4
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 161 deletions.
24 changes: 13 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ gitSemVer {
}

group = "org.danilopianini"

inner class ProjectInfo {
val longName = "Gradle Publish On Maven Central Plugin"
val projectDetails = "A Plugin for easily publishing artifacts on Maven Central"
Expand Down Expand Up @@ -130,20 +131,21 @@ if (System.getenv("CI") == true.toString()) {
}
}

val registerCredentials = tasks.register("registerGradlePluginPortalCredentials") {
doLast {
listOf("gradle.publish.key", "gradle.publish.secret").forEach {
if (!(project.hasProperty(it) or System.getenv().containsKey(it))) {
val bashName = it.uppercase().replace(".", "_")
System.getProperties().setProperty(
it,
System.getenv(bashName)
?: error("Property $it is unset and environment variable $bashName unavailable"),
)
val registerCredentials =
tasks.register("registerGradlePluginPortalCredentials") {
doLast {
listOf("gradle.publish.key", "gradle.publish.secret").forEach {
if (!(project.hasProperty(it) or System.getenv().containsKey(it))) {
val bashName = it.uppercase().replace(".", "_")
System.getProperties().setProperty(
it,
System.getenv(bashName)
?: error("Property $it is unset and environment variable $bashName unavailable"),
)
}
}
}
}
}

tasks.publishPlugins {
dependsOn(registerCredentials)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
gitSemVer = "org.danilopianini.git-sensitive-semantic-versioning:3.1.7"
gradlePluginPublish = "com.gradle.plugin-publish:1.3.0"
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin-qa = "org.danilopianini.gradle-kotlin-qa:0.70.2"
kotlin-qa = "org.danilopianini.gradle-kotlin-qa:0.75.0"
multiJvmTesting = "org.danilopianini.multi-jvm-test-plugin:1.3.2"
nexus-publish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexus-publish" }
publishOnCentral = "org.danilopianini.publish-on-central:5.1.11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ internal fun Project.dokkaTasksFor(docStyle: DocStyle): TaskCollection<out Task>
* If a task is of type `DokkaTask` (cf. [isDokkaTask]), then retrieves the value of its `outputDirectory`
* property, if any.
*/
internal val Task.dokkaOutputDirectory: Any get() = when {
isDokkaTask -> property("outputDirectory")
?: error("$name has no property 'outputDirectory': Dokka version incompatible with publish-on-central?")
else -> error("$name is not of type $DOKKA_TASK_CLASS_NAME")
}
internal val Task.dokkaOutputDirectory: Any get() =
when {
isDokkaTask ->
property("outputDirectory")
?: error("$name has no property 'outputDirectory': Dokka version incompatible with publish-on-central?")
else -> error("$name is not of type $DOKKA_TASK_CLASS_NAME")
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,34 @@ import javax.inject.Inject
/**
* A task that creates a Nexus Client operating at the [Repository] [repoToConfigure] with URL [nexusUrl].
*/
open class InitializeNexusClient @Inject constructor(
@Input
val repoToConfigure: Repository,
@Input
val nexusUrl: String,
) : DefaultTask() {
open class InitializeNexusClient
@Inject
constructor(
@Input
val repoToConfigure: Repository,
@Input
val nexusUrl: String,
) : DefaultTask() {
/**
* The Nexus Client, accessible only **after** the execution of the task.
*/
@Internal
lateinit var nexusClient: NexusStatefulOperation

/**
* The Nexus Client, accessible only **after** the execution of the task.
*/
@Internal
lateinit var nexusClient: NexusStatefulOperation

/**
* Initializes the Nexus Client.
*/
@TaskAction
fun initializeClient() {
nexusClient = NexusStatefulOperation(
project = project,
nexusUrl = nexusUrl,
group = project.group.toString(),
user = repoToConfigure.user,
password = repoToConfigure.password,
timeOut = repoToConfigure.nexusTimeOut,
connectionTimeOut = repoToConfigure.nexusConnectTimeOut,
)
/**
* Initializes the Nexus Client.
*/
@TaskAction
fun initializeClient() {
nexusClient =
NexusStatefulOperation(
project = project,
nexusUrl = nexusUrl,
group = project.group.toString(),
user = repoToConfigure.user,
password = repoToConfigure.password,
timeOut = repoToConfigure.nexusTimeOut,
connectionTimeOut = repoToConfigure.nexusConnectTimeOut,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ data class NexusStatefulOperation(
private val connectionTimeOut: Duration,
private val group: String,
) {

/**
* Repository description.
*/
Expand Down Expand Up @@ -83,7 +82,7 @@ data class NexusStatefulOperation(
private val transitioner by lazy {
StagingRepositoryTransitioner(
client,
BasicActionRetrier(Int.MAX_VALUE, Duration.ofSeconds(retryInterval)) { it.transitioning },
BasicActionRetrier(Int.MAX_VALUE, Duration.ofSeconds(RETRY_INTERVAL)) { it.transitioning },
)
}

Expand Down Expand Up @@ -129,6 +128,6 @@ data class NexusStatefulOperation(
}

private companion object {
private const val retryInterval: Long = 10
private const val RETRY_INTERVAL: Long = 10
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import java.net.URI
import kotlin.reflect.KClass

internal object ProjectExtensions {

/**
* The id of the Kotlin/JS plugin.
*/
Expand Down Expand Up @@ -105,86 +104,97 @@ internal object ProjectExtensions {
}
}

private fun Project.configureNexusRepository(repoToConfigure: Repository, nexusUrl: String) {
private fun Project.configureNexusRepository(
repoToConfigure: Repository,
nexusUrl: String,
) {
val repoName = repoToConfigure.name
val nexusClient = rootProject.registerTaskIfNeeded(
"createNexusClientFor$repoName",
InitializeNexusClient::class,
repoToConfigure,
nexusUrl,
) as InitializeNexusClient
val nexusClient =
rootProject.registerTaskIfNeeded(
"createNexusClientFor$repoName",
InitializeNexusClient::class,
repoToConfigure,
nexusUrl,
) as InitializeNexusClient
/*
* Creates a new staging repository on the Nexus server, or fetches an existing one if the repoId is known.
*/
val createStagingRepository = rootProject.registerTaskIfNeeded(
"createStagingRepositoryOn$repoName",
) {
val stagingRepoIdsFileName = "staging-repo-ids.properties"
val stagingRepoIdsFile = rootProject.layout.buildDirectory.map { it.asFile.resolve(stagingRepoIdsFileName) }
outputs.file(stagingRepoIdsFile)
dependsOn(nexusClient)
doLast {
rootProject.warnIfCredentialsAreMissing(repoToConfigure)
nexusClient.nexusClient.repoUrl // triggers the initialization of a repository
val repoId = nexusClient.nexusClient.repoId
// Write the staging repository ID to build/staging-repo-ids.properties file
stagingRepoIdsFile.get().appendText("$repoName=$repoId" + System.lineSeparator())
logger.lifecycle("Append repo name {} to file {}", repoId, stagingRepoIdsFile.get().path)
val createStagingRepository =
rootProject.registerTaskIfNeeded(
"createStagingRepositoryOn$repoName",
) {
val stagingRepoIdsFileName = "staging-repo-ids.properties"
val stagingRepoIdsFile =
rootProject.layout.buildDirectory.map { it.asFile.resolve(stagingRepoIdsFileName) }
outputs.file(stagingRepoIdsFile)
dependsOn(nexusClient)
doLast {
rootProject.warnIfCredentialsAreMissing(repoToConfigure)
nexusClient.nexusClient.repoUrl // triggers the initialization of a repository
val repoId = nexusClient.nexusClient.repoId
// Write the staging repository ID to build/staging-repo-ids.properties file
stagingRepoIdsFile.get().appendText("$repoName=$repoId" + System.lineSeparator())
logger.lifecycle("Append repo name {} to file {}", repoId, stagingRepoIdsFile.get().path)
}
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Creates a new Nexus staging repository on $repoName."
}
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Creates a new Nexus staging repository on $repoName."
}
/*
* Collector of all upload tasks. Actual uploads are defined at the bottom.
* Requires the creation of the staging repository.
*/
val uploadAllPublications = tasks.register("uploadAllPublicationsTo${repoName}Nexus") {
it.dependsOn(createStagingRepository)
it.group = PublishingPlugin.PUBLISH_TASK_GROUP
it.description = "Uploads all publications to a staging repository on $repoName."
}
val uploadAllPublications =
tasks.register("uploadAllPublicationsTo${repoName}Nexus") {
it.dependsOn(createStagingRepository)
it.group = PublishingPlugin.PUBLISH_TASK_GROUP
it.description = "Uploads all publications to a staging repository on $repoName."
}
/*
* Closes the staging repository. If it's closed already, skips the operation.
* Runs after all uploads. Requires the creation of the staging repository.
*/
val closeStagingRepository = rootProject.registerTaskIfNeeded("closeStagingRepositoryOn$repoName") {
doLast {
with(nexusClient.nexusClient) {
when (client.getStagingRepositoryStateById(repoId).state) {
CLOSED -> logger.warn("The staging repository is already closed. Skipping.")
else -> close()
val closeStagingRepository =
rootProject.registerTaskIfNeeded("closeStagingRepositoryOn$repoName") {
doLast {
with(nexusClient.nexusClient) {
when (client.getStagingRepositoryStateById(repoId).state) {
CLOSED -> logger.warn("The staging repository is already closed. Skipping.")
else -> close()
}
}
}
dependsOn(createStagingRepository)
mustRunAfter(uploadAllPublications)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Closes the Nexus repository on $repoName."
}
dependsOn(createStagingRepository)
mustRunAfter(uploadAllPublications)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Closes the Nexus repository on $repoName."
}
/*
* Releases the staging repository. Requires closing.
*/
val release = rootProject.registerTaskIfNeeded("releaseStagingRepositoryOn${repoToConfigure.name}") {
doLast { nexusClient.nexusClient.release() }
dependsOn(closeStagingRepository)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Releases the Nexus repo on ${repoToConfigure.name}. " +
"Mutually exclusive with dropStagingRepositoryOn${repoToConfigure.name}."
}
val release =
rootProject.registerTaskIfNeeded("releaseStagingRepositoryOn${repoToConfigure.name}") {
doLast { nexusClient.nexusClient.release() }
dependsOn(closeStagingRepository)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Releases the Nexus repo on ${repoToConfigure.name}. " +
"Mutually exclusive with dropStagingRepositoryOn${repoToConfigure.name}."
}
/*
* Drops the staging repository.
* Requires the creation of the staging repository.
* It must run after all uploads.
* If closing is requested as well, drop must run after it.
*/
val drop = rootProject.registerTaskIfNeeded("dropStagingRepositoryOn${repoToConfigure.name}") {
doLast { nexusClient.nexusClient.drop() }
dependsOn(createStagingRepository)
mustRunAfter(uploadAllPublications)
mustRunAfter(closeStagingRepository)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description = "Drops the Nexus repo on ${repoToConfigure.name}. Incompatible with releasing the same repo."
}
val drop =
rootProject.registerTaskIfNeeded("dropStagingRepositoryOn${repoToConfigure.name}") {
doLast { nexusClient.nexusClient.drop() }
dependsOn(createStagingRepository)
mustRunAfter(uploadAllPublications)
mustRunAfter(closeStagingRepository)
group = PublishingPlugin.PUBLISH_TASK_GROUP
description =
"Drops the Nexus repo on ${repoToConfigure.name}. Incompatible with releasing the same repo."
}
/*
* Checks that only release or drop are selected for execution, as they are mutually exclusive.
*/
Expand All @@ -198,14 +208,15 @@ internal object ProjectExtensions {
project.tasks.register<PublishToMavenRepository>(
"upload${publicationName}To${repoToConfigure.name}Nexus",
).configure { uploadTask ->
uploadTask.repository = project.repositories.maven { repo ->
repo.name = repoToConfigure.name
repo.url = project.uri(repoToConfigure.url)
repo.credentials {
it.username = repoToConfigure.user.orNull
it.password = repoToConfigure.password.orNull
uploadTask.repository =
project.repositories.maven { repo ->
repo.name = repoToConfigure.name
repo.url = project.uri(repoToConfigure.url)
repo.credentials {
it.username = repoToConfigure.user.orNull
it.password = repoToConfigure.password.orNull
}
}
}
uploadTask.publication = publication
publication.signingTasks(project).forEach { uploadTask.dependsOn(it) }
tasks.withType<Sign>().forEach { uploadTask.mustRunAfter(it) }
Expand Down Expand Up @@ -247,12 +258,13 @@ internal object ProjectExtensions {
name: String,
vararg parameters: Any = emptyArray(),
configuration: DefaultTask.() -> Unit = { },
): Task = registerTaskIfNeeded(
name = name,
type = DefaultTask::class,
parameters = parameters,
configuration = configuration,
)
): Task =
registerTaskIfNeeded(
name = name,
type = DefaultTask::class,
parameters = parameters,
configuration = configuration,
)

private fun Project.warnIfCredentialsAreMissing(repository: Repository) {
if (repository.user.orNull == null) {
Expand Down
Loading

0 comments on commit 1bdc5c4

Please sign in to comment.