美文网首页
将本地jar 批量发布到 Nexus 私服

将本地jar 批量发布到 Nexus 私服

作者: 今日开发技巧分享 | 来源:发表于2023-02-19 14:58 被阅读0次

前言

在日常开发过程中,我们有遇到将项目依赖通过 mvn deploy 发布到自己的公司的私服,以便其他人/环境统一下载入口。但是也有一些特殊情况,比如只能拿到第三方(或甲方)的依赖jar ,并拿不到源码,所以只能将jar 通过 mvn deploy:deploy-file 发布到nexus,供构建环境下载依赖,一个两个还好,那几十个呢?所以,自己就用go 写了个发布工具。

问题/需求描述

项目性质决定我们的开发模式(苦逼项目外包),由甲方提供基础框架,我们基于基础框架进行业务开发,但是甲方不提供源码或者编译构建环境,需要申请VPN 已经访问账号,如果对方的VPN 网络不通畅很容易影响开发进度;而且,我们自己的构建环境没法通过他们的VPN 去下载依赖,所以,需要解决以下问题:

  • 连甲方的VPN 下载基础框架的依赖(十几个包,而且还是 SNAPSHOT)
  • 将基础框架的依赖发布到我们私服

解决方案

使用 mvn deploy:deploy-file命令发布到私服,我们先看看需要哪些参数:

  • -Dfile需要发布jar包的绝对路径
  • -DgroupId 对应pom 里面的 <groupId>groupId</groupId>
  • -DartifactId 对应pom 里面的 <artifactId>artifactId</artifactId>
  • -Dversion 对应pom 里面的 <version>version</version>
  • -Dpackaging 对应pom 里面的 <packaging>packaging</packaging> ,比如 jar、pom
  • -DrepositoryId 仓库ID
  • -Durl 远程仓库地址

经过观察,我们本地仓库的依赖目录结构,会有两个关键内容,一个是jar 编译包,另个就是当前jar的maven 描述,如下图:


image.png

jar 都知道是需要发布到私服的文件,最关键的是 xxx-xxx-xxx.pom 描述文件,我们可以通过解析这个描述文件拿到 maven的 groupIdartifactIdversion关键信息。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <!-- This module was also published with a richer model, Gradle metadata,  -->
  <!-- which should be used instead. Do not delete the following line which  -->
  <!-- is to indicate to Gradle or any Gradle module metadata file consumer  -->
  <!-- that they should prefer consuming it instead. -->
  <!-- do_not_remove: published-with-gradle-metadata -->
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot</artifactId>
  <version>2.7.6</version>
  <name>spring-boot</name>
  <description>Spring Boot</description>
  <url>https://spring.io/projects/spring-boot</url>
  <organization>
    <name>Pivotal Software, Inc.</name>
    <url>https://spring.io</url>
  </organization>
  <licenses>
    <license>
      <name>Apache License, Version 2.0</name>
      <url>https://www.apache.org/licenses/LICENSE-2.0</url>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>Pivotal</name>
      <email>info@pivotal.io</email>
      <organization>Pivotal Software, Inc.</organization>
      <organizationUrl>https://www.spring.io</organizationUrl>
    </developer>
  </developers>
  <scm>
    <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
    <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
    <url>https://github.com/spring-projects/spring-boot</url>
  </scm>
  <issueManagement>
    <system>GitHub</system>
    <url>https://github.com/spring-projects/spring-boot/issues</url>
  </issueManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.24</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.24</version>
      <scope>compile</scope>
    </dependency>
  </dependencies>
</project>

分析完成之后,就可以知道代码的实现逻辑了,读取某个目录下的所有文件(含子目录),文件包含jar 以及 pom.xml 文件获取maven 坐标以及版本信息,代码:

