|
| 1 | +package com.redmadrobot.build |
| 2 | + |
| 3 | +import com.android.build.gradle.BaseExtension |
| 4 | +import org.gradle.api.Plugin |
| 5 | +import org.gradle.api.Project |
| 6 | +import org.gradle.api.publish.PublishingExtension |
| 7 | +import org.gradle.api.publish.maven.MavenPublication |
| 8 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin |
| 9 | +import org.gradle.jvm.tasks.Jar |
| 10 | +import org.gradle.kotlin.dsl.* |
| 11 | +import org.gradle.plugins.signing.SigningExtension |
| 12 | +import org.gradle.plugins.signing.SigningPlugin |
| 13 | +import java.io.File |
| 14 | +import java.util.* |
| 15 | + |
| 16 | +class PublishPlugin : Plugin<Project> { |
| 17 | + lateinit var projectDir: File |
| 18 | + lateinit var rootDir: File |
| 19 | + |
| 20 | + private val publishProperties by lazy { |
| 21 | + |
| 22 | + Properties().apply { |
| 23 | + load(File("$rootDir/gradle/publish.properties").inputStream()) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private val libraryProperties by lazy { |
| 28 | + Properties().apply { |
| 29 | + load(File("$projectDir/library.properties").inputStream()) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + override fun apply(target: Project) { |
| 34 | + projectDir = target.projectDir |
| 35 | + rootDir = target.rootDir |
| 36 | + |
| 37 | + with(target) { |
| 38 | + afterEvaluate { |
| 39 | + configurePublishing() |
| 40 | + configureSigning() |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private fun Project.configurePublishing() { |
| 46 | + plugins.apply(MavenPublishPlugin::class.java) |
| 47 | + val libDescription = this.description.orEmpty() |
| 48 | + |
| 49 | + configure<PublishingExtension> { |
| 50 | + val android = extensions.getByName<BaseExtension>("android") |
| 51 | + |
| 52 | + val sourcesJar = tasks.register<Jar>("sourcesJar") { |
| 53 | + archiveClassifier.set("sources") |
| 54 | + from(android.sourceSets["main"].java.srcDirs) |
| 55 | + } |
| 56 | + |
| 57 | + publications { |
| 58 | + create("release", MavenPublication::class.java) { |
| 59 | + from(project.components.findByName("release")) |
| 60 | + |
| 61 | + artifact(sourcesJar) |
| 62 | + |
| 63 | + groupId = publishProperties.getProperty("group_id") |
| 64 | + artifactId = libraryProperties.getProperty("lib_name") |
| 65 | + version = publishProperties.getProperty("lib_version") |
| 66 | + |
| 67 | + pom { |
| 68 | + name.set(libraryProperties.getProperty("lib_name")) |
| 69 | + description.set(libDescription) |
| 70 | + url.set(publishProperties.getProperty("home_page")) |
| 71 | + |
| 72 | + licenses { |
| 73 | + license { |
| 74 | + name.set(publishProperties.getProperty("license_name")) |
| 75 | + url.set(publishProperties.getProperty("license_url")) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + developers { |
| 80 | + developer { |
| 81 | + id.set("Zestxx") |
| 82 | + name.set("Roman Choriev") |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + developer { |
| 87 | + id.set("osipxd") |
| 88 | + name.set("Osip Fatkullin") |
| 89 | + |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + scm { |
| 94 | + connection.set("scm:git:git://github.com/RedMadRobot/debug-panel-android.git") |
| 95 | + developerConnection.set("scm:git:ssh://github.com/RedMadRobot/debug-panel-android.git") |
| 96 | + url.set(publishProperties.getProperty("home_page")) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + repositories { |
| 101 | + mavenLocal() |
| 102 | + maven { |
| 103 | + name = "OSSRH" |
| 104 | + url = project.uri(publishProperties.getProperty("sonatype_repo")) |
| 105 | + |
| 106 | + credentials { |
| 107 | + username = System.getenv("OSSRH_USER") |
| 108 | + password = System.getenv("OSSRH_PASSWORD") |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private fun Project.configureSigning() { |
| 118 | + plugins.apply(SigningPlugin::class.java) |
| 119 | + |
| 120 | + configure<SigningExtension> { |
| 121 | + val publishing = extensions.getByType(PublishingExtension::class.java) |
| 122 | + |
| 123 | + useGpgCmd() |
| 124 | + sign(publishing.publications.getByName("release")) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments