From d48553bf3041869afca3eb02445a190f89543379 Mon Sep 17 00:00:00 2001 From: Mustafa Ozhan Date: Thu, 21 Dec 2023 22:15:44 +0100 Subject: [PATCH] [Oztechan/CCC#3020] Fix Library publication issues --- build.gradle.kts | 109 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/build.gradle.kts b/build.gradle.kts index 8cb834d..eb015ef 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,22 +2,131 @@ * Copyright (c) 2020 Mustafa Ozhan. All rights reserved. */ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import java.io.IOException +import java.util.Properties plugins { libs.plugins.apply { alias(libs.plugins.kotlinMultiplatform).apply(false) alias(libs.plugins.kover) } + `maven-publish` } allprojects { + apply(plugin = rootProject.libs.plugins.kover.get().pluginId).also { rootProject.dependencies.add("kover", project(path)) } + Library.apply { + + group = GROUP + version = ProjectSettings.getVersionName(project) + + repositories { + google() + mavenCentral() + } + + val emptyJavadocJar by tasks.registering(Jar::class) { + archiveClassifier.set("javadoc") + } + + afterEvaluate { + extensions.findByType()?.apply { + repositories { + maven { + url = uri(if (isReleaseBuild) RELEASE_URL else SNAPSHOT_URL) + credentials { + username = getSecret("MAVEN_USERNAME") + password = getSecret("MAVEN_PASSWORD") + } + } + } + + publications.withType().configureEach { + artifact(emptyJavadocJar.get()) + + pom { + name.set(NAME) + description.set(DESCRIPTION) + url.set(URL) + + licenses { + license { + name.set(LICENSE_NAME) + url.set(LICENSE_URL) + distribution.set(LICENSE_DISTRIBUTION) + } + } + developers { + developer { + id.set(DEVELOPER_ID) + name.set(DEVELOPER_NAME) + email.set(DEVELOPER_EMAIL) + } + } + scm { url.set(URL) } + } + } + } + + extensions.findByType()?.let { publishing -> + val key = getSecret("GPG_KEY").replace("\\n", "\n") + val password = getSecret("GPG_PASSWORD") + + extensions.findByType()?.apply { + useInMemoryPgpKeys(key, password) + sign(publishing.publications) + } + } + + tasks.withType().configureEach { + onlyIf { isReleaseBuild } + } + } + } + tasks.withType { kotlinOptions { allWarningsAsErrors = true } } } + +val isReleaseBuild: Boolean + get() = System.getenv("GPG_KEY") != null + +fun getSecret( + key: String, + default: String = "secret" // these values can not be public +): String = System.getenv(key).let { + if (it.isNullOrEmpty()) { + getSecretProperties()?.get(key)?.toString() ?: default + } else { + it + } +} + +fun getSecretProperties() = try { + Properties().apply { load(file("key.properties").inputStream()) } +} catch (e: IOException) { + logger.debug(e.message, e) + null +} + +object Library { + const val GROUP = "com.github.submob" + const val URL = "https://github.com/SubMob/ScopeMob" + const val NAME = "ScopeMob" + const val DESCRIPTION = "Set of useful scope and higher-order functions" + const val DEVELOPER_NAME = "Mustafa Ozhan" + const val DEVELOPER_ID = "mustafaozhan" + const val DEVELOPER_EMAIL = "mr.mustafa.ozhan@gmail.com" + const val LICENSE_NAME = "The Apache Software License, Version 2.0" + const val LICENSE_URL = "http://www.apache.org/licenses/LICENSE-2.0.txt" + const val LICENSE_DISTRIBUTION = "repo" + const val RELEASE_URL = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2" + const val SNAPSHOT_URL = "https://s01.oss.sonatype.org/content/repositories/snapshots" +}