|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package publishing |
| 21 | + |
| 22 | +import org.gradle.api.DefaultTask |
| 23 | +import org.gradle.api.Project |
| 24 | +import org.gradle.api.file.DirectoryProperty |
| 25 | +import org.gradle.api.plugins.JavaLibraryPlugin |
| 26 | +import org.gradle.api.provider.ListProperty |
| 27 | +import org.gradle.api.tasks.CacheableTask |
| 28 | +import org.gradle.api.tasks.Input |
| 29 | +import org.gradle.api.tasks.OutputDirectory |
| 30 | +import org.gradle.api.tasks.SourceSetContainer |
| 31 | +import org.gradle.api.tasks.Sync |
| 32 | +import org.gradle.api.tasks.TaskAction |
| 33 | +import org.gradle.kotlin.dsl.provideDelegate |
| 34 | + |
| 35 | +@CacheableTask |
| 36 | +abstract class GeneratePomProperties : DefaultTask() { |
| 37 | + @Suppress("unused") @get:Input abstract val pomInputs: ListProperty<String> |
| 38 | + |
| 39 | + @get:OutputDirectory abstract val destinationDir: DirectoryProperty |
| 40 | + |
| 41 | + init { |
| 42 | + pomInputs.convention(listOf(project.group.toString(), project.name, project.version.toString())) |
| 43 | + destinationDir.convention(project.layout.buildDirectory.dir("generated/pom-properties")) |
| 44 | + } |
| 45 | + |
| 46 | + @TaskAction |
| 47 | + fun generate() { |
| 48 | + val buildDir = destinationDir.get().asFile |
| 49 | + buildDir.deleteRecursively() |
| 50 | + val targetDir = buildDir.resolve("META-INF/maven/${project.group}/${project.name}") |
| 51 | + targetDir.mkdirs() |
| 52 | + targetDir |
| 53 | + .resolve("pom.properties") |
| 54 | + .writeText( |
| 55 | + """ |
| 56 | + # Generated by the Apache Polaris build. |
| 57 | + groupId=${project.group} |
| 58 | + artifactId=${project.name} |
| 59 | + version=${project.version} |
| 60 | + """ |
| 61 | + .trimIndent() |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Adds convenient, but not strictly necessary information to each generated "main" jar. |
| 68 | + * |
| 69 | + * This includes `pom.properties` and `pom.xml` files where Maven places those, in |
| 70 | + * `META-INF/maven/group-id/artifact-id/`. Also adds the `NOTICE` and `LICENSE` files in `META-INF`, |
| 71 | + * which makes it easier for license scanners. |
| 72 | + */ |
| 73 | +fun addAdditionalJarContent(project: Project): Unit = |
| 74 | + project.run { |
| 75 | + project.plugins.withType(JavaLibraryPlugin::class.java) { |
| 76 | + val generatePomProperties = |
| 77 | + tasks.register("generatePomProperties", GeneratePomProperties::class.java) {} |
| 78 | + |
| 79 | + val additionalJarContent = |
| 80 | + tasks.register("additionalJarContent", Sync::class.java) { |
| 81 | + // Have to manually declare the inputs of this task here on top of the from/include below |
| 82 | + inputs.files(rootProject.layout.files("LICENSE", "NOTICE")) |
| 83 | + inputs.property("GAV", "${project.group}:${project.name}:${project.version}") |
| 84 | + dependsOn("generatePomFileForMavenPublication") |
| 85 | + from(rootProject.rootDir) { |
| 86 | + include("LICENSE", "NOTICE") |
| 87 | + eachFile { |
| 88 | + this.path = |
| 89 | + "META-INF/licenses/${project.group}/${project.name}-${project.version}/$sourceName" |
| 90 | + } |
| 91 | + } |
| 92 | + from(tasks.named("generatePomFileForMavenPublication")) { |
| 93 | + include("pom-default.xml") |
| 94 | + eachFile { this.path = "META-INF/maven/${project.group}/${project.name}/pom.xml" } |
| 95 | + } |
| 96 | + into(layout.buildDirectory.dir("license-for-jar")) |
| 97 | + } |
| 98 | + |
| 99 | + tasks.named("processResources") { dependsOn("additionalJarContent") } |
| 100 | + |
| 101 | + val sourceSets: SourceSetContainer by project |
| 102 | + sourceSets.named("main") { |
| 103 | + resources.srcDir(additionalJarContent) |
| 104 | + resources.srcDir(generatePomProperties) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
0 commit comments