|
17 | 17 | * under the License.
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -import java.time.ZoneOffset |
| 20 | +import com.github.jk1.license.ProjectData |
| 21 | +import com.github.jk1.license.render.ReportRenderer |
| 22 | +import java.io.FileWriter |
21 | 23 |
|
22 | 24 | plugins {
|
23 | 25 | java
|
24 | 26 | checkstyle
|
25 | 27 | `maven-publish`
|
| 28 | + id("com.github.jk1.dependency-license-report") version "1.17" |
26 | 29 | }
|
27 | 30 |
|
28 | 31 | java {
|
@@ -144,3 +147,55 @@ dependencies {
|
144 | 147 | testImplementation("org.testcontainers", "testcontainers", "1.15.3")
|
145 | 148 | testImplementation("org.testcontainers", "elasticsearch", "1.15.3")
|
146 | 149 | }
|
| 150 | + |
| 151 | + |
| 152 | +licenseReport { |
| 153 | + renderers = arrayOf(SpdxReporter(File(rootProject.buildDir, "release/dependencies.csv"))) |
| 154 | + excludeGroups = arrayOf("org.elasticsearch.client") |
| 155 | +} |
| 156 | + |
| 157 | +class SpdxReporter(val dest: File) : ReportRenderer { |
| 158 | + // License names to their SPDX identifier |
| 159 | + val spdxIds = mapOf( |
| 160 | + "Apache License, Version 2.0" to "Apache-2.0", |
| 161 | + "The Apache Software License, Version 2.0" to "Apache-2.0", |
| 162 | + "BSD Zero Clause License" to "0BSD", |
| 163 | + "Eclipse Public License 2.0" to "EPL-2.0", |
| 164 | + "Eclipse Public License v. 2.0" to "EPL-2.0", |
| 165 | + "GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0", |
| 166 | + "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0" |
| 167 | + ) |
| 168 | + |
| 169 | + private fun quote(str: String) : String { |
| 170 | + return if (str.contains(',') || str.contains("\"")) { |
| 171 | + "\"" + str.replace("\"", "\"\"") + "\"" |
| 172 | + } else { |
| 173 | + str |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + override fun render(data: ProjectData?) { |
| 178 | + dest.parentFile.mkdirs() |
| 179 | + FileWriter(dest).use { out -> |
| 180 | + out.append("name,url,version,revision,license\n") |
| 181 | + data?.allDependencies?.forEach { dep -> |
| 182 | + val info = com.github.jk1.license.render.LicenseDataCollector.multiModuleLicenseInfo(dep) |
| 183 | + |
| 184 | + val depVersion = dep.version |
| 185 | + val depName = dep.group + ":" + dep.name |
| 186 | + val depUrl = info.moduleUrls.first() |
| 187 | + |
| 188 | + val licenseIds = info.licenses.mapNotNull { license -> |
| 189 | + license.name?.let { |
| 190 | + checkNotNull(spdxIds[it]) { "No SPDX identifier for $license" } |
| 191 | + } |
| 192 | + }.toSet() |
| 193 | + |
| 194 | + // Combine multiple licenses. |
| 195 | + // See https://spdx.github.io/spdx-spec/appendix-IV-SPDX-license-expressions/#composite-license-expressions |
| 196 | + val licenseId = licenseIds.joinToString(" OR ") |
| 197 | + out.append("${quote(depName)},${quote(depUrl)},${quote(depVersion)},,${quote(licenseId)}\n") |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments