|
1 | 1 | package publish.mavencentral |
2 | 2 |
|
| 3 | +import com.vanniktech.maven.publish.MavenPublishBaseExtension |
| 4 | +import com.vanniktech.maven.publish.MavenPublishPlugin |
| 5 | +import com.vanniktech.maven.publish.SonatypeHost |
3 | 6 | import org.gradle.api.Plugin |
4 | 7 | import org.gradle.api.Project |
5 | 8 | import org.gradle.api.publish.PublishingExtension |
6 | 9 | import org.gradle.api.publish.maven.MavenPublication |
7 | | -import org.gradle.plugins.signing.SigningExtension |
| 10 | +import org.gradle.plugins.signing.Sign |
8 | 11 |
|
9 | | -class PublishToMavenCentralPlugin: Plugin<Project> { |
| 12 | +@Suppress("unused") // false positive |
| 13 | +class PublishToMavenCentralPlugin : Plugin<Project> { |
10 | 14 | override fun apply(target: Project) { |
11 | | - target.plugins.apply("maven-publish") |
12 | | - target.plugins.apply("signing") |
13 | | - target.plugins.apply("org.ajoberstar.grgit") |
| 15 | + target.plugins.apply(org.gradle.api.publish.maven.plugins.MavenPublishPlugin::class.java) |
14 | 16 |
|
15 | 17 | target.afterEvaluate { |
16 | | - val ossrhUser = target.propOrEnv("GODOT_KOTLIN_MAVEN_CENTRAL_TOKEN_USERNAME") |
17 | | - val ossrhPassword = target.propOrEnv("GODOT_KOTLIN_MAVEN_CENTRAL_TOKEN_PASSWORD") |
18 | | - val signingKey = target.propOrEnv("GODOT_KOTLIN_GPG_PRIVATE_KEY_ASCII") |
19 | | - val signingPassword = target.propOrEnv("GODOT_KOTLIN_GPG_KEY_PASSPHRASE") |
| 18 | + val mavenCentralUser = target.propOrEnv("UTOPIA_RISE_MAVEN_CENTRAL_PORTAL_TOKEN_USERNAME") ?: target.propOrEnv("mavenCentralUsername") |
| 19 | + val mavenCentralPassword = target.propOrEnv("UTOPIA_RISE_MAVEN_CENTRAL_PORTAL_TOKEN_PASSWORD") ?: target.propOrEnv("mavenCentralPassword") |
| 20 | + val gpgInMemoryKey = target.propOrEnv("UTOPIA_RISE_GPG_PRIVATE_KEY_ASCII") ?: target.propOrEnv("signingInMemoryKey") |
| 21 | + val gpgPassword = target.propOrEnv("UTOPIA_RISE_GPG_KEY_PASSPHRASE") ?: target.propOrEnv("signingInMemoryKeyPassword") |
20 | 22 |
|
21 | | - val releaseMode = !(target.version as String).endsWith("-SNAPSHOT") |
| 23 | + val canSign = mavenCentralUser != null && mavenCentralPassword != null && gpgInMemoryKey != null && gpgPassword != null |
22 | 24 |
|
23 | | - target.extensions.configure(SigningExtension::class.java) { |
24 | | - @Suppress("UnstableApiUsage") |
25 | | - useInMemoryPgpKeys(signingKey, signingPassword) |
26 | | - target.extensions.findByType(PublishingExtension::class.java)?.publications?.all { |
27 | | - sign(this) |
28 | | - } |
29 | | - } |
30 | | - |
31 | | - target.extensions.configure(PublishingExtension::class.java) { |
32 | | - repositories { |
33 | | - maven { |
34 | | - val targetRepo = if (releaseMode) { |
35 | | - "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/" |
36 | | - } else { |
37 | | - "https://s01.oss.sonatype.org/content/repositories/snapshots/" |
38 | | - } |
39 | | - setUrl(targetRepo) |
40 | | - |
41 | | - credentials { |
42 | | - username = ossrhUser |
43 | | - password = ossrhPassword |
44 | | - } |
45 | | - } |
46 | | - } |
| 25 | + target.extensions.getByType(PublishingExtension::class.java).apply { |
47 | 26 | publications { |
48 | 27 | all { |
49 | 28 | if (this is MavenPublication) { |
| 29 | + groupId = "com.utopia-rise" |
| 30 | + artifactId = if (artifactId.isNullOrEmpty()) target.name else artifactId |
| 31 | + version = target.version as String |
| 32 | + |
50 | 33 | pom { |
51 | | - name.set("kotlin-preprocessors") |
52 | | - description.set("Gradle plugin to define preprocessors for kotlin language") |
| 34 | + url.set("https://github.com/utopia-rise/godot-kotlin-jvm.git") |
53 | 35 |
|
54 | | - url.set("https://github.com/utopia-rise/kotlin-preprocessors.git") |
| 36 | + if (name.getOrElse("").isNullOrEmpty()) { |
| 37 | + name.set(target.name) |
| 38 | + } |
| 39 | + if (description.getOrElse("").isNullOrEmpty()) { |
| 40 | + description.set(target.description ?: "Godot kotlin jvm module") |
| 41 | + } |
55 | 42 |
|
56 | 43 | scm { |
57 | | - connection.set("scm:git:https://github.com/utopia-rise/kotlin-preprocessors") |
58 | | - developerConnection.set("scm:git:github.com:utopia-rise/kotlin-preprocessors.git") |
| 44 | + connection.set("scm:git:https://github.com/utopia-rise/godot-kotlin-jvm") |
| 45 | + developerConnection.set("scm:git:github.com:utopia-rise/godot-kotlin-jvm.git") |
59 | 46 | tag.set("master") //FIXME |
60 | | - url.set("https://github.com/utopia-rise/kotlin-preprocessors") |
| 47 | + url.set("https://github.com/utopia-rise/godot-kotlin-jvm") |
61 | 48 | } |
62 | 49 |
|
63 | 50 | licenses { |
64 | 51 | license { |
65 | 52 | name.set("MIT License") |
66 | | - url.set("https://github.com/utopia-rise/kotlin-preprocessors/blob/master/LICENSE") |
| 53 | + url.set("https://github.com/utopia-rise/godot-kotlin-jvm/blob/master/LICENSE") |
67 | 54 | distribution.set("repo") |
68 | 55 | } |
69 | 56 | } |
70 | 57 |
|
71 | 58 | developers { |
| 59 | + developer { |
| 60 | + id.set("core") |
| 61 | + name.set("Ranie Jade Ramiso") |
| 62 | + url.set("https://github.com/raniejade") |
| 63 | + email.set("raniejaderamiso@gmail.com") |
| 64 | + } |
72 | 65 | developer { |
73 | 66 | id.set("core") |
74 | 67 | name.set("Pierre-Thomas Meisels") |
75 | 68 | url.set("https://github.com/piiertho") |
76 | 69 | email.set("meisels27@yahoo.fr") |
77 | 70 | } |
| 71 | + developer { |
| 72 | + id.set("core") |
| 73 | + name.set("Cedric Hippmann") |
| 74 | + url.set("https://github.com/chippmann") |
| 75 | + email.set("cedric.hippmann@hotmail.com") |
| 76 | + } |
| 77 | + developer { |
| 78 | + id.set("core") |
| 79 | + name.set("Tristan Grespinet") |
| 80 | + url.set("https://github.com/CedNaru") |
| 81 | + email.set("ced.naru@gmail.com") |
| 82 | + } |
78 | 83 | } |
79 | 84 | } |
80 | 85 | } |
81 | 86 | } |
82 | 87 | } |
83 | 88 | } |
| 89 | + |
| 90 | + |
| 91 | + if (canSign) { |
| 92 | + logger.info("Will sign artifact for project \"${name}\" and setup publishing") |
| 93 | + |
| 94 | + pluginManager.apply(MavenPublishPlugin::class.java) |
| 95 | + extensions.getByType(MavenPublishBaseExtension::class.java).apply { |
| 96 | + publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL) |
| 97 | + signAllPublications() |
| 98 | + } |
| 99 | + |
| 100 | + target.afterEvaluate { |
| 101 | + target |
| 102 | + .tasks |
| 103 | + .filter { task -> task.name.startsWith("publish") } |
| 104 | + .forEach { task -> |
| 105 | + task.dependsOn(target.tasks.withType(Sign::class.java)) |
| 106 | + } |
| 107 | + } |
| 108 | + } else { |
| 109 | + logger.warn("Cannot sign project \"${name}\" as credentials are missing. Will not setup signing and remote publishing credentials. Publishing will only work to maven local!") |
| 110 | + } |
84 | 111 | } |
85 | 112 | } |
86 | 113 | } |
87 | 114 |
|
88 | 115 | fun Project.propOrEnv(name: String): String? { |
89 | | - var property: String? = findProperty(name) as String? |
| 116 | + var property: String? = findProperty(name) as? String? |
90 | 117 | if (property == null) { |
91 | 118 | property = System.getenv(name) |
92 | 119 | } |
|
0 commit comments