Golang依赖关联工具:glide从入门到精通使用
介绍不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题。Java有牛逼轰轰的Maven和Gradle。 Golang亦有godep、govendor、glide、gvt、gopack等等,本文主要给大家介绍gilde。 glide是Golang的包管理工具,是为了解决Golang依赖问题的。 安装Golang环境设置Golang 1.5版本后才采用vendor目录特性。 //设置环境变量使用vendor目录 GO15VENDOREXPERIMENT=1 安装glide$gogetgithub.com/Masterminds/glide $goinstallgithub.com/Masterminds/glide 验证$glide NAME: glide-VendorPackageManagementforyourGoprojects. Eachprojectshouldhavea'glide.yaml'fileintheprojectdirectory.Files looksomethinglikethis: package:github.com/Masterminds/glide imports: -package:github.com/Masterminds/cookoo version:1.1.0 -package:github.com/kylelemons/go-gypsy subpackages: -yaml Formoredetailsonthe'glide.yaml'filesseethedocumentationat https://glide.sh/docs/glide.yaml USAGE: glide[globaloptions]command[commandoptions][arguments...] VERSION: 0.13.0-dev COMMANDS: create,initInitializeanewproject,creatingaglide.yamlfile config-wizard,cwWizardthatmakesoptionalsuggestionstoimproveconfiginaglide.yamlfile. getInstalloneormorepackagesinto`vendor/`andadddependencytoglide.yaml. remove,rmRemoveapackagefromtheglide.yamlfile,andregeneratethelockfile. importImportfilesfromotherdependencymanagementsystems. namePrintthenameofthisproject. novendor,nvListallnon-vendorpathsinadirectory. rebuildRebuild('gobuild')thedependencies install,iInstallaproject'sdependencies update,upUpdateaproject'sdependencies tree(Deprecated)Treeprintsthedependenciesofthisprojectasatree. listListprintsalldependenciesthatthepresentcodereferences. infoInfoprintsinformationaboutthisproject cache-clear,ccClearstheGlidecache. aboutLearnaboutGlide mirrorManagemirrors help,hShowsalistofcommandsorhelpforonecommand GLOBALOPTIONS: --yamlvalue,-yvalueSetaYAMLconfigurationfile.(default:"glide.yaml") --quiet,-qQuiet(noinfoordebugmessages) --debugPrintdebugverboseinformationalmessages --homevalueThelocationofGlidefiles(default:"/home/users/qiangmzsx/.glide")[$GLIDE_HOME] --tmpvalueThetempdirectorytouse.Defaultstosystemstemp[$GLIDE_TMP] --no-colorTurnoffcoloredoutputforlogmessages --help,-hshowhelp --version,-vprinttheversion 看到这样,那就恭喜你,已经安装成功了!!! 使用篇幅有限,我只介绍经常使用到的。先进入在GOPATH的一个项目中。 cd$GOPATH/src/foor 初始化 (glide init)$glideinit [INFO]GeneratingaYAMLconfigurationfileandguessingthedependencies [INFO]Attemptingtoimportfromotherpackagemanagers(use--skip-importtoskip) [INFO]Scanningcodetolookfordependencies [INFO]-->Foundreferencetogithub.com/urfave/cli [INFO]Writingconfigurationfile(glide.yaml) [INFO]WouldyoulikeGlidetohelpyoufindwaystoimproveyourglide.yamlconfiguration? [INFO]Ifyouwanttorevisitthisstepyoucanusetheconfig-wizardcommandatanytime. [INFO]Yes(Y)orNo(N)? Y [INFO]Loadingmirrorsfrommirrors.yamlfile [INFO]Lookingfordependenciestomakesuggestionson [INFO]-->Scanningfordependenciesnotusingversionranges [INFO]-->Scanningfordependenciesusingcommitids [INFO]Gatheringinformationoneachdependency [INFO]-->Thismaytakeamoment.Especiallyonacodebasewithmanydependencies [INFO]-->Gatheringreleaseinformationfordependencies [INFO]-->Lookingfordependencyimportswhereversionsarecommitids [INFO]Herearesomesuggestions... [INFO]Thepackagegithub.com/urfave/cliappearstohaveSemanticVersionreleases(http://semver.org). [INFO]Thelatestreleaseisv1.19.1.Youarecurrentlynotusingarelease.Wouldyoulike [INFO]tousethisrelease?Yes(Y)orNo(N) Y [INFO]Wouldyouliketorememberthepreviousdecisionandapplyittofuture [INFO]dependencies?Yes(Y)orNo(N) Y [INFO]Updatinggithub.com/urfave/clitousethereleasev1.19.1insteadofnorelease [INFO]Thepackagegithub.com/urfave/cliappearstousesemanticversions(http://semver.org). [INFO]Wouldyouliketotrackthelatestminororpatchreleases(major.minor.patch)? [INFO]Trackingminorversionreleaseswoulduse'>=1.19.1,<2.0.0'('^1.19.1').Trackingpatchversion [INFO]releaseswoulduse'>=1.19.1,<1.20.0'('~1.19.1').FormoreinformationonGlideversions [INFO]andrangesseehttps://glide.sh/docs/versions [INFO]Minor(M),Patch(P),orSkipRanges(S)? P [INFO]Wouldyouliketorememberthepreviousdecisionandapplyittofuture [INFO]dependencies?Yes(Y)orNo(N) Y [INFO]Updatinggithub.com/urfave/clitousetherange~1.19.1insteadofcommitidv1.19.1 [INFO]Configurationchangeshavebeenmade.Wouldyouliketowritethese [INFO]changestoyourconfigurationfile?Yes(Y)orNo(N) Y [INFO]Writingupdatestoconfigurationfile(glide.yaml) [INFO]Youcannowedittheglide.yamlfile.: [INFO]-->Formoreinformationonversionsandrangesseehttps://glide.sh/docs/versions/ [INFO]-->Fordetailsonadditionalmetadataseehttps://glide.sh/docs/glide.yaml/ $ll glide.yaml $catglide.yaml package:foor import:[] 在初始化过程中, glide 会询问一些问题。 glide.yaml记载了依赖包的列表及其更新规则,每次执行 glide up 时,都会按照指定的规则(如只下载补丁(patch)不下载升级(minor))下载新版。 一个完整的gilde.yaml package:foor homepage:https://github.com/qiangmzsx license:MIT owners: -name:qiangmzsx email:qiangmzsx@hotmail.com homepage:https://github.com/qiangmzsx #去除包 ignore: -appengine -golang.org/x/net #排除目录 excludeDirs: -node_modules #导入包 import: -package:github.com/astaxie/beego version:1.8.0 -package:github.com/coocood/freecache -package:github.com/garyburd/redigo/redis -package:github.com/go-sql-driver/mysql -package:github.com/bitly/go-simplejson -package:git.oschina.net/qiangmzsx/beegofreecache testImport: -package:github.com/smartystreets/goconvey subpackages: -convey 很多人看着yaml很不习惯,没事,我转一下json给大家看看。 { "excludeDirs":[ "node_modules" ],"owners":[ { "homepage":"https://github.com/qiangmzsx","name":"qiangmzsx","email":"qiangmzsx@hotmail.com" } ],"license":"MIT","package":"foor","ignore":[ "appengine","golang.org/x/net" ],"import":[ { "version":"1.8.0","package":"github.com/astaxie/beego" },{ "package":"github.com/coocood/freecache" },{ "package":"github.com/garyburd/redigo/redis" },{ "package":"github.com/go-sql-driver/mysql" },{ "package":"github.com/bitly/go-simplejson" },{ "package":"git.oschina.net/qiangmzsx/beegofreecache" } ],"testImport":[ { "subpackages":[ "convey" ],"package":"github.com/smartystreets/goconvey" } ],"homepage":"https://github.com/qiangmzsx" } 版本号指定规则 =:equal(aliasedtonooperator) !=:notequal >:greaterthan <:lessthan >=:greaterthanorequalto <=:lessthanorequalto 1.2-1.4.5whichisequivalentto>=1.2,<=1.4.5 2.3.4-4.5whichisequivalentto>=2.3.4,<=4.5 1.2.xisequivalentto>=1.2.0,<1.3.0 >=1.2.xisequivalentto>=1.2.0 <=2.xisequivalentto<3 *isequivalentto>=0.0.0 ~1.2.3isequivalentto>=1.2.3,<1.3.0 ~1isequivalentto>=1,<2 ~2.3isequivalentto>=2.3,<2.4 ~1.2.xisequivalentto>=1.2.0,<1.3.0 ~1.xisequivalentto>=1,<2 ^1.2.3isequivalentto>=1.2.3,<2.0.0 ^1.2.xisequivalentto>=1.2.0,<2.0.0 ^2.3isequivalentto>=2.3,<3 ^2.xisequivalentto>=2.0.0,<3 ’‘指定版本报错,需要用’’指定的可以不填写 安装依赖 (glide install)glide.yaml我们已经准备好了,现在就改安装一下试试。 $glideinstall [ERROR]Failedtoparse/home/users/xxxx/golang/src/foor/glide.yaml:yaml:invalidleadingUTF-8octet 报错了!别担心看看你的yaml文件是否为utf-8编码,不是就转换一下就好啦! $glideinstall [INFO]Lockfile(glide.lock)doesnotexist.Performingupdate. [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Fetchingupdatesforgithub.com/go-sql-driver/mysql [INFO]-->Fetchingupdatesforgithub.com/astaxie/beego [INFO]-->Fetchingupdatesforgithub.com/coocood/freecache [INFO]-->Fetchingupdatesforgit.oschina.net/qiangmzsx/beegofreecache [INFO]-->Fetchingupdatesforgithub.com/bitly/go-simplejson [INFO]-->Fetchingupdatesforgithub.com/garyburd/redigo [INFO]-->Fetchingupdatesforgithub.com/smartystreets/goconvey [INFO]-->Detectedsemanticversion.Settingversionforgithub.com/astaxie/beegotov1.8.0 [INFO]Resolvingimports [INFO]Downloadingdependencies.Pleasewait... [INFO]Settingreferencesforremainingimports [INFO]Exportingresolveddependencies... [INFO]-->Exportinggithub.com/astaxie/beego [INFO]-->Exportinggithub.com/coocood/freecache [INFO]-->Exportinggithub.com/bitly/go-simplejson [INFO]-->Exportinggithub.com/go-sql-driver/mysql [INFO]-->Exportinggithub.com/garyburd/redigo [INFO]-->Exportinggithub.com/smartystreets/goconvey [INFO]-->Exportinggit.oschina.net/qiangmzsx/beegofreecache [INFO]Replacingexistingvendordependencies [INFO]Projectrelieson6dependencies. $ll total12 glide.lock glide.yaml vendor $llvendor/ git.oschina.net github.com 看到glide.look了吗,这个文件记载了依赖包确定的revision,下次再执行 glide install 时,会直接读这个文件下载确定的版本。 升级版本 (glide up)glide up 会按照语义化版本规则更新依赖包代码,开发过程中如果需要使用新版代码,可以执行这个命令:修改一下glide.yaml中的一个Package. -package:github.com/astaxie/beego version:1.8.3 执行glide up。 $glideup [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Fetchingupdatesforgit.oschina.net/qiangmzsx/beegofreecache [INFO]-->Fetchingupdatesforgithub.com/garyburd/redigo [INFO]-->Fetchingupdatesforgithub.com/go-sql-driver/mysql [INFO]-->Fetchingupdatesforgithub.com/astaxie/beego [INFO]-->Fetchingupdatesforgithub.com/bitly/go-simplejson [INFO]-->Fetchingupdatesforgithub.com/coocood/freecache [INFO]-->Fetchingupdatesforgithub.com/smartystreets/goconvey [INFO]-->Detectedsemanticversion.Settingversionforgithub.com/astaxie/beegotov1.8.3 [INFO]Resolvingimports [INFO]Downloadingdependencies.Pleasewait... [INFO]Settingreferencesforremainingimports [INFO]Exportingresolveddependencies... [INFO]-->Exportinggithub.com/astaxie/beego [INFO]-->Exportinggithub.com/bitly/go-simplejson [INFO]-->Exportinggithub.com/garyburd/redigo [INFO]-->Exportinggithub.com/go-sql-driver/mysql [INFO]-->Exportinggithub.com/coocood/freecache [INFO]-->Exportinggithub.com/smartystreets/goconvey [INFO]-->Exportinggit.oschina.net/qiangmzsx/beegofreecache [INFO]Replacingexistingvendordependencies [INFO]Projectrelieson6dependencies. 添加并下载依赖 (glide get)除了自动从代码中解析 import 外,glide 还可以通过 glide get 直接下载代码中没有的依赖,与 go get 的用法基本一致: $glidegetgithub.com/orcaman/concurrent-map [INFO]Preparingtoinstall1package. [INFO]Attemptingtogetpackagegithub.com/orcaman/concurrent-map [INFO]-->Gatheringreleaseinformationforgithub.com/orcaman/concurrent-map [INFO]-->Addinggithub.com/orcaman/concurrent-maptoyourconfiguration [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Fetchingupdatesforgithub.com/garyburd/redigo [INFO]-->Fetchingupdatesforgithub.com/astaxie/beego [INFO]-->Fetchingupdatesforgithub.com/go-sql-driver/mysql [INFO]-->Fetchingupdatesforgit.oschina.net/qiangmzsx/beegofreecache [INFO]-->Fetchingupdatesforgithub.com/bitly/go-simplejson [INFO]-->Fetchinggithub.com/orcaman/concurrent-map [INFO]-->Fetchingupdatesforgithub.com/coocood/freecache [INFO]-->Fetchingupdatesforgithub.com/smartystreets/goconvey [INFO]Resolvingimports [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Detectedsemanticversion.Settingversionforgithub.com/astaxie/beegotov1.8.3 [INFO]Exportingresolveddependencies... [INFO]-->Exportinggithub.com/smartystreets/goconvey [INFO]-->Exportinggithub.com/garyburd/redigo [INFO]-->Exportinggithub.com/go-sql-driver/mysql [INFO]-->Exportinggithub.com/orcaman/concurrent-map [INFO]-->Exportinggithub.com/astaxie/beego [INFO]-->Exportinggithub.com/bitly/go-simplejson [INFO]-->Exportinggithub.com/coocood/freecache [INFO]-->Exportinggit.oschina.net/qiangmzsx/beegofreecache [INFO]Replacingexistingvendordependencies 使用镜像 (glide mirror)[WARN]Unabletocheckoutgolang.org/x/crypto [ERROR]Updatefailedforgolang.org/x/crypto:CannotdetectVCS [ERROR]Failedtodoinitialcheckoutofconfig:CannotdetectVCS 这几行信息估计很多人都是遇到过的。在我天朝或者在公司内部都可能不能访问一些站点,导致很Golang的依赖包不能通过go get下载。此时也就是glide大发神威的时候到了,可以通过配置将墙了的版本库 URL 映射到没被墙的 URL,甚至也可以映射到本地版本库。将golang.org映射到github: 修改glide.yaml加入 -package:golang.org/x/crypto 如果你的网络可以访问就不需要使用glide镜像功能,可以跳过。 $glidemirrorsetgolang.org/x/cryptogithub.com/golang/crypto [INFO]golang.org/x/cryptobeingsettogithub.com/golang/crypto [INFO]mirrors.yamlwrittenwithchanges $glideup [INFO]Loadingmirrorsfrommirrors.yamlfile [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Fetchingupdatesforgithub.com/orcaman/concurrent-map [INFO]-->Fetchinggolang.org/x/crypto [INFO]-->Fetchingupdatesforgithub.com/astaxie/beego [INFO]-->Fetchingupdatesforgithub.com/go-sql-driver/mysql [INFO]-->Fetchingupdatesforgithub.com/garyburd/redigo [INFO]-->Fetchingupdatesforgithub.com/coocood/freecache [INFO]-->Fetchingupdatesforgithub.com/bitly/go-simplejson [INFO]-->Fetchingupdatesforgit.oschina.net/qiangmzsx/beegofreecache [INFO]-->Fetchingupdatesforgithub.com/smartystreets/goconvey [INFO]-->Detectedsemanticversion.Settingversionforgithub.com/astaxie/beegotov1.8.3 [INFO]Resolvingimports [INFO]Downloadingdependencies.Pleasewait... [INFO]Settingreferencesforremainingimports [INFO]Exportingresolveddependencies... [INFO]-->Exportinggithub.com/astaxie/beego [INFO]-->Exportinggithub.com/coocood/freecache [INFO]-->Exportinggithub.com/smartystreets/goconvey [INFO]-->Exportinggithub.com/garyburd/redigo [INFO]-->Exportinggithub.com/go-sql-driver/mysql [INFO]-->Exportinggithub.com/bitly/go-simplejson [INFO]-->Exportinggithub.com/orcaman/concurrent-map [INFO]-->Exportinggolang.org/x/crypto [INFO]-->Exportinggit.oschina.net/qiangmzsx/beegofreecache [INFO]Replacingexistingvendordependencies [INFO]Projectrelieson8dependencies. $llvendor/ git.oschina.net github.com golang.org 终于看到golang.org啦!!!细心的你一定已经发现了 [INFO]mirrors.yamlwrittenwithchanges 说明执行glide mirror时候镜像配置写入到的是$HOME/.glide/mirrors.yaml中,打开看看。 repos: -original:golang.org/x/crypto repo:github.com/golang/crypto 还可以映射到本地目录。推荐大家可以去https://www.golangtc.com/download/package下载很多Golang类库。现在我去下载了:https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz,解压到本地目录/home/users/qiangmzsx/var/golang/golang.org/x/text。 $glidemirrorsetgolang.org/x/text/home/users/qiangmzsx/var/golang/golang.org/x/text [INFO]golang.org/x/textbeingsetto/home/users/qiangmzsx/var/golang/golang.org/x/text [INFO]mirrors.yamlwrittenwithchanges $glideup [INFO]Loadingmirrorsfrommirrors.yamlfile [INFO]Downloadingdependencies.Pleasewait... [INFO]-->Fetchinggolang.org/x/text [INFO]-->Fetchingupdatesforgithub.com/garyburd/redigo [INFO]-->Fetchingupdatesforgit.oschina.net/qiangmzsx/beegofreecache [INFO]-->Fetchingupdatesforgithub.com/astaxie/beego [INFO]-->Fetchingupdatesforgithub.com/bitly/go-simplejson [INFO]-->Fetchingupdatesforgithub.com/go-sql-driver/mysql [INFO]-->Fetchingupdatesforgithub.com/coocood/freecache [INFO]-->Fetchingupdatesforgithub.com/orcaman/concurrent-map [INFO]-->Fetchingupdatesforgolang.org/x/crypto [INFO]-->Fetchingupdatesforgithub.com/smartystreets/goconvey [INFO]-->Detectedsemanticversion.Settingversionforgithub.com/astaxie/beegotov1.8.3 [INFO]Resolvingimports [INFO]Downloadingdependencies.Pleasewait... [INFO]Settingreferencesforremainingimports [INFO]Exportingresolveddependencies... [INFO]-->Exportinggithub.com/astaxie/beego [INFO]-->Exportinggithub.com/go-sql-driver/mysql [INFO]-->Exportinggithub.com/bitly/go-simplejson [INFO]-->Exportinggithub.com/coocood/freecache [INFO]-->Exportinggithub.com/smartystreets/goconvey [INFO]-->Exportinggithub.com/garyburd/redigo [INFO]-->Exportinggithub.com/orcaman/concurrent-map [INFO]-->Exportinggolang.org/x/text [INFO]-->Exportinggolang.org/x/crypto [INFO]-->Exportinggit.oschina.net/qiangmzsx/beegofreecache [INFO]Replacingexistingvendordependencies [INFO]Projectrelieson9dependencies. 全局选项运行glide,在最后就可以看到 GLOBALOPTIONS: --yamlvalue,-vprinttheversion 如果大家想把glide的yaml文件换别的默认名称可以执行 $glide-yqiangmzsx.yaml 在官网中会看到一个GLIDE_HOME变量,该变量就是/home/users/qiangmzsx/.glide。这个目录之前有提到过,除了包含有mirrors.yaml还有一个很重要的目录cache本地 cache,每次更新代码时, glide 都会在本地保存 cache,以备下次 glide install 使用。 GLIDE_HOME可以通过如下命令修改。 $glide--home/home/glide 总结除了上述说到的功能,glide还有很多好的功能,后续有机会在写出来吧。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |