From ec4d278392cfd2b0034a1247faf6e56586245eee Mon Sep 17 00:00:00 2001 From: Thanos Psaridis Date: Sun, 19 Sep 2021 16:12:20 +0300 Subject: [PATCH] Prepare for maven publishing --- buildSrc/build.gradle.kts | 28 +++++-- buildSrc/src/main/kotlin/Artifact.kt | 44 +++++++++- wifiutils/build.gradle.kts | 117 ++++++++++++++++++++++++++- 3 files changed, 179 insertions(+), 10 deletions(-) diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 5d0dfdf..bdb406a 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -1,11 +1,25 @@ plugins { - base + `java-gradle-plugin` `kotlin-dsl` } -allprojects { - repositories { - mavenCentral() - gradlePluginPortal() - } -} \ No newline at end of file +java { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +repositories { + //google() + gradlePluginPortal() + mavenCentral() +} + +dependencies { + testImplementation(gradleTestKit()) + testImplementation("junit:junit:4.13.2") + +} + +tasks.withType().configureEach { + testLogging { showStandardStreams = true } +} diff --git a/buildSrc/src/main/kotlin/Artifact.kt b/buildSrc/src/main/kotlin/Artifact.kt index ad92884..9dba0be 100644 --- a/buildSrc/src/main/kotlin/Artifact.kt +++ b/buildSrc/src/main/kotlin/Artifact.kt @@ -1,3 +1,7 @@ +import org.gradle.api.Project +import java.util.* +import kotlin.collections.LinkedHashMap + object Artifact { val ARTIFACT_NAME = "wifiutils" @@ -6,6 +10,8 @@ object Artifact { val VERSION_CODE = 22 val POM_URL = "https://github.com/ThanosFisherman/WifiUtils" val POM_SCM_URL = "https://github.com/ThanosFisherman/WifiUtils" + val POM_SCM_CONNECTION = "scm:git:github.com/ThanosFisherman/WifiUtils.git" + val POM_SCM_DEV_CONNECTION = "scm:git:ssh://github.com/ThanosFisherman/WifiUtils.git" val POM_ISSUE_URL = "https://github.com/ThanosFisherman/WifiUtils/issues" val POM_DESC = "Library that makes it easy to Connect to WiFi hotspots using ssid and/or bssid" @@ -21,4 +27,40 @@ object Artifact { val POM_DEVELOPER_ID = "thanosfisherman" val POM_DEVELOPER_NAME = "Thanos Psaridis" -} \ No newline at end of file + val DEVELOPER_EMAIL = "psaridis@gmail.com" + + val RELEASE_REPO_URL = "https://oss.sonatype.org/service/local/staging/deploy/maven2" + val SNAPSHOT_REPO_URL = "https://oss.sonatype.org/content/repositories/snapshots" + val REPO_NAME = "sonatype" +} + + +val Project.credentialsMap: Map + inline get() = + LinkedHashMap().apply { + + val propertiesFile = rootProject.file("local.properties") + if (propertiesFile.exists() && propertiesFile.canRead()) { + try { + val properties = Properties() + properties.load(propertiesFile.inputStream()) + this["signing.keyId"] = properties.getProperty("signing.keyId") + this["signing.password"] = properties.getProperty("signing.password") + this["signing.secretKeyRingFile"] = + properties.getProperty("signing.secretKeyRingFile") + this["ossrhUsername"] = properties.getProperty("ossrhUsername") + this["ossrhPassword"] = properties.getProperty("ossrhPassword") + this["sonatypeStagingProfileId"] = + properties.getProperty("sonatypeStagingProfileId") + } catch (e: Exception) {} + } else { + this["signing.keyId"] = System.getenv("signing.keyId") + this["signing.password"] = System.getenv("signing.password") + this["signing.secretKeyRingFile"] = System.getenv("signing.secretKeyRingFile") + this["ossrhUsername"] = System.getenv("ossrhUsername") + this["ossrhPassword"] = System.getenv("ossrhPassword") + this["sonatypeStagingProfileId"] = System.getenv("sonatypeStagingProfileId") + //val keystoreFile = project.rootProject.file(rootDir.path + File.separator + System.getenv("keystore_name")) + } + } + diff --git a/wifiutils/build.gradle.kts b/wifiutils/build.gradle.kts index f8bdbf8..c7be209 100644 --- a/wifiutils/build.gradle.kts +++ b/wifiutils/build.gradle.kts @@ -1,5 +1,3 @@ -import java.util.* - plugins { id(GradlePluginId.ANDROID_LIBRARY) kotlin("android") @@ -7,6 +5,7 @@ plugins { id(GradlePluginId.DOKKA) version GradlePluginVersion.DOKKA_VERSION // Maven publication `maven-publish` + signing } android { @@ -49,3 +48,117 @@ dependencies { //addTestDependencies() } +val dokkaTask by tasks.creating(org.jetbrains.dokka.gradle.DokkaTask::class) { + group = JavaBasePlugin.DOCUMENTATION_GROUP + description = "Assembles Kotlin docs with Dokka" + outputDirectory.set(File("$buildDir/dokka")) + //documentationFileName.set("README.md") +} + +val dokkaJar by tasks.creating(Jar::class) { + archiveClassifier.set("dokka") + from("$buildDir/dokka") + dependsOn(dokkaTask) +} + +val sourcesJar by tasks.creating(Jar::class) { + archiveClassifier.set("sources") + if (project.plugins.findPlugin("com.android.library") != null) { + from(android.sourceSets.getByName("main").java.srcDirs) + } else { + from(sourceSets.getByName("main").java.srcDirs) + } +} + +artifacts { + archives(sourcesJar) + archives(dokkaJar) +} + +credentialsMap.forEach { extra.set(it.key, it.value) } + +publishing { + publications { + create(Artifact.ARTIFACT_NAME) { + groupId = Artifact.ARTIFACT_GROUP + artifactId = Artifact.ARTIFACT_NAME + version = Artifact.VERSION_NAME + + if (project.plugins.findPlugin("com.android.library") != null) { + artifact("$buildDir/outputs/aar/${project.name}-release.aar") + } else { + from(components["java"]) + //artifact("$buildDir/libs/${project.getName()}-${version}.jar") + } + artifacts { + artifact(sourcesJar) + artifact(dokkaJar) + } + + pom { + name.set(Artifact.LIBRARY_NAME) + description.set(Artifact.POM_DESC) + url.set(Artifact.POM_URL) + licenses { + license { + name.set(Artifact.POM_LICENSE_NAME) + url.set(Artifact.POM_LICENSE_URL) + distribution.set(Artifact.POM_URL) + } + } + developers { + developer { + id.set(Artifact.POM_DEVELOPER_ID) + name.set(Artifact.POM_DEVELOPER_NAME) + email.set(Artifact.DEVELOPER_EMAIL) + } + } + scm { + connection.set(Artifact.POM_SCM_CONNECTION) + developerConnection.set(Artifact.POM_SCM_DEV_CONNECTION) + url.set(Artifact.POM_SCM_URL) + } + repositories { + maven { + // change URLs to point to your repos, e.g. http://my.org/repo + val releasesRepoUrl = uri(Artifact.RELEASE_REPO_URL) + val snapshotsRepoUrl = uri(Artifact.SNAPSHOT_REPO_URL) + name = Artifact.REPO_NAME + url = if (version.toString() + .endsWith("SNAPSHOT") + ) snapshotsRepoUrl else releasesRepoUrl + credentials { + username = credentialsMap["ossrhUsername"] + password = credentialsMap["ossrhPassword"] + } + } + } + + // hack if you wanna include any transitive dependencies. I'm a hackur indeed + /* withXml { + asNode().apply { + appendNode("description", Artifact.POM_DESC) + appendNode("name", Artifact.LIBRARY_NAME) + appendNode("url", Artifact.POM_URL) + appendNode("licenses").appendNode("license").apply { + appendNode("name", Artifact.POM_LICENSE_NAME) + appendNode("url", Artifact.POM_LICENSE_URL) + appendNode("distribution", Artifact.POM_LICENSE_DIST) + } + appendNode("developers").appendNode("developer").apply { + appendNode("id", Artifact.POM_DEVELOPER_ID) + appendNode("name", Artifact.POM_DEVELOPER_NAME) + } + appendNode("scm").apply { + appendNode("url", Artifact.POM_SCM_URL) + } + } + }*/ + } + } + } +} + +signing { + sign(publishing.publications[Artifact.ARTIFACT_NAME]) +}