步骤
1. 生成 Cargo.lock
bash
cargo generate-lockfile
或者直接:
bash
cargo check
这步需要能访问 crates.io(和 git 仓库)。生成后会得到 Cargo.lock。
2. Vendor 源码
bash
cargo vendor --versioned-dirs > vendor.toml
会创建 vendor/ 目录(所有依赖源码)和 vendor.toml(映射表)。
3. 配置 .cargo/config.toml 指向 vendor 目录
在项目根目录的 .cargo/config.toml 中加入:
toml
[source]
registry = "sparse+https://index.crates.io/"
# 本地 vendor 映射
[registries.crates-io]
replace-with = "vendored-sources"
toml
[source.vendored-sources]
directory = "vendor"
或者直接覆盖默认 source:
toml
[source.crates-io]
replace-with = "vendored-sources"
toml
[source.vendored-sources]
directory = "vendor"
4. 把整个项目拷到内网
需要拷贝的内容:
- 项目源码
- vendor/ 目录
- .cargo/config.toml
- Cargo.lock
5. 内网机器上构建
bash
cargo build --release
此时 cargo 不会联网,完全从 vendor/ 读取源码。