各种编程语言使用本地依赖库的方法
我们在开发的过程中或多或少都要用到一些开源项目,有时候需要根据具体的业务需求对开源依赖包进行修改,但是涉及到商业秘密又不能把自己修改的东西开源,所以就只能以本地依赖的形式使用。
这篇文章整理了本人常用的几种编程语言如何使用本地的依赖库的方法,记录一下,方便后面查阅。
# 1. PHP
编辑 composer.json
, 加入下面的配置(herosphp 是依赖包的名称):
"repositories": [ { "type": "path", "url": "/php/herosphp" } ], "require": { "herosphp": "*@dev" }, "minimum-stability": "dev"
Copied!
依赖类型指定为 path
, 而不是走网络下载,url
是本地依赖库的地址。完整的 composer.json
文件内容为:
{ "name": "test/app", "description": "test web appliaction", "type": "app", "license": "MIT", "authors": [ { "name": "blackfox", "email": "yangjian102621@gmail.com" } ], "repositories": [ { "type": "path", "url": "/php/herosphp" } ], "require": { "herosphp/framework": "*@dev", "qiniu/php-sdk": "^7.2" }, "minimum-stability": "dev" }
Copied!
# 2. Javascript(Nodejs)
{ "name": "qark-docs", "version": "1.0.0", "description": "lotus operation maintenance courses of YuanYu cloud", "main": "index.js", "scripts": { "dev": "vuepress dev .", "build": "vuepress build . " }, "author": "Rock", "license": "MIT", "devDependencies": { "@vuepress/plugin-back-to-top": "^1.8.2", "babel-plugin-component": "^1.1.1", "vuepress": "^1.9.7", "vuepress-plugin-code-copy": "^1.0.6", "markdown-it-attrs": "^4.1.3", "@vuepress/theme-default": "^1.9.7" }, "dependencies": { "async-validator": "^1.11.5", "element-ui": "^2.15.5" } }
Copied!
假如我现在要把上面的 @vuepress/theme-default
组件换成我本地自己维护的仓库,只需要把当前依赖改成使用 file
协议加载就好了:
{ "name": "qark-docs", "version": "1.0.0", "description": "lotus operation maintenance courses of YuanYu cloud", "main": "index.js", "scripts": { "dev": "vuepress dev .", "build": "vuepress build . " }, "author": "Rock", "license": "MIT", "devDependencies": { "@vuepress/plugin-back-to-top": "^1.8.2", "babel-plugin-component": "^1.1.1", "vuepress": "^1.9.7", "vuepress-plugin-code-copy": "^1.0.6", "markdown-it-attrs": "^4.1.3", "@vuepress/theme-default": "file:/code/nodejs/vuepress-theme" }, "dependencies": { "async-validator": "^1.11.5", "element-ui": "^2.15.5" } }
Copied!
# 3. Golang
启用
Go Module
支持,在 ~/.bashrc 文件中导出GO111MODULE
环境变量即可:export GO111MODULE=on
Copied!在
$GOPATH
下面先创建一个 go module,并初始化 module:mkdir $GOPATH/src/testmod -p cd $GOPATH/src/testmod go mod init testmod
Copied!上面命令会在 testmod 目录下生成一个
go.mod
文件:module testmod go 1.16
Copied!我们在当前目录下新建一个 http 包,导出一个 Post 函数:
package http func Post(url string) { }
Copied!如果是在同一个项目下,可以直接引用 testmod 下的包就可以了:
package main import ( "bytes" "strings" "testmod/http" ) func main() { http.Post("http://www.yycloud.pro") }
Copied!testmod 项目结构如下:
testmod ├── go.mod └── http └── http.go
Copied!如果是其他项目想要引入这个本地 module, 只需在
go.mod
文件中加入下面 2 行配置:require ( testmod v0.0.0 ) replace ( testmod => "../testmod" # 这里使用相对路径或者绝对路径都可以 )
Copied!
# 4. Java(maven)
Maven 使用本地依赖构件(Artifact)和使用远程几乎一样,假设本地构件信息如下:
<groupId>org.rockyang</groupId> <artifactId>test-lib</artifactId> <version>1.0.0</version>
Copied!
我们只需要先把本地依赖 install 到本地:
cd /java/test-lib mvn clean install
Copied!
然后在其他项目就可以直接在 pom.xml
引入依赖了:
<dependency> <groupId>org.rockyang</groupId> <artifactId>jblock-base</artifactId> <version>2.0.0</version> </dependency>
Copied!
# 5. Rust(Cargo)
创建一个本地包(hello):
cargo new hello # 输出 Created library 'hello' project # 项目结构 tree hello hello ├── Cargo.toml └── src └── lib.rs
Copied!我们给这个包加一个
say_hello
函数:// cat hello/src/lib.rs pub fn say_hello(name: &str) { println!("hello {}!", name); }
Copied!使用本地包,假设我们在 hello 的同级目录有一个 demo 项目,这个项目将使用 hello 包里的函数:
先创建这个 demo 项目:
cargo new demo --bin # 输出 Created binary (application) `demo` project
Copied!修改 demo 项目的
Cargo.toml
文件,指定 hello 包的本地路径(相对路径或者绝对路径都行):[dependencies] hello = { path = "../hello" }
Copied!然后在项目中增加调用代码:
// cat demo/src/main.rs extern crate hello; fn main() { hello::say_hello("RockYang"); }
Copied!运行程序,检查是否调用成功:
$ cargo run Compiling hello v0.1.0 (/data/test/hello) Compiling demo v0.1.0 (/data/test/demo) Finished dev [unoptimized + debuginfo] target(s) in 0.35s Running `target/debug/demo` hello RockYang!
Copied!从编译日志中可以看出,确实是调用的本地依赖包,撒哟啦啦 O(∩_∩)O~。