|
| 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 java.util.Base64 |
| 21 | +import org.cyclonedx.gradle.CyclonedxDirectTask |
| 22 | +import org.cyclonedx.model.AttachmentText |
| 23 | +import org.cyclonedx.model.Component.Type.APPLICATION |
| 24 | +import org.cyclonedx.model.License |
| 25 | +import org.cyclonedx.model.LicenseChoice |
| 26 | +import org.cyclonedx.model.Property |
| 27 | +import sbom.CyclonedxBundleTask |
| 28 | +import sbom.createCyclonedxConfigurations |
| 29 | + |
| 30 | +val cyclonedxApplicationBomTask = |
| 31 | + tasks.register<CyclonedxBundleTask>("cyclonedxApplicationBom") { |
| 32 | + group = "publishing" |
| 33 | + description = "Generate CycloneDX SBOMs for this as an application" |
| 34 | + |
| 35 | + val cyclonedxDirectBom = tasks.named<CyclonedxDirectTask>("cyclonedxDirectBom") |
| 36 | + inputBoms.from(cyclonedxDirectBom.map { it.jsonOutput }) |
| 37 | + |
| 38 | + // want to include the original license text in the generated SBOM as it may include variations |
| 39 | + // from the "standard" license text |
| 40 | + includeLicenseText = true |
| 41 | + |
| 42 | + projectType = APPLICATION |
| 43 | + |
| 44 | + // Needed for projects that use subdirectories in their build/ directory, like the Spark plugin |
| 45 | + jsonOutput.set(project.layout.buildDirectory.file("reports/cyclonedx-app/bom.json")) |
| 46 | + xmlOutput.set(project.layout.buildDirectory.file("reports/cyclonedx-app/bom.xml")) |
| 47 | + |
| 48 | + val relativeProjectDir = project.projectDir.relativeTo(project.rootProject.projectDir) |
| 49 | + val gitInfo = GitInfo.memoized(project) |
| 50 | + |
| 51 | + licenseChoice.set( |
| 52 | + LicenseChoice().apply { |
| 53 | + addLicense( |
| 54 | + License().apply { |
| 55 | + val gitCommit = GitInfo.memoized(project).gitHead |
| 56 | + id = "Apache-2.0" |
| 57 | + // TODO URL or text ?? |
| 58 | + url = gitInfo.rawGithubLink("$relativeProjectDir/distribution/LICENSE") |
| 59 | + setLicenseText( |
| 60 | + AttachmentText().apply() { |
| 61 | + contentType = "plain/text" |
| 62 | + encoding = "base64" |
| 63 | + text = |
| 64 | + Base64.getEncoder() |
| 65 | + .encodeToString(project.file("distribution/LICENSE").readBytes()) |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + // TODO Is there a better way to include NOTICE + DISCLAIMER in a CycloneDX SBOM? |
| 70 | + val props = mutableListOf<Property>() |
| 71 | + props.add( |
| 72 | + Property().apply { |
| 73 | + name = "NOTICE" |
| 74 | + value = |
| 75 | + project.file("distribution/NOTICE").readText(Charsets.UTF_8).replace("\n", "\\n") |
| 76 | + } |
| 77 | + ) |
| 78 | + val disclaimerFile = project.file("distribution/DISCLAIMER") |
| 79 | + if (disclaimerFile.isFile) { |
| 80 | + props.add( |
| 81 | + Property().apply { |
| 82 | + name = "DISCLAIMER" |
| 83 | + value = disclaimerFile.readText(Charsets.UTF_8).replace("\n", "\\n") |
| 84 | + } |
| 85 | + ) |
| 86 | + } |
| 87 | + properties = props |
| 88 | + } |
| 89 | + ) |
| 90 | + } |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | +createCyclonedxConfigurations(project, cyclonedxApplicationBomTask) |
0 commit comments