将 jar 包发布到 Github 的 Maven 仓库

将 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>  <!-- 改 OWNER -->
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>USERNAME</username>  <!-- 改 USERNAME -->
      <password>TOKEN</password>  <!-- 改 TOKEN -->
    </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>  <!-- 改 OWNER -->
     <url>https://maven.pkg.github.com/OWNER/REPOSITORY</url>  <!-- 改 OWNER、REPOSITORY -->
   </repository>
</distributionManagement>

2.2. Maven 发布

使用以下命令发布 package:

$ mvn deploy

3. 发布 Release

  • 在仓库页面中的 Releases 标签下,Create a new Release
  • 要记住自己填写的版本号

4. 使用 Jitpack

  1. 进入 https://jitpack.io
  2. 使用 Github 账号登录 jitpack
  3. 在搜索框中输入自己的仓库(例如:https://github.com/tzq0301/common-api-1.0.0,其中 tzq0301 是我的 id,common-api-1.0.0 是仓库名)
  4. 根据页面显示的内容操作,即可完成安装

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'
}

参考资料

  1. https://jitpack.io/
  2. https://stackoverflow.com/questions/20161602/loading-maven-dependencies-from-github
  3. https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry
  4. https://github.com/tzq0301/common-api-1.0.0
  5. https://docs.github.com/en/actions/guides/publishing-java-packages-with-maven / https://docs.github.com/cn/actions/guides/publishing-java-packages-with-maven
  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值