|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Dremio |
| 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 | +import groovy.util.Node |
| 18 | +import groovy.util.NodeList |
| 19 | +import javax.inject.Inject |
| 20 | +import org.gradle.api.GradleException |
| 21 | +import org.gradle.api.Plugin |
| 22 | +import org.gradle.api.Project |
| 23 | +import org.gradle.api.artifacts.Configuration |
| 24 | +import org.gradle.api.artifacts.ConfigurationVariant |
| 25 | +import org.gradle.api.artifacts.ProjectDependency |
| 26 | +import org.gradle.api.artifacts.SelfResolvingDependency |
| 27 | +import org.gradle.api.artifacts.component.ModuleComponentSelector |
| 28 | +import org.gradle.api.artifacts.result.DependencyResult |
| 29 | +import org.gradle.api.attributes.Bundling |
| 30 | +import org.gradle.api.attributes.Category |
| 31 | +import org.gradle.api.attributes.LibraryElements |
| 32 | +import org.gradle.api.attributes.Usage |
| 33 | +import org.gradle.api.component.SoftwareComponentFactory |
| 34 | +import org.gradle.api.plugins.JavaBasePlugin |
| 35 | +import org.gradle.api.provider.Property |
| 36 | +import org.gradle.api.publish.PublishingExtension |
| 37 | +import org.gradle.api.publish.maven.MavenPublication |
| 38 | +import org.gradle.api.publish.maven.plugins.MavenPublishPlugin |
| 39 | +import org.gradle.api.publish.tasks.GenerateModuleMetadata |
| 40 | +import org.gradle.api.tasks.PathSensitivity |
| 41 | +import org.gradle.kotlin.dsl.configure |
| 42 | +import org.gradle.kotlin.dsl.extra |
| 43 | +import org.gradle.kotlin.dsl.provideDelegate |
| 44 | +import org.gradle.kotlin.dsl.register |
| 45 | +import org.gradle.kotlin.dsl.withType |
| 46 | +import org.gradle.plugins.signing.SigningExtension |
| 47 | +import org.gradle.plugins.signing.SigningPlugin |
| 48 | + |
| 49 | +/** Applies common configurations to all Nessie projects. */ |
| 50 | +@Suppress("unused") |
| 51 | +class PublishingHelperPlugin |
| 52 | +@Inject |
| 53 | +constructor(private val softwareComponentFactory: SoftwareComponentFactory) : Plugin<Project> { |
| 54 | + override fun apply(project: Project): Unit = |
| 55 | + project.run { |
| 56 | + extensions.create("publishingHelper", PublishingHelperExtension::class.java) |
| 57 | + |
| 58 | + plugins.withType<MavenPublishPlugin>().configureEach { |
| 59 | + configure<PublishingExtension> { |
| 60 | + publications { |
| 61 | + register<MavenPublication>("maven") { |
| 62 | + val mavenPublication = this |
| 63 | + afterEvaluate { |
| 64 | + // This MUST happen in an 'afterEvaluate' to ensure that the Shadow*Plugin has |
| 65 | + // been applied. |
| 66 | + from(components.firstOrNull { c -> c.name == "javaPlatform" || c.name == "java" }) |
| 67 | + |
| 68 | + suppressPomMetadataWarningsFor("testApiElements") |
| 69 | + suppressPomMetadataWarningsFor("testJavadocElements") |
| 70 | + suppressPomMetadataWarningsFor("testRuntimeElements") |
| 71 | + suppressPomMetadataWarningsFor("testSourcesElements") |
| 72 | + |
| 73 | + mavenPublication.groupId = "$group" |
| 74 | + mavenPublication.version = project.version.toString() |
| 75 | + } |
| 76 | + |
| 77 | + tasks.named("generatePomFileForMavenPublication") { |
| 78 | + val e = rootProject.extensions.getByType(PublishingHelperExtension::class.java) |
| 79 | + |
| 80 | + pom { |
| 81 | + name.set( |
| 82 | + project.provider { |
| 83 | + if (project.extra.has("maven.name")) { |
| 84 | + project.extra["maven.name"].toString() |
| 85 | + } else { |
| 86 | + project.name |
| 87 | + } |
| 88 | + } |
| 89 | + ) |
| 90 | + description.set(project.description) |
| 91 | + |
| 92 | + val nessieRepoName = e.nessieRepoName.get() |
| 93 | + |
| 94 | + inputs |
| 95 | + .file(rootProject.file("gradle/developers.csv")) |
| 96 | + .withPathSensitivity(PathSensitivity.RELATIVE) |
| 97 | + inputs |
| 98 | + .file(rootProject.file("gradle/contributors.csv")) |
| 99 | + .withPathSensitivity(PathSensitivity.RELATIVE) |
| 100 | + doFirst { |
| 101 | + inceptionYear.set(e.inceptionYear.get()) |
| 102 | + url.set("https://github.com/projectnessie/$nessieRepoName") |
| 103 | + organization { |
| 104 | + name.set("Project Nessie") |
| 105 | + url.set("https://projectnessie.org") |
| 106 | + } |
| 107 | + licenses { |
| 108 | + license { |
| 109 | + name.set("The Apache License, Version 2.0") |
| 110 | + url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") |
| 111 | + } |
| 112 | + } |
| 113 | + mailingLists { |
| 114 | + mailingList { |
| 115 | + name.set("Project Nessie List") |
| 116 | + subscribe.set( "[email protected]") |
| 117 | + unsubscribe.set( "[email protected]") |
| 118 | + |
| 119 | + archive.set("https://groups.google.com/g/projectnessie") |
| 120 | + } |
| 121 | + } |
| 122 | + scm { |
| 123 | + connection.set("scm:git:https://github.com/projectnessie/$nessieRepoName") |
| 124 | + developerConnection.set( |
| 125 | + "scm:git:https://github.com/projectnessie/$nessieRepoName" |
| 126 | + ) |
| 127 | + url.set("https://github.com/projectnessie/$nessieRepoName/tree/main") |
| 128 | + tag.set("main") |
| 129 | + } |
| 130 | + issueManagement { |
| 131 | + system.set("Github") |
| 132 | + url.set("https://github.com/projectnessie/$nessieRepoName/issues") |
| 133 | + } |
| 134 | + developers { |
| 135 | + file(rootProject.file("gradle/developers.csv")) |
| 136 | + .readLines() |
| 137 | + .map { line -> line.trim() } |
| 138 | + .filter { line -> line.isNotEmpty() && !line.startsWith("#") } |
| 139 | + .forEach { line -> |
| 140 | + val args = line.split(",") |
| 141 | + if (args.size < 3) { |
| 142 | + throw GradleException( |
| 143 | + "gradle/developers.csv contains invalid line '${line}'" |
| 144 | + ) |
| 145 | + } |
| 146 | + developer { |
| 147 | + id.set(args[0]) |
| 148 | + name.set(args[1]) |
| 149 | + url.set(args[2]) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + contributors { |
| 154 | + file(rootProject.file("gradle/contributors.csv")) |
| 155 | + .readLines() |
| 156 | + .map { line -> line.trim() } |
| 157 | + .filter { line -> line.isNotEmpty() && !line.startsWith("#") } |
| 158 | + .forEach { line -> |
| 159 | + val args = line.split(",") |
| 160 | + if (args.size > 2) { |
| 161 | + throw GradleException( |
| 162 | + "gradle/contributors.csv contains invalid line '${line}'" |
| 163 | + ) |
| 164 | + } |
| 165 | + contributor { |
| 166 | + name.set(args[0]) |
| 167 | + url.set(args[1]) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + // Gradle complains when a Gradle module metadata ("pom on steroids") is generated with an |
| 180 | + // enforcedPlatform() dependency - but Quarkus requires enforcedPlatform(), so we have to |
| 181 | + // allow it. |
| 182 | + tasks.withType<GenerateModuleMetadata>().configureEach { |
| 183 | + suppressedValidationErrors.add("enforced-platform") |
| 184 | + } |
| 185 | + |
| 186 | + if (project.hasProperty("release")) { |
| 187 | + plugins.withType<SigningPlugin>().configureEach { |
| 188 | + configure<SigningExtension> { |
| 189 | + val signingKey: String? by project |
| 190 | + val signingPassword: String? by project |
| 191 | + useInMemoryPgpKeys(signingKey, signingPassword) |
| 192 | + val publishing = project.extensions.getByType(PublishingExtension::class.java) |
| 193 | + afterEvaluate { sign(publishing.publications.getByName("maven")) } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private fun xmlNode(node: Node?, child: String): Node? { |
| 200 | + val found = node?.get(child) |
| 201 | + if (found is NodeList) { |
| 202 | + if (found.isNotEmpty()) { |
| 203 | + return found[0] as Node |
| 204 | + } |
| 205 | + } |
| 206 | + return null |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +abstract class PublishingHelperExtension { |
| 211 | + abstract val nessieRepoName: Property<String> |
| 212 | + abstract val inceptionYear: Property<String> |
| 213 | +} |
0 commit comments