前言
在日常开发过程中,我们有遇到将项目依赖通过 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 描述,如下图:
jar 都知道是需要发布到私服的文件,最关键的是 xxx-xxx-xxx.pom 描述文件,我们可以通过解析这个描述文件拿到 maven的 groupId
、artifactId
、version
关键信息。
<?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