|
| 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 | +import org.gradle.api.DefaultTask |
| 21 | +import org.gradle.api.GradleException |
| 22 | +import org.gradle.api.file.ArchiveOperations |
| 23 | +import org.gradle.api.file.RegularFileProperty |
| 24 | +import org.gradle.api.tasks.CacheableTask |
| 25 | +import org.gradle.api.tasks.InputFile |
| 26 | +import org.gradle.api.tasks.OutputFile |
| 27 | +import org.gradle.api.tasks.PathSensitive |
| 28 | +import org.gradle.api.tasks.PathSensitivity |
| 29 | +import org.gradle.api.tasks.TaskAction |
| 30 | +import javax.inject.Inject |
| 31 | + |
| 32 | +/** |
| 33 | + * Checks LICENSE/NOTICE coverage of bundled jars in a binary distribution tarball. |
| 34 | + * |
| 35 | + * Mirrors the behaviour of the legacy `src/check-binary-license.sh`: |
| 36 | + * 1. Every bundled jar whose basename does not contain "org.apache.pulsar" |
| 37 | + * must appear as a substring of the LICENSE text. |
| 38 | + * 2. Every jar referenced from LICENSE must be bundled. |
| 39 | + * 3. Every jar referenced from NOTICE (except "checker-qual.jar") must be bundled. |
| 40 | + * |
| 41 | + * Cacheable + configuration-cache friendly: state is held only on inputs/outputs and the |
| 42 | + * injected `ArchiveOperations` service; the task action does not reach into the project. |
| 43 | + */ |
| 44 | +@CacheableTask |
| 45 | +abstract class CheckBinaryLicenseTask : DefaultTask() { |
| 46 | + |
| 47 | + @get:InputFile |
| 48 | + @get:PathSensitive(PathSensitivity.NONE) |
| 49 | + abstract val binaryDistribution: RegularFileProperty |
| 50 | + |
| 51 | + @get:OutputFile |
| 52 | + abstract val report: RegularFileProperty |
| 53 | + |
| 54 | + @get:Inject |
| 55 | + abstract val archiveOperations: ArchiveOperations |
| 56 | + |
| 57 | + @TaskAction |
| 58 | + fun check() { |
| 59 | + val tarFile = binaryDistribution.get().asFile |
| 60 | + val tarTree = archiveOperations.tarTree(tarFile) |
| 61 | + |
| 62 | + val licenseEntryRegex = Regex("^[^/]+/LICENSE$") |
| 63 | + val noticeEntryRegex = Regex("^[^/]+/NOTICE$") |
| 64 | + val nameExclusionSubstrings = listOf( |
| 65 | + "pulsar-client", |
| 66 | + "pulsar-cli-utils", |
| 67 | + "pulsar-common", |
| 68 | + "pulsar-package", |
| 69 | + "pulsar-websocket", |
| 70 | + "bouncy-castle-bc", |
| 71 | + ) |
| 72 | + |
| 73 | + val bundledJars = sortedSetOf<String>() |
| 74 | + var licenseContent: String? = null |
| 75 | + var noticeContent: String? = null |
| 76 | + |
| 77 | + tarTree.visit { |
| 78 | + if (isDirectory) return@visit |
| 79 | + val path = relativePath.pathString |
| 80 | + when { |
| 81 | + path.endsWith(".jar") -> { |
| 82 | + val inExcludedDir = path.contains("/examples/") || path.contains("/instances/") |
| 83 | + val nameExcluded = nameExclusionSubstrings.any { name.contains(it) } |
| 84 | + if (!inExcludedDir && !nameExcluded) { |
| 85 | + bundledJars.add(name) |
| 86 | + } |
| 87 | + } |
| 88 | + licenseEntryRegex.matches(path) -> licenseContent = file.readText() |
| 89 | + noticeEntryRegex.matches(path) -> noticeContent = file.readText() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + val license = licenseContent |
| 94 | + ?: throw GradleException("Could not find a top-level LICENSE entry in ${tarFile.name}") |
| 95 | + val notice = noticeContent |
| 96 | + ?: throw GradleException("Could not find a top-level NOTICE entry in ${tarFile.name}") |
| 97 | + |
| 98 | + val licenseJars = extractJarReferences(license) |
| 99 | + val noticeJars = extractJarReferences(notice) |
| 100 | + |
| 101 | + val errors = mutableListOf<String>() |
| 102 | + |
| 103 | + // Check 1: every bundled non-pulsar jar must appear as a substring of LICENSE. |
| 104 | + for (jar in bundledJars) { |
| 105 | + if (jar.contains("org.apache.pulsar")) continue |
| 106 | + if (!license.contains(jar)) { |
| 107 | + errors.add("$jar unaccounted for in LICENSE") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Check 2: every jar mentioned in LICENSE must be bundled. |
| 112 | + // Reference may contain wildcards like "org.rocksdb.*.jar"; treat it as a regex |
| 113 | + // to match the legacy bash `grep -q $J` semantics. |
| 114 | + for (jar in licenseJars) { |
| 115 | + val pattern = Regex(jar) |
| 116 | + if (bundledJars.none { pattern.containsMatchIn(it) }) { |
| 117 | + errors.add("$jar mentioned in LICENSE, but not bundled") |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Check 3: every jar mentioned in NOTICE (except checker-qual.jar) must be bundled. |
| 122 | + for (jar in noticeJars) { |
| 123 | + if (jar == "checker-qual.jar") continue |
| 124 | + val pattern = Regex(jar) |
| 125 | + if (bundledJars.none { pattern.containsMatchIn(it) }) { |
| 126 | + errors.add("$jar mentioned in NOTICE, but not bundled") |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + val reportFile = report.get().asFile |
| 131 | + reportFile.parentFile.mkdirs() |
| 132 | + reportFile.writeText(buildReport(tarFile.name, bundledJars, licenseJars, noticeJars, errors)) |
| 133 | + |
| 134 | + if (errors.isNotEmpty()) { |
| 135 | + errors.forEach { logger.error(it) } |
| 136 | + throw GradleException( |
| 137 | + "LICENSE/NOTICE check failed for ${tarFile.name}: ${errors.size} issue(s). " + |
| 138 | + "See report at ${reportFile.absolutePath}", |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private fun extractJarReferences(content: String): List<String> { |
| 144 | + val jarRegex = Regex(""".* (.*\.jar).*""") |
| 145 | + return content.lines().mapNotNull { line -> jarRegex.matchEntire(line)?.groupValues?.get(1) } |
| 146 | + } |
| 147 | + |
| 148 | + private fun buildReport( |
| 149 | + tarballName: String, |
| 150 | + bundledJars: Set<String>, |
| 151 | + licenseJars: List<String>, |
| 152 | + noticeJars: List<String>, |
| 153 | + errors: List<String>, |
| 154 | + ): String = buildString { |
| 155 | + appendLine("Binary license check report for $tarballName") |
| 156 | + appendLine("Bundled jars: ${bundledJars.size}") |
| 157 | + appendLine("Jars referenced in LICENSE: ${licenseJars.size}") |
| 158 | + appendLine("Jars referenced in NOTICE: ${noticeJars.size}") |
| 159 | + appendLine() |
| 160 | + if (errors.isEmpty()) { |
| 161 | + appendLine("Result: OK") |
| 162 | + } else { |
| 163 | + appendLine("Result: FAILED (${errors.size} issue(s))") |
| 164 | + errors.forEach { appendLine(" - $it") } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments