How to access maven dependecy from GitHub Package Registry (Beta)

Tob*_*bse 13 github maven github-ci

I uploaded a maven artefact to the GitHub Package Registry (Beta) and want to add it as maven dependency. I'm already in the Regestry-Beta and activated it for my sample HalloMaven project. Also the mvn deploy was succesful, so the artifact is public available here: https://github.com/TobseF/HelloMaven/packages
But how to include it as a maven dependency?

I tried to add it in a fresh sample project with this pom.xml:

<groupId>de.tfr.test</groupId>
<artifactId>maven-repo-test</artifactId>
<version>1.0-SNAPSHOT</version>

<repositories>
    <repository>
        <id>github</id>
        <name>GitHub TobseF Apache Maven Packages</name>
        <url>https://github.com/TobseF/HelloMaven/packages</url>
        <!-- also tried:
        <url>https://maven.pkg.github.com/HelloMaven/</url> -->
    </repository>
</repositories>


<dependencies>
    <dependency>
        <groupId>github.tobsef</groupId>
        <artifactId>hello-maven</artifactId>
        <version>1.2.1</version>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

But the dependency cannot be resolved. It's strange that the artifactId is github.tobsef.hello-maven, which doesn't match the hello-maven specified in the pom. But I have no clue why the github.tobsef gets prepended and if the repository url is correct.

The official GitHub Configuring Apache Maven for use with GitHub Package Registry only shows how to push it with credentials. But my repo is public, authentication is not needed.

Setup for the HalloMaven example:

settings.xml

    <profiles>
        <profile>
            <id>github</id>
            <repositories>
                <repository>
                    <id>github</id>
                    <name>GitHub TobseF Apache Maven Packages</name>
                    <url>https://maven.pkg.github.com/TobseF</url>
                </repository>
            </repositories>
        </profile>
    </profiles>

    <servers>
        <server>
            <id>github</id>
            <username>${env.GITHUB_USERNAME}</username>
            <password>${env.GITHUB_TOKEN}</password>
        </server>
    </servers>
Run Code Online (Sandbox Code Playgroud)

pom.xml

   <groupId>github.tobsef</groupId>
    <artifactId>hello-maven</artifactId>
    <version>1.2.1</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
            </plugin>
        </plugins>
    </build>

    <properties>
        <github.global.server>github</github.global.server>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>11</java.version>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <distributionManagement>
        <repository>
            <id>github</id>
            <name>GitHub TobseF Apache Maven Packages</name>
            <url>https://maven.pkg.github.com/TobseF/HelloMaven</url>
        </repository>
    </distributionManagement>
Run Code Online (Sandbox Code Playgroud)

deploy.yml

name: Maven Deploy

on:
  push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v1
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Maven build
        run: mvn --file pom.xml package

  deploy:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    - name: Deploy to Github Package Registry
      env:
        GITHUB_USERNAME: ${{ secrets.GITHUB_USERNAME }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: mvn --settings settings.xml --file pom.xml deploy
Run Code Online (Sandbox Code Playgroud)

The result is a Could not find artifact github.tobsef:hello-maven:pom:1.2.1 in github (https://github.com/TobseF/HelloMaven/packages).

Any Idea how to setup the deployment to deploy the correct artifact and how to add it as dependency?

Tob*_*bse 12

好吗?,我找到了如何以正确的方式配置它。

您可以在此处查看具有工作GitHub Actions CI 和GitHub 包注册表的示例项目:
HelloMaven
要了解如何包含依赖项,请检查:
GitHub-plugin-registry-example Template

诀窍是在全局 maven 中向 GitHub API 添加身份验证settings.xml

<servers>
    <server>
        <id>github</id>
        <username>YOUR_USERNAME</username>
        <password>YOUR_AUTH_TOKEN</password>
    </server>
</servers>
Run Code Online (Sandbox Code Playgroud)

将 替换为YOUR_USERNAME您的 GitHub 登录名。
将 替换YOUR_AUTH_TOKEN为生成的 GitHub 个人访问令牌:
GitHub >设置>开发人员设置>个人访问令牌>生成新令牌
令牌至少需要read:packages范围。否则你会得到一个Not authorized例外。

尚不清楚读取包也需要此身份验证。特别是因为该 jar 在包页面上无需任何登录即可使用:https : //github.com/TobseF/HelloMaven/packages

所以它有点讨厌,因为我们必须添加<server><id>github</id>...并希望其他人也提供repository带有githubid 的 。否则,我们必须为每个 github 依赖项添加服务器配置。

请记住,每个 GitHub 存储库都是其自己的 Maven 存储库。所以没有像 maven central 那样的全局注册表。每个依赖项都必须提供自己的repository链接声明。

但是结合 GitHub Actions CI,这是一个非常好的替代方案,无需任何第三方插件。

  • 截至今天,即 2020 年 7 月 11 日,未经授权无法使用 GitHub Packages。换句话说,不可能对 GitHub Packages 使用匿名访问。您可以关注 GitHub 论坛中的讨论:https://github.community/t/download-from-github-package-registry-without-authentication/14407/10 (5认同)
  • @FelipeDesiderati 友好建议:我认为你的意思是“甚至”今天,而不是“直到”今天。但谢谢你的确认。 (4认同)
  • 您还可以使用 Github 内置的身份验证: `username = System.getenv("GITHUB_ACTOR")` `password = System.getenv("GITHUB_TOKEN")` (3认同)
  • 2023 年 8 月更新:整个互联网仍然没有更好的资源展示如何将包发布到 gh 包注册表并在 github CI 中使用它。杰出的 (2认同)