【问题标题】:How to reference public GitHub packages from maven project如何从 Maven 项目中引用公共 GitHub 包
【发布时间】:2020-10-18 23:32:04
【问题描述】:

我有一个 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 正常访问这个依赖项?我的图书馆在公共仓库中。

附:请不要建议使用 Jitpack,因为我希望在没有任何额外资源的情况下获得干净的解决方案。

【问题讨论】:

    标签: maven github-package-registry


    【解决方案1】:

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

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

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

    <repository>
      <id>github</id>
      <name>GitHub Packages</name>
      <url>https://my-user:b96e7de7514e45c5@maven.pkg.github.com/my-user/my-repo</url>
    </repository>
    

    否则,选项可能是:

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

    【讨论】:

    • 最后我已经发布到 Bintray。
    • @AlexeyAnufriev 我发现您可以在 URL 中包含访问令牌。这可能是目前要走的路。我已经更新了我的答案。但是除了非常琐碎的设置之外,我认为 GitHub Packages 不是您使用 Maven 的最佳方式。你需要为每个依赖项单独的存储库声明也很糟糕,因为你不能只使用maven.pkg.github.com/my-user 来访问 GitHub 组织中所有项目的所有工件。
    • 很好,只是不想暴露任何凭据。但是为了答案的完整性,它非常有用。
    • 小更新:创建对包具有只读访问权限并包含在 pom.xml 中的个人访问令牌会触发 GitGuardian - 自动撤销密钥。因此,除非为 repo 禁用 GitGuardian,否则这不是解决方案。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      接受的答案不再有效

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

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

      这将输出以下内容:

      $ 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;123456789abcsef@maven.pkg.github.com/<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。在这种情况下,最好创建一个机器用户。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-08-18
        • 2012-09-08
        • 1970-01-01
        • 1970-01-01
        • 2022-11-03
        • 2021-11-20
        • 2010-11-16
        • 2021-11-19
        相关资源
        最近更新 更多