|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// buildSrc/src/main/kotlin/PublishingConventionPlugin.kt |
| 18 | +import org.gradle.api.Plugin |
| 19 | +import org.gradle.api.Project |
| 20 | +import org.gradle.api.publish.PublishingExtension |
| 21 | +import org.gradle.api.publish.maven.MavenPublication |
| 22 | +import org.gradle.kotlin.dsl.* |
| 23 | +import org.gradle.testing.jacoco.plugins.JacocoPluginExtension |
| 24 | +import org.gradle.api.tasks.testing.Test |
| 25 | +import org.gradle.testing.jacoco.plugins.JacocoTaskExtension |
| 26 | +import org.gradle.plugins.signing.SigningExtension |
| 27 | +import org.gradle.api.publish.maven.* |
| 28 | + |
| 29 | +class PublishingConventionPlugin : Plugin<Project> { |
| 30 | + override fun apply(project: Project) { |
| 31 | + project.run { |
| 32 | + |
| 33 | + applyPlugins() |
| 34 | + configureJacoco() |
| 35 | + configurePublishing() |
| 36 | + configureSigning() |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private fun Project.applyPlugins() { |
| 41 | + apply(plugin = "com.android.library") |
| 42 | + apply(plugin = "com.mxalbert.gradle.jacoco-android") |
| 43 | + apply(plugin = "maven-publish") |
| 44 | + apply(plugin = "org.jetbrains.dokka") |
| 45 | + apply(plugin = "signing") |
| 46 | + } |
| 47 | + |
| 48 | + private fun Project.configureJacoco() { |
| 49 | + configure<JacocoPluginExtension> { |
| 50 | + toolVersion = "0.8.7" |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + tasks.withType<Test>().configureEach { |
| 55 | + extensions.configure(JacocoTaskExtension::class.java) { |
| 56 | + isIncludeNoLocationClasses = true |
| 57 | + excludes = listOf("jdk.internal.*") |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private fun Project.configurePublishing() { |
| 63 | + extensions.configure<com.android.build.gradle.LibraryExtension> { |
| 64 | + publishing { |
| 65 | + singleVariant("release") { |
| 66 | + withSourcesJar() |
| 67 | + withJavadocJar() |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + extensions.configure<PublishingExtension> { |
| 72 | + publications { |
| 73 | + create<MavenPublication>("aar") { |
| 74 | + afterEvaluate { |
| 75 | + from(components["release"]) |
| 76 | + } |
| 77 | + pom { |
| 78 | + name.set(project.name) |
| 79 | + description.set("Handy extensions to the Google Maps Android API.") |
| 80 | + url.set("https://github.com/googlemaps/android-maps-utils") |
| 81 | + scm { |
| 82 | + connection.set( "scm:[email protected]:googlemaps/android-maps-utils.git") |
| 83 | + developerConnection.set( "scm:[email protected]:googlemaps/android-maps-utils.git") |
| 84 | + url.set( "scm:[email protected]:googlemaps/android-maps-utils.git") |
| 85 | + } |
| 86 | + licenses { |
| 87 | + license { |
| 88 | + name.set("The Apache Software License, Version 2.0") |
| 89 | + url.set("http://www.apache.org/licenses/LICENSE-2.0.txt") |
| 90 | + distribution.set("repo") |
| 91 | + } |
| 92 | + } |
| 93 | + organization { |
| 94 | + name.set("Google Inc") |
| 95 | + url.set("http://developers.google.com/maps") |
| 96 | + } |
| 97 | + developers { |
| 98 | + developer { |
| 99 | + name.set("Google Inc.") |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + repositories { |
| 106 | + maven { |
| 107 | + val releasesRepoUrl = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/") |
| 108 | + val snapshotsRepoUrl = uri("https://oss.sonatype.org/content/repositories/snapshots/") |
| 109 | + url = if (project.version.toString().endsWith("SNAPSHOT")) snapshotsRepoUrl else releasesRepoUrl |
| 110 | + credentials { |
| 111 | + username = project.findProperty("sonatypeToken") as String? |
| 112 | + password = project.findProperty("sonatypeTokenPassword") as String? |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + private fun Project.configureSigning() { |
| 120 | + configure<SigningExtension> { |
| 121 | + sign(extensions.getByType<PublishingExtension>().publications["aar"]) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments