nexus 3.17.0 做为golang 的包管理工具

时间:2023-03-09 18:55:43
nexus 3.17.0 做为golang 的包管理工具

nexus 3.17.0 新版本对于go 包管理的支持是基于go mod 的,同时我们也需要一个athens server
然后在nexus 中配置proxy 类型的repo

参考配置

  • 来自官方的配置图

nexus 3.17.0 做为golang 的包管理工具

  • 说明
    就和上边说的一样,我们需要一个athens server,nexus 对于go mod 的支持就是通过配置proxy到athens server
    同时为了使用需要配置GOPROXY 环境变量

环境搭建

  • docker-compose 文件
version: "3"
services: 
  nexus:
    image: sonatype/nexus3:3.17.0
    ports: 
    - "80:8081"
    volumes: 
    - "./nexus-data:/nexus-data"
  athens:
    image: gomods/athens:latest
    ports: 
    - "3000:3000"
  • 配置go proxy

nexus 3.17.0 做为golang 的包管理工具

golang 代码使用proxy

  • 配置host 文件
    因为localhost 有问题,所以简单配置了一个127.0.0.1 dalongrong.com
127.0.0.1  dalongrong.com
  • 简单go mod 项目
go mod init github.com/rongfengliang/nexus-mod
go: creating new go.mod: module github.com/rongfengliang/nexus-mod
  • 一个简单的http server 代码
main.go:
package main
import (
    "fmt"
    "net/http"
    "github.com/urfave/negroni"
)
func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "Welcome to the home page!")
    })
    n := negroni.Classic() // Includes some default middlewares
    n.UseHandler(mux)
    http.ListenAndServe(":8000", n)
}
  • 配置GOPROXY
export GOPROXY=http://dalongrong.com/repository/golang/
  • 添加缺少的依赖
go mod tidy
  • 构建
go build
  • 运行
./nexus-mod

效果

├── go.mod
├── go.sum
├── main.go
└── nexus-mod
  • nexus 系统proxy 的信息

nexus 3.17.0 做为golang 的包管理工具

说明

以上是一个简单的学习试用,同时也支持group 类型的,还是很不错的

参考资料

https://help.sonatype.com/repomanager3/release-notes/2019-release-notes#id-2019ReleaseNotes-RepositoryManager3.17.0 
https://help.sonatype.com/repomanager3/formats/go-repositories 
https://github.com/rongfengliang/nexus-golang-package