cargo 解决git依赖私库办法

Rust实际场景,不仅需要依赖https://crates.io/的公共mod,自己依赖的git私库服务也是常见现象。
cargo正好也解决了私库依赖的问题。详细说明见链接:
https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories

cargo 解决git依赖私库办法_第1张图片
image.png

cargo git私库解决办法

    1. 添加.ssh/config配置
Host *
   UseKeychain yes
   AddKeysToAgent yes
   IdentityFile ~/.ssh/id_rsa
    1. 指定私钥文件
ssh-add -K ~/.ssh/id_rsa
    1. cargo 的toml文件添加依赖,如下
[package]
name = "demo_rust"
version = "0.1.0"
authors = ["baoyachi "]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
json               = "0.4"
pretty_env_logger = "0.3"
serde             = "1.0"
common-utils = { git = "ssh://[email protected]/common/common-utils.git", tag = "v0.0.1" }
common-log = { git = "ssh://[email protected]/common/common-log.git", branch = "master" }

这里的 common-utils common-log 就是私库的具体依赖。

common-utils = { git = "ssh://[email protected]/common/common-utils.git", tag = "v0.0.1" }
common-log = { git = "ssh://[email protected]/common/common-log.git", branch = "master" }
  • 这里的git使用ssh方式访问

  • tag :表示当前git上的tag

  • branch :表示当前git的 分支号
    通常,我们会采用tag对依赖的git代码管理;branch的缺点无法lock代码。

    1. 执行cargo build
      正常情况下,cargo build可以正常下载crates.io和git私库代码

你可能感兴趣的:(cargo 解决git依赖私库办法)