Golang包管理工具之govendor的使用
轉(zhuǎn)載:https://blog.csdn.net/huwh_/article/details/77169858
介紹
golang工程的依賴包經(jīng)常使用go get命令來獲取迎捺,例如:go get github.com/kardianos/govendor ,會將依賴包下載到GOPATH的路徑下揍障。
常用的依賴包管理工具有g(shù)odep肮之,govendor等择镇,在Golang1.5之后累铅,Go提供了 GO15VENDOREXPERIMENT 環(huán)境變量(Go 1.6版本默認開啟該環(huán)境變量),用于將go build時的應(yīng)用路徑搜索調(diào)整成為 當(dāng)前項目目錄/vendor 目錄方式眷篇。通過這種形式愤惰,我們可以實現(xiàn)類似于 godep 方式的項目依賴管理苇经。
安裝使用
安裝
go get -u -v github.com/kardianos/govendor
使用
#進入到項目目錄
cd /home/gopath/src/mytool
#初始化vendor目錄
govendor init
#查看vendor目錄
[root@CC54425A mytool]# ls
commands main.go vendor mytool_test.sh
#將GOPATH中本工程使用到的依賴包自動移動到vendor目錄中
#說明:如果本地GOPATH沒有依賴包,先go get相應(yīng)的依賴包
govendor add +external
或使用縮寫: govendor add +e
#Go 1.6以上版本默認開啟 GO15VENDOREXPERIMENT 環(huán)境變量宦言,可忽略該步驟扇单。
#通過設(shè)置環(huán)境變量 GO15VENDOREXPERIMENT=1 使用vendor文件夾構(gòu)建文件。
#可以選擇 export GO15VENDOREXPERIMENT=1 或 GO15VENDOREXPERIMENT=1 go build 執(zhí)行編譯
export GO15VENDOREXPERIMENT=1
說明
govendor只是用來管理項目的依賴包奠旺,如果GOPATH中本身沒有項目的依賴包蜘澜,則需要通過go get先下載到GOPATH中,再通過govendor add +external拷貝到vendor目錄中响疚。Go 1.6以上版本默認開啟GO15VENDOREXPERIMENT環(huán)境變量鄙信。
使用命令
[root@CC54425A mytool]# govendor
govendor (v1.0.8): record dependencies and copy into vendor folder
-govendor-licenses Show govendor's licenses.
-version Show govendor version
-cpuprofile 'file' Writes a CPU profile to 'file' for debugging.
-memprofile 'file' Writes a heap profile to 'file' for debugging.
Sub-Commands
init Create the "vendor" folder and the "vendor.json" file.
list List and filter existing dependencies and packages.
add Add packages from $GOPATH.
update Update packages from $GOPATH.
remove Remove packages from the vendor folder.
status Lists any packages missing, out-of-date, or modified locally.
fetch Add new or update vendor folder packages from remote repository.
sync Pull packages into vendor folder from remote repository with revisions
from vendor.json file.
migrate Move packages from a legacy tool to the vendor folder with metadata.
get Like "go get" but copies dependencies into a "vendor" folder.
license List discovered licenses for the given status or import paths.
shell Run a "shell" to make multiple sub-commands more efficient for large
projects.
go tool commands that are wrapped:
"+status" package selection may be used with them
fmt, build, install, clean, test, vet, generate, tool
Status Types
+local (l) packages in your project
+external (e) referenced packages in GOPATH but not in current project
+vendor (v) packages in the vendor folder
+std (s) packages in the standard library
+excluded (x) external packages explicitly excluded from vendoring
+unused (u) packages in the vendor folder, but unused
+missing (m) referenced packages but not found
+program (p) package is a main package
+outside +external +missing
+all +all packages
Status can be referenced by their initial letters.
Package specifier
<path>[::<origin>][{/...|/^}][@[<version-spec>]]
Ignoring files with build tags, or excluding packages from being vendored:
The "vendor.json" file contains a string field named "ignore".
It may contain a space separated list of build tags to ignore when
listing and copying files.
This list may also contain package prefixes (containing a "/", possibly
as last character) to exclude when copying files in the vendor folder.
If "foo/" appears in this field, then package "foo" and all its sub-packages
("foo/bar", …) will be excluded (but package "bar/foo" will not).
By default the init command adds the "test" tag to the ignore list.
If using go1.5, ensure GO15VENDOREXPERIMENT=1 is set.
vendor.json
govendor配置文件,記錄依賴包列表忿晕。
{
"comment": "",
"ignore": "test",
"package": [
{
"checksumSHA1": "uGalSICR4r7354vvKuNnh7Y/R/4=",
"path": "github.com/urfave/cli",
"revision": "b99aa811b4c1dd84cc6bccb8499c82c72098085a",
"revisionTime": "2017-08-04T09:34:15Z"
}
],
"rootPath": "mytool"
}