如何从Maven项目引用公共GitHub软件包

问题描述

我有一个GitHub回购,其中的一个库发布到了自己的GitHub软件包maven存储库中。而且我还有另一个项目,希望将该库作为依赖项引用。

当我将以下配置添加到项目的POM文件中时,它根本不起作用。

<repositories>
 <repository>
  <id>github</id>
  <name>GitHub Packages</name>
  <url>https://maven.pkg.github.com/test-account/test-lib</url>
 </repository>
</repositories>

这需要我进行身份验证。我知道这很合乎逻辑,因为它基本上不是源存储库,而是底层的Maven存储库。但是有没有办法让普通的Maven可以访问这种依赖关系呢?我的图书馆在公共仓库中。

P.S。请不要建议使用Jitpack,因为我希望在没有任何其他资源的情况下获得干净的解决方案。

解决方法

答案似乎是“你不能”。参见this comment from a GitHub staff member

我们的Maven服务目前不允许未经授权的访问。我们计划在将来提供此服务,但在此之前需要改进服务。

目前,最简单的选择似乎是创建具有读取访问权限的个人访问令牌,并将其包含在<repository>pom.xml部分的URL中,如下所示:

<repository>
  <id>github</id>
  <name>GitHub Packages</name>
  <url>https://my-user:[email protected]/my-user/my-repo</url>
</repository>

否则,选项可能是:

  • 创建具有读取权限的个人访问令牌,并与全世界共享它。
  • 使用替代方法described here
  • 发布到Maven Central(但这是一个痛苦的世界)
,

目前,您不能。关于此功能请求的讨论正在进行中 here。您可以在该讨论主题中找到多种解决方法,也可以发表您的意见。

,

接受的答案不再有效

目前 GitGuardian 会自动撤销个人访问令牌 (PAT),如果该方法应用于公共存储库。按照 GitHub staff 的建议,解决方案如下:

  1. 仅使用 read:packages 范围创建 PAT
  2. 执行 docker run ghcr.io/jcansdale/gpr encode

这将输出以下内容:

$ docker run ghcr.io/jcansdale/gpr encode 0123456789abcsef
An encoded token can be included in a public repository without being automatically deleted by GitHub.

这些可用于各种软件包生态系统,如下所示:

A NuGet `nuget.config` file:
<packageSourceCredentials>
  <github>
    <add key="Username" value="PublicToken" />
    <add key="ClearTextPassword" value="&#48;123456789abcsef" />
  </github>
</packageSourceCredentials>

A Maven `pom.xml` file:
<repositories>
  <repository>
    <id>github-public</id>
    <url>https://public:&#48;[email protected]/<OWNER>/*</url>
  </repository>
</repositories>

An npm `.npmrc` file:
@OWNER:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken="\u0030123456789abcsef"
You can use this snippet in you project’s configuration file.

请注意,如果您有权访问需要保护的任何私有包,则不应包含自己的 read:packages PAT。在这种情况下,最好创建一个机器用户。