此版本的 GitHub Enterprise 将停止服务 2022-02-16. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

Working with the Apache Maven registry

You can configure Apache Maven to publish packages to GitHub Packages and to use packages stored on GitHub Packages as dependencies in a Java project.

GitHub Packages 可用于 GitHub Free、GitHub Pro、组织的 GitHub Free、GitHub Team、GitHub Enterprise Cloud、GitHub Enterprise Server 和 GitHub AE。

注:这种包类型可能不适用于您的实例,因为站点管理员可以启用或禁用每种支持的包类型。 更多信息请参阅“为企业配置软件包支持”。

Authenticating to GitHub Packages

您需要访问令牌才能发布、安装和删除 GitHub Packages 中的包。

You can use a personal access token (PAT) to authenticate to GitHub Packages or the GitHub Enterprise Server API. 创建个人访问令牌时,可根据需要为令牌分配不同的作用域。 有关 PAT 与包相关的域的更多信息,请参阅“关于 GitHub Packages 的权限”。

要在 GitHub Actions 工作流程内向 GitHub Packages 注册表验证,您可以使用:

  • GITHUB_TOKEN 来发布与工作流程仓库相关联的包。
  • PAT 来安装与其他私有仓库(GITHUB_TOKEN 无法访问)相关联的包。

有关 GitHub Actions 工作流程中使用的 GITHUB_TOKEN 的更多信息,请参阅“工作流程中的身份验证”。

Authenticating with a personal access token

您必须使用具有适当范围的个人访问令牌才可在 GitHub Packages 中发布和安装。 更多信息请参阅“关于 GitHub Packages”。

You can authenticate to GitHub Packages with Apache Maven by editing your ~/.m2/settings.xml file to include your personal access token. Create a new ~/.m2/settings.xml file if one doesn't exist.

In the servers tag, add a child server tag with an id, replacing USERNAME with your GitHub username, and TOKEN with your personal access token.

In the repositories tag, configure a repository by mapping the id of the repository to the id you added in the server tag containing your credentials. Replace HOSTNAME with the host name of your GitHub Enterprise Server instance, and OWNER with the name of the user or organization account that owns the repository. Because uppercase letters aren't supported, you must use lowercase letters for the repository owner even if the GitHub user or organization name contains uppercase letters.

If you want to interact with multiple repositories, you can add each repository to separate repository children in the repositories tag, mapping the id of each to the credentials in the servers tag.

GitHub Packages 支持 Apache Maven 的 SNAPSHOT 版本。 要使用 GitHub Packages 仓库下载 SNAPSHOT 工件,请在所用项目的 POM 或 ~/.m2/settings.xml 文件中启用 SNAPSHOTS。

If your instance has subdomain isolation enabled:

<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.HOSTNAME/OWNER/REPOSITORY</url>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>USERNAME</username>
      <password>TOKEN</password>
    </server>
  </servers>
</settings>

If your instance has subdomain isolation disabled:

<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>HOSTNAME/_registry/maven/OWNER/REPOSITORY</url>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>USERNAME</username>
      <password>TOKEN</password>
    </server>
  </servers>
</settings>

Publishing a package

默认情况下,GitHub 将包发布到名称与包相同的现有仓库中。 For example, GitHub will publish a package named com.example:test in a repository called OWNER/test.

If you would like to publish multiple packages to the same repository, you can include the URL of the repository in the <distributionManagement> element of the pom.xml file. GitHub will match the repository based on that field. Since the repository name is also part of the distributionManagement element, there are no additional steps to publish multiple packages to the same repository.

For more information on creating a package, see the maven.apache.org documentation.

  1. Edit the distributionManagement element of the pom.xml file located in your package directory, replacing HOSTNAME with the host name of your GitHub Enterprise Server instance, OWNER with the name of the user or organization account that owns the repository and REPOSITORY with the name of the repository containing your project.

    If your instance has subdomain isolation enabled:

    <distributionManagement>
       <repository>
         <id>github</id>
         <name>GitHub OWNER Apache Maven Packages</name>
         <url>https://maven.HOSTNAME/OWNER/REPOSITORY</url>
       </repository>
    </distributionManagement>
    

    If your instance has subdomain isolation disabled:

    <distributionManagement>
       <repository>
         <id>github</id>
         <name>GitHub OWNER Apache Maven Packages</name>
         <url>https://HOSTNAME/_registry/maven/OWNER/REPOSITORY</url>
       </repository>
    </distributionManagement>
    
  2. Publish the package.

    $ mvn deploy

在发布包后,您可以在 GitHub 上查看该包。 更多信息请参阅“查看包”。

Installing a package

To install an Apache Maven package from GitHub Packages, edit the pom.xml file to include the package as a dependency. If you want to install packages from more than one repository, add a repository tag for each. For more information on using a pom.xml file in your project, see "Introduction to the POM" in the Apache Maven documentation.

  1. 向 GitHub Packages 验证。 更多信息请参阅“向 GitHub Packages 验证”。

  2. Add the package dependencies to the dependencies element of your project pom.xml file, replacing com.example:test with your package.

    <dependencies>
      <dependency>
        <groupId>com.example</groupId>
        <artifactId>test</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </dependency>
    </dependencies>
    
  3. Install the package.

    $ mvn install

Further reading

此文档对您有帮助吗?

隐私政策

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或者, 了解如何参与。