Rust许可证管理工具cargo-bundle-licenses的使用,自动收集和打包项目依赖的开源许可证文件
Rust许可证管理工具cargo-bundle-licenses的使用,自动收集和打包项目依赖的开源许可证文件
简介
cargo-bundle-licenses是一个Rust工具,用于将所有第三方许可证打包到一个文件中。
注意:该工具不能保证许可证选择的正确性。它依赖于包元数据中提供的信息,这些信息可能不准确。对于正式场景,建议手动检查和验证所有许可证。
安装
cargo install cargo-bundle-licenses
使用方法
典型的使用场景如下:
- 生成初始的许可证捆绑文件:
cargo bundle-licenses --format yaml --output THIRDPARTY.yml
-
检查列出的警告信息,跟踪无法找到的许可证,并将许可证文本粘贴到"THIRDPARTY.yml"文件中。
- 如果许可证本应被
cargo-bundle-licenses
找到,请创建问题或提交拉取请求。
- 如果许可证本应被
-
在CI中,运行以下命令检查更改并在发现差异时失败:
cargo bundle-licenses --format yaml --output CI.yaml --previous THIRDPARTY.yml --check-previous
支持的格式
目前支持的格式有json
、yaml
和toml
。
常见警告及解决方法
最常见的问题是工作区没有include
转发其许可证文件。需要去工作区的仓库复制相关文件。
与其他工具的区别
cargo-about
:也查找并聚合所有依赖项的许可证文本。cargo-bundle-licenses
更专注于生成内容并允许手动更新未找到的许可证。cargo-license
:仅生成Cargo.toml
文件中找到的许可证列表。cargo-deny
:收集依赖项的许可证列表,但目的是警告或错误提示不可接受的许可证,不收集许可证文本。
完整示例
// 此示例展示如何在Cargo.toml中添加cargo-bundle-licenses作为依赖
[dependencies]
cargo-bundle-licenses = "4.0.0"
// 在build.rs中使用
fn main() {
// 生成YAML格式的许可证文件
std::process::Command::new("cargo")
.arg("bundle-licenses")
.arg("--format")
.arg("yaml")
.arg("--output")
.arg("THIRDPARTY.yml")
.status()
.expect("Failed to bundle licenses");
}
# 在CI中检查许可证变更的示例脚本
#!/bin/bash
# 生成新的许可证文件
cargo bundle-licenses --format yaml --output CI.yaml --previous THIRDPARTY.yml --check-previous
# 如果有差异则失败
if [ $? -ne 0 ]; then
echo "License changes detected! Please update THIRDPARTY.yml"
exit 1
fi
完整示例demo
下面是一个完整的项目示例,展示如何在实际项目中使用cargo-bundle-licenses:
- 首先创建项目结构:
my_project/
├── Cargo.toml
├── build.rs
├── src/
│ └── main.rs
└── licenses/
└── THIRDPARTY.yml
- Cargo.toml内容:
[package]
name = "my_project"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = "1.0"
serde_json = "1.0"
[build-dependencies]
cargo-bundle-licenses = "4.0.0"
- build.rs内容:
fn main() {
// 生成YAML格式的许可证文件
let status = std::process::Command::new("cargo")
.arg("bundle-licenses")
.arg("--format")
.arg("yaml")
.arg("--output")
.arg("licenses/THIRDPARTY.yml")
.status()
.expect("Failed to execute command");
if !status.success() {
panic!("Failed to bundle licenses");
}
}
- CI检查脚本(ci_check_licenses.sh):
#!/bin/bash
set -e
# 生成临时许可证文件用于比较
cargo bundle-licenses --format yaml --output CI.yaml --previous licenses/THIRDPARTY.yml --check-previous
# 检查命令返回值
if [ $? -ne 0 ]; then
echo "错误:检测到许可证变更!请更新licenses/THIRDPARTY.yml文件"
exit 1
fi
echo "许可证检查通过"
- 在项目根目录运行:
# 首次生成许可证文件
cargo build
# 在CI中检查许可证变更
./ci_check_licenses.sh
这个完整示例展示了如何在项目构建过程中自动生成许可证文件,并在CI流程中进行验证,确保项目的许可证信息始终保持最新。
1 回复
Rust许可证管理工具 cargo-bundle-licenses 使用指南
工具介绍
cargo-bundle-licenses
是一个用于自动收集和打包 Rust 项目所有依赖项的开源许可证文件的工具。它可以帮助开发者轻松满足开源许可证合规要求,特别适合需要分发软件或发布商业产品的项目。
安装方法
cargo install cargo-bundle-licenses
基本使用方法
1. 生成许可证报告
cargo bundle-licenses
这会扫描项目的所有依赖项,并在项目根目录下生成一个 licenses
文件夹,包含所有依赖的许可证文件。
2. 指定输出格式
cargo bundle-licenses --format json
支持多种输出格式:
json
(默认)text
markdown
html
3. 自定义输出目录
cargo bundle-licenses --output-dir ./dist/licenses
高级用法
1. 包含开发依赖
cargo bundle-licenses --dev
2. 生成许可证摘要
cargo bundle-licenses --summary
这会生成一个包含所有许可证类型的摘要文件。
3. 排除特定许可证
cargo bundle-licenses --exclude MIT --exclude Apache-2.0
集成到Cargo.toml
你可以在 Cargo.toml
中添加配置:
[package.metadata.bundle-licenses]
format = "markdown"
output = "THIRD-PARTY-LICENSES.md"
示例输出结构
运行后生成的目录结构示例:
licenses/
├── LICENSE-APACHE
├── LICENSE-MIT
├── LICENSE-BSD
├── summary.json
└── THIRD-PARTY-LICENSES.md
CI/CD集成示例
在GitHub Actions中的使用示例:
- name: Bundle licenses
run: cargo bundle-licenses --format markdown --output THIRD-PARTY-LICENSES.md
注意事项
- 工具会尝试自动检测每个依赖项的许可证类型,但建议人工复核
- 某些特殊许可证可能需要手动处理
- 对于商业项目,建议咨询法律专业人士确保合规
这个工具大大简化了Rust项目的许可证合规工作,是发布软件前的重要步骤。
完整示例
- 首先安装工具:
cargo install cargo-bundle-licenses
- 创建一个新的Rust项目:
cargo new demo_project
cd demo_project
- 添加一些依赖到Cargo.toml:
[dependencies]
serde = "1.0"
tokio = { version = "1.0", features = ["full"] }
reqwest = "0.11"
- 生成许可证报告:
cargo bundle-licenses --format markdown --output LICENSES.md
- 检查生成的许可证文件:
ls licenses/
cat LICENSES.md
- 集成到CI/CD的完整示例(GitHub Actions):
name: License Compliance Check
on: [push, pull_request]
jobs:
license-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
profile: minimal
override: true
- name: Bundle licenses
run: |
cargo install cargo-bundle-licenses
cargo bundle-licenses --format markdown --output THIRD-PARTY-LICENSES.md
cat THIRD-PARTY-LICENSES.md
- name: Upload licenses
uses: actions/upload-artifact@v2
with:
name: licenses
path: |
licenses/
THIRD-PARTY-LICENSES.md
- 在项目根目录添加许可证配置:
[package.metadata.bundle-licenses]
format = "markdown"
output = "THIRD-PARTY-LICENSES.md"
exclude = ["Unlicense"] # 排除特定许可证
- 检查许可证摘要:
cargo bundle-licenses --summary
- 最终项目结构示例:
demo_project/
├── Cargo.toml
├── src/
├── licenses/
│ ├── LICENSE-APACHE
│ ├── LICENSE-MIT
│ └── summary.json
└── THIRD-PARTY-LICENSES.md