飞行的蜗牛

vuePress-theme-reco 极客学长    2013 - 2025
飞行的蜗牛 飞行的蜗牛

Choose mode

  • dark
  • auto
  • light
首页
分类
  • 技术杂谈
  • Database
  • Docker
  • PHP
  • 随笔杂谈
  • 前端开发
  • FunnyTools
  • Jekyll
  • 读书笔记
  • Java
  • SpringBoot
  • 区块链技术
  • IPFS
  • C/C++
  • Filecoin
  • Golang
  • Sharding-JDBC
  • 分布式存储
  • Lotus-源码系列
  • Lotus
  • 框架源码系列
  • Spring-源码系列
  • AI
  • ChatGPT
  • Stable Diffusion
  • DeepSeek-R1
  • DeepSeek-V3
标签
时间抽
关于作者
开源项目
GeekAI (opens new window)
author-avatar

极客学长

154

文章

151

标签

首页
分类
  • 技术杂谈
  • Database
  • Docker
  • PHP
  • 随笔杂谈
  • 前端开发
  • FunnyTools
  • Jekyll
  • 读书笔记
  • Java
  • SpringBoot
  • 区块链技术
  • IPFS
  • C/C++
  • Filecoin
  • Golang
  • Sharding-JDBC
  • 分布式存储
  • Lotus-源码系列
  • Lotus
  • 框架源码系列
  • Spring-源码系列
  • AI
  • ChatGPT
  • Stable Diffusion
  • DeepSeek-R1
  • DeepSeek-V3
标签
时间抽
关于作者
开源项目
GeekAI (opens new window)
  • 各种编程语言使用本地依赖库的方法

    • 1. PHP
      • 2. Javascript(Nodejs)
        • 3. Golang
          • 4. Java(maven)
            • 5. Rust(Cargo)

            各种编程语言使用本地依赖库的方法

            vuePress-theme-reco 极客学长    2013 - 2025

            各种编程语言使用本地依赖库的方法


            极客学长 2022-05-02 0 Nodejs Golang PHP Java

            我们在开发的过程中或多或少都要用到一些开源项目,有时候需要根据具体的业务需求对开源依赖包进行修改,但是涉及到商业秘密又不能把自己修改的东西开源,所以就只能以本地依赖的形式使用。

            这篇文章整理了本人常用的几种编程语言如何使用本地的依赖库的方法,记录一下,方便后面查阅。

            # 1. PHP

            编辑 composer.json, 加入下面的配置(herosphp 是依赖包的名称):



             
             



             




            "repositories": [
                {
                    "type": "path",
                    "url": "/php/herosphp"
                }
            ],
            "require": {
                "herosphp": "*@dev"
            },
            
            "minimum-stability": "dev"
            

            依赖类型指定为 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"
            }
            

            # 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"
              }
            }
            

            假如我现在要把上面的 @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"
              }
            }
            

            # 3. Golang

            1. 启用 Go Module 支持,在 ~/.bashrc 文件中导出 GO111MODULE 环境变量即可:

              export GO111MODULE=on
              
            2. 在 $GOPATH 下面先创建一个 go module,并初始化 module:

              mkdir $GOPATH/src/testmod -p
              cd $GOPATH/src/testmod
              go mod init testmod
              

              上面命令会在 testmod 目录下生成一个 go.mod 文件:

              module testmod
              
              go 1.16
              

              我们在当前目录下新建一个 http 包,导出一个 Post 函数:

              package http
              
              func Post(url string)  {
               
              }
              
            3. 如果是在同一个项目下,可以直接引用 testmod 下的包就可以了:

              package main
              
              import (
                 "bytes"
                 "strings"
                 "testmod/http"
              )
              
              func main() {
                 http.Post("http://www.yycloud.pro")
              }
              

              testmod 项目结构如下:

              testmod
              ├── go.mod
              └── http
                  └── http.go
              
            4. 如果是其他项目想要引入这个本地 module, 只需在 go.mod 文件中加入下面 2 行配置:

              require (
                testmod v0.0.0
              )
              replace (
                testmod => "../testmod" # 这里使用相对路径或者绝对路径都可以
              )
              

            # 4. Java(maven)

            Maven 使用本地依赖构件(Artifact)和使用远程几乎一样,假设本地构件信息如下:

            <groupId>org.rockyang</groupId>
            <artifactId>test-lib</artifactId>
            <version>1.0.0</version>
            

            我们只需要先把本地依赖 install 到本地:

            cd /java/test-lib
            mvn clean install
            

            然后在其他项目就可以直接在 pom.xml 引入依赖了:

            <dependency>
              <groupId>org.rockyang</groupId>
              <artifactId>jblock-base</artifactId>
              <version>2.0.0</version>
            </dependency>
            

            # 5. Rust(Cargo)

            1. 创建一个本地包(hello):

              cargo new hello
              # 输出
              Created library 'hello' project
              
              # 项目结构
              tree hello
              hello
              ├── Cargo.toml
              └── src
                  └── lib.rs
              

              我们给这个包加一个 say_hello 函数:

              // cat hello/src/lib.rs
              pub fn say_hello(name: &str) {
                 println!("hello {}!", name);
              }
              
            2. 使用本地包,假设我们在 hello 的同级目录有一个 demo 项目,这个项目将使用 hello 包里的函数:

              先创建这个 demo 项目:

              cargo new demo --bin
              # 输出
              Created binary (application) `demo` project
              

              修改 demo 项目的 Cargo.toml 文件,指定 hello 包的本地路径(相对路径或者绝对路径都行):

              [dependencies]
              hello = { path = "../hello" }
              

              然后在项目中增加调用代码:

              // cat demo/src/main.rs
              extern crate hello;
              
              fn main() {
                  hello::say_hello("RockYang");
              }
              

              运行程序,检查是否调用成功:


               





              $ 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!
              

              从编译日志中可以看出,确实是调用的本地依赖包,撒哟啦啦 O(∩_∩)O~。

            本站博文如非注明转载则均属作者原创文章,引用或转载无需申请版权或者注明出处,如需联系作者请加微信: geekmaster01

            Lotus 扇区续期详解 Lotus 多重签名钱包使用详解