// 查找需要发布的jar文件
func findDeployFile() ([]DeployFile, error) {

    var deployeFilesSlice []DeployFile

    err := filepath.Walk(RootPath, func(path string, info fs.FileInfo, err error) error {
        if err != nil {
            log.Fatal(err)
            return err
        }

        if !info.IsDir() {
            s := strings.Split(path, "\\")
            // 取上一层目录,作为版本
            version := s[len(s)-2 : len(s)-1]
            if strings.Contains(info.Name(), version[0]) && strings.HasSuffix(info.Name(), ".pom") {
                p, _ := getPom(path)
                if strings.Contains(info.Name(), p.Version) {
                    var _path = path
                    if p.Packaging == "jar" {
                        _path = strings.Replace(_path, ".pom", ".jar", 1)
                    }
                    var depolyeFile = DeployFile{FilePath: _path, PomConfig: p}
                    deployeFilesSlice = append(deployeFilesSlice, depolyeFile)
                }
            }

        }
        return nil
    })
    if err != nil {
        return nil, err
    }
    return deployeFilesSlice, nil
}

// 读取pom 文件,获取坐标
func getPom(path string) (Pom, error) {
    fmt.Printf("path: %v\n", path)
    file, err := os.Open(path)
    if err != nil {
        log.Fatal(err)
        return Pom{}, err
    }
    // 关闭流
    defer file.Close()
    // 读取xml 文件
    b, err2 := ioutil.ReadAll(file)
    if err2 != nil {
        log.Fatal(err2)
        return Pom{}, err2
    }
    s := strings.Split(path, "\\")

    v := Pom{}
    err3 := xml.Unmarshal(b, &v)
    if err != nil {
        log.Fatal(err3)
    }
    if v.GroupId == "" {
        v.GroupId = v.Parent.GroupId
    }
    if v.Packaging == "" {
        v.Packaging = "jar"
    }
    if v.Version == "" {
        // 取上一层目录,作为版本
        version := s[len(s)-2 : len(s)-1]
        v.Version = version[0]
    }
    fmt.Printf("version: %v\n", v.Version)
    // fmt.Printf("v: %v\n", v)
    return v, nil
}

拿到需要发布jar 的坐标关键信息之后,就是拼接 mvn deploy:deploy-file的命令并执行该命令。代码:

// 发布到私服
func deployeCMD(deployConfig DeployFile) {
    var sourceFilePath = ""
    if deployConfig.PomConfig.Packaging == "jar" {
        sourceFilePath = "-Dsources=" + strings.Replace(deployConfig.FilePath, ".jar", "-sources.jar", 1)
    }
    cmd := exec.Command("mvn",
        "deploy:deploy-file",
        "-Dmaven.test.skip=true",
        "-Dfile="+deployConfig.FilePath,
        sourceFilePath,
        "-DgroupId="+deployConfig.PomConfig.GroupId,
        "-DartifactId="+deployConfig.PomConfig.ArtifactId,
        "-Dversion="+deployConfig.PomConfig.Version,
        "-Dpackaging="+deployConfig.PomConfig.Packaging,
        "-DrepositoryId="+RerepositoryId,
        "-Durl="+RemoteURL)

    fmt.Printf("cmd.Args: %v\n", cmd.Args)
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatal(err)
    }
    // 关闭流
    defer stdout.Close()
    // 输出命令执行结果
    if err := cmd.Start(); err != nil { // 运行命令
        log.Fatal(err)
    }
    if opBytes, err := ioutil.ReadAll(stdout); err != nil { // 读取输出结果
        log.Fatal(err)
        return
    } else {
        log.Println(string(opBytes))
    }
}

到这里,将本地jar批量发布到nexus 的代码就算敲完了,下面附上完整的代码。

package main

import (
    "encoding/xml"
    "fmt"
    "io/fs"
    "io/ioutil"
    "log"
    "os"
    "os/exec"
    "path/filepath"
    "strings"
)

type Pom struct {
    XMLName    xml.Name `xml:"project"`
    GroupId    string   `xml:"groupId"`
    ArtifactId string   `xml:"artifactId"`
    Version    string   `xml:"version"`
    Packaging  string   `xml:"packaging"`
    Parent     Parent   `xml:"parent"`
}

type Parent struct {
    GroupId    string `xml:"groupId"`
    ArtifactId string `xml:"artifactId"`
    Version    string `xml:"version"`
}

type DeployFile struct {
    FilePath  string
    PomConfig Pom
}

const (
    RootPath       string = "D:\\Baas\\leatop_jar\\leatop"
    RemoteURL      string = "https://middle.xxxx.cn/repository/mvn-leatop-snapshots/"
    RerepositoryId string = "mvn-leatop-snapshots"
)

