本文主题
主要介绍如何通过github的Packages进行发布和引用maven依赖包
个人拙见
这个功能当前好像比较鸡肋,无特殊场景需要建议直接弃坑 (由于个人认知有限,可能会造成误导,所以仅做参考;欢迎各位大佬的指导)
原因:
1. 每个要引用或发布的服务都需要单独配置repository和server等信息
2. 引用的服务也需要配置server(账号)信息
如何发布依赖
1.在pom中加入配置
<distributionManagement>
<repository>
<!-- id 需要和 server id一致 -->
<id>github</id>
<!-- name, 可选 -->
<name>GitHub OWNER Apache Maven Packages</name>
<!-- 替换成实际的地址, e.g: 账户名称 zhangSan, 仓库名称 test 则为 https://maven.pkg.github.com/zhangsan/test -->
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</distributionManagement>
注:
将OWNER替换为拥有该仓库的用户或组织帐户的名称。
由于不支持大写字母,因此,即使您的 GitHub 用户或组织名称中包含大写字母,也必须对仓库所有者使用小写字母。
2.在maven全局settings.xml加入配置
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<!-- 与pom中的repository url一致 -->
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<!-- 该仓库的用户或组织帐户的名称 -->
<username>USERNAME</username>
<!-- 发布依赖的TOKEN -->
<password>TOKEN</password>
</server>
</servers>
注:
server id 与 (github)repository id 及 profile id一致
Token如何获取?
github Pc端:
Settings > Developer settings > Personal access tokens > Generate new token
或 直接访问 https://github.com/settings/tokens/new
先输入token的名称(Note)
选择有效期
再选择权限范围
write:packages
delete:packages
点击Generate token即可获取生成的token
3.maven deploy
如何引入依赖
1.引入自己maven配置中发布的依赖
<dependencies>
<!-- 直接在pom中引入即可 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
2.引入他人通过github Packages发布的依赖
### 1.在pom中加入对应依赖配置
<dependencies>
...
<dependency>
<groupId>com.github.lisi</groupId>
<artifactId>test</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
### 2.在maven全局settings.xml加入对应仓库相关的配置 (当前示例中的id: github-OWNER-REPOSITORY)
<activeProfiles>
...
<activeProfile>github-lisi-test</activeProfile>
</activeProfiles>
<profiles>
...
<profile>
<id>github-lisi-test</id>
<repositories>
<repository>
<id>github-lisi-test</id>
<!-- https://maven.pkg.github.com/OWNER/REPOSITORY 替换后的url, 即实际的仓库地址 -->
<url>https://maven.pkg.github.com/lisi/test</url>
</repository>
</repositories>
</profile>
</profiles>
<servers>
...
<server>
<id>github-lisi-test</id>
<!-- 访问该仓库的用户或组织帐户的名称 -->
<username>USERNAME</username>
<!-- 访问依赖的TOKEN -->
<password>TOKEN</password>
</server>
</servers>
注:
server id 与 (github)repository id 及 profile id一致
参考链接
-
https://docs.github.com/cn/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry
-
https://zhuanlan.zhihu.com/p/116913962