使用 sbt-license-report 生成清单
解读
在国内金融、电商、车联网等合规场景下,第三方组件许可证审计已成为上线前的强制环节。sbt-license-report 是社区维护的 SBT 插件,可一键扫描 Compile / Test / Provided 全范围依赖,输出 CSV / HTML / JSON 三种格式,自动解析 POM 与 Ivy 元数据,对 Apache-2.0、MIT、BSD-3、GPL-3.0 等常见协议进行归一化,并标记 “禁止商业”(如 AGPL)与 “需披露源码”(如 GPL)的高风险项。面试官通过此题考察候选人是否具备 合规交付意识、SBT 插件生态熟悉度 以及 CI 集成落地能力。
知识点
- 插件坐标与版本:
"com.github.sbt" % "sbt-license-report" % "1.6.1" - 启用方式:项目级
project/plugins.sbt添加后,全局自动生效,无需修改主构建。 - 核心任务:
licenseReport(默认输出到target/license-report),licenseCheck(可配置白名单,构建阶段即失败)。 - 配置 DSL:
licenseReportTitle := "XXX 平台第三方许可证清单"licenseReportStyle := LicenseReportStyle.HtmllicenseConfigurations := Set(Compile, Test, Provided)licenseWhitelist := Seq("Apache-2.0", "MIT", "BSD-3-Clause")licenseFailOnForbidden := true
- 与 CI 集成:在 GitLab-CI / 阿里云效 / GitHub Actions 中,将
sbt licenseReport置于 test 阶段之后、制品发布之前,并将target/license-report/index.html作为 构建产物持久化,供法务审计。 - 多模块项目:需在 ThisBuild 作用域 统一配置,避免子项目重复扫描;可结合
Def.derive实现差异化白名单。 - 自定义解析:通过
licenseOverrides修正部分 Jar 缺失<license>标签的误差,例如
licenseOverrides += "org.typelevel" %% "cats-core" % "*" -> LicenseCategory.Apache - 与 sbt-native-packager 联动:在
Docker / stage之前生成清单,随镜像层交付,满足银保监 “开源组件可追溯” 监管要求。
答案
(1)在 project/plugins.sbt 新增
addSbtPlugin("com.github.sbt" % "sbt-license-report" % "1.6.1")
(2)在 build.sbt 根作用域配置
import sbtlicensereport.LicenseReport
import sbtlicensereport.license.LicenseReportStyle
ThisBuild / licenseReportTitle := "XX 平台第三方许可证清单"
ThisBuild / licenseReportStyle := LicenseReportStyle.Html
ThisBuild / licenseConfigurations := Set(Compile, Test, Provided)
ThisBuild / licenseWhitelist := Seq("Apache-2.0", "MIT", "BSD-3-Clause")
ThisBuild / licenseFailOnForbidden := true
(3)本地验证
sbt clean licenseReport
成功后可在 target/license-report/index.html 查看明细,红色高亮项即为合规风险。
(4)CI 集成示例(GitHub Actions)
- name: License Audit
run: sbt licenseReport
- name: Upload Report
uses: actions/upload-artifact@v3
with:
name: license-report
path: target/license-report/
(5)若出现 GPL-3.0 被禁止,可:
- 升级依赖到 Apache 版本;
- 或采用 “隔离进程 + 进程通信” 方式规避 GPL 传染;
- 并在
licenseOverrides中显式声明 “经法务评估,已隔离” 备查。
拓展思考
- 如何对 自定义公司私服 中的 Jar 补充许可证?
可在私服 POM 中补齐<licenses>段,或在licenseOverrides中批量映射,避免每次手动修正。 - 当项目采用 sbt 1.9+ 的 Dependency Submission 功能时,可将许可证清单 自动推送至 GitHub Dependency Graph,实现 “代码-依赖-许可证” 三维可视化,方便 安全团队 做 CVE 与合规双重审计。
- 对于 Spark/Flink 超大型运行时,依赖数量常超 500+,建议:
- 使用
licenseFilter剔除Test与Provided中 不会随包发布的组件,减少噪音; - 将
licenseReport输出为 JSON 格式,再由 内部合规平台 解析入库,自动生成 SBOM(软件物料清单),满足 《网络安全审查办法》 对关键信息基础设施的要求。
- 使用