// 读取pom 文件,获取坐标
func getPom(path string) (Pom, error) {
    fmt.Printf("path: %v\n", path)
    file, err := os.Open(path)
    if err != nil {
        log.Fatal(err)
        return Pom{}, err
    }
    // 关闭流
    defer file.Close()
    // 读取xml 文件
    b, err2 := ioutil.ReadAll(file)
    if err2 != nil {
        log.Fatal(err2)
        return Pom{}, err2
    }
    s := strings.Split(path, "\\")

    v := Pom{}
    err3 := xml.Unmarshal(b, &v)
    if err != nil {
        log.Fatal(err3)
    }
    if v.GroupId == "" {
        v.GroupId = v.Parent.GroupId
    }
    if v.Packaging == "" {
        v.Packaging = "jar"
    }
    if v.Version == "" {
        // 取上一层目录,作为版本
        version := s[len(s)-2 : len(s)-1]
        v.Version = version[0]
    }
    fmt.Printf("version: %v\n", v.Version)
    // fmt.Printf("v: %v\n", v)
    return v, nil
}

// 查找需要发布的jar文件
func findDeployFile() ([]DeployFile, error) {

    var deployeFilesSlice []DeployFile

    err := filepath.Walk(RootPath, func(path string, info fs.FileInfo, err error) error {
        if err != nil {
            log.Fatal(err)
            return err
        }

        if !info.IsDir() {
            s := strings.Split(path, "\\")
            // 取上一层目录,作为版本
            version := s[len(s)-2 : len(s)-1]
            if strings.Contains(info.Name(), version[0]) && strings.HasSuffix(info.Name(), ".pom") {
                p, _ := getPom(path)
                if strings.Contains(info.Name(), p.Version) {
                    var _path = path
                    if p.Packaging == "jar" {
                        _path = strings.Replace(_path, ".pom", ".jar", 1)
                    }
                    var depolyeFile = DeployFile{FilePath: _path, PomConfig: p}
                    deployeFilesSlice = append(deployeFilesSlice, depolyeFile)
                }
            }

        }
        return nil
    })
    if err != nil {
        return nil, err
    }
    return deployeFilesSlice, nil
}

// 发布到私服
func deployeCMD(deployConfig DeployFile) {
    var sourceFilePath = ""
    if deployConfig.PomConfig.Packaging == "jar" {
        sourceFilePath = "-Dsources=" + strings.Replace(deployConfig.FilePath, ".jar", "-sources.jar", 1)
    }
    cmd := exec.Command("mvn",
        "deploy:deploy-file",
        "-Dmaven.test.skip=true",
        "-Dfile="+deployConfig.FilePath,
        sourceFilePath,
        "-DgroupId="+deployConfig.PomConfig.GroupId,
        "-DartifactId="+deployConfig.PomConfig.ArtifactId,
        "-Dversion="+deployConfig.PomConfig.Version,
        "-Dpackaging="+deployConfig.PomConfig.Packaging,
        "-DrepositoryId="+RerepositoryId,
        "-Durl="+RemoteURL)

    fmt.Printf("cmd.Args: %v\n", cmd.Args)
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        log.Fatal(err)
    }
    // 关闭流
    defer stdout.Close()
    // 输出命令执行结果
    if err := cmd.Start(); err != nil { // 运行命令
        log.Fatal(err)
    }
    if opBytes, err := ioutil.ReadAll(stdout); err != nil { // 读取输出结果
        log.Fatal(err)
        return
    } else {
        log.Println(string(opBytes))
    }
}

func main() {

    df, _ := findDeployFile()

    for _, file := range df {
        fmt.Printf("file: %v\n", file)
        deployeCMD(file)
    }

    // cmd := exec.Command("mvn", "-version")

}

总结

在使用过程中需要注意的2点:

  • windows 下与mac 的目录结构不一样,需要调整
  • RootPath string = "D:\\Baas\\leatop_jar\\leatop" 这个目录不能直接是maven 本地仓库目录,需要单独出来

相关文章

网友评论

      本文标题:将本地jar 批量发布到 Nexus 私服

      本文链接:https://www.haomeiwen.com/subject/wciikdtx.html