将 jar 包发布到 Github 的 Maven 仓库
前置条件
我的前置条件:Github 学生认证附赠的 packet
我使用了 personal access token (PAT) 来认证 GitHub Packages / GitHub API
0. 获得 PAT
参考(很详细,一步一步来):https://docs.github.com/en/enterprise-server@3.1/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token
1. 修改 Maven 的 settings.xml
- 如果没有该文件,则新建一个即可
- 默认地址:~/.m2/settings.xml
- 在
<servers> 标签中添加一个 <server> 标签
- 使用自己的 Github 的 ID(用户名)替换掉以下的
USERNAME - 使用自己的 Github 的 personal access token (PAT) 替换掉以下的
TOKEN - 在
<repositories> 标签中
- 将
<repositories> 标签中的 id 的值与 <server> 标签中的 id 的值进行映射,保持一致(即复制 <server> 标签中的 id 的值到 <repositories> 标签中的 id 标签) - 使用仓库的拥有者(比如自己的 Github 的 ID(用户名))替换掉以下的
OWNER (注意:因为不支持大写字母,所以必须使用小写字母,如果 ID 中包含了大写字母,将其转换为小写字母即可)
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<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>
<url>https://maven.pkg.github.com/OWNER/*</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>USERNAME</username>
<password>TOKEN</password>
</server>
</servers>
</settings>
2. 发布 package
2.1. 修改 pom.xml
- 在 pom.xml 文件中,添加
<distributionManagement> 标签 - 将
OWNER 改成自己的 ID - 将
REPOSITORY 改成仓库名
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub OWNER Apache Maven Packages</name>
<url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>
</repository>
</distributionManagement>
2.2. Maven 发布
使用以下命令发布 package:
$ mvn deploy
3. 发布 Release
- 在仓库页面中的 Releases 标签下,Create a new Release
- 要记住自己填写的版本号
4. 使用 Jitpack
- 进入 https://jitpack.io
- 使用 Github 账号登录 jitpack
- 在搜索框中输入自己的仓库(例如:https://github.com/tzq0301/common-api-1.0.0,其中 tzq0301 是我的 id,common-api-1.0.0 是仓库名)
- 根据页面显示的内容操作,即可完成安装
5. Install package
例:https://jitpack.io/#tzq0301/common-api-1.0.0/1.0.0
5.1. 添加 JitPack repository 到构建文件
- 构建文件包括但不仅限于:pom.xml, build.gradle
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
5.2. 添加你自己的依赖
将页面中提供的 gav dependency 添加到自己的构建文件中即可
<dependency>
<groupId>com.github.tzq0301</groupId>
<artifactId>common-api-1.0.0</artifactId>
<version>1.0.0</version>
</dependency>
dependencies {
implementation 'com.github.tzq0301:common-api-1.0.0:1.0.0'
}
参考资料
- https://jitpack.io/
- https://stackoverflow.com/questions/20161602/loading-maven-dependencies-from-github
- https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry
- https://github.com/tzq0301/common-api-1.0.0
- https://docs.github.com/en/actions/guides/publishing-java-packages-with-maven / https://docs.github.com/cn/actions/guides/publishing-java-packages-with-maven
|