Skip to main content

使用 Apache Maven 注册表

您可以配置 Apache Maven 以将包发布到 GitHub Packages 并将存储在 GitHub Packages 上的包用作 Java 项目中的依赖项。

管理员可以配置包类型

向 GitHub Packages 验证

数据重用.package_registry.认证包 %}

有关 GitHub Actions 工作流中使用的 GITHUB_TOKEN 的详细信息,请参阅 在工作流中使用 GITHUB_TOKEN 进行身份验证

使用 personal access token

进行身份验证

必须使用具有适当范围的 personal access token (classic) 才可在 GitHub Packages 中发布和安装包。 有关详细信息,请参阅“GitHub Packages 简介”。

通过编辑 ~/.m2/settings.xml 文件以包含 personal access token (classic),您可以使用 Apache Maven 对 GitHub Packages 进行身份验证。 如果 ~/.m2/settings.xml 文件不存在,请新建该文件。

servers 标签中,添加一个包含 server 标签的子 id 标签,并将“USERNAME”替换为您的 GitHub 用户名,将“TOKEN”替换为您的 personal access token。

repositories 标签中,将存储库的 id 映射到您在包含您凭据的 id 标签中添加的 server,以配置该存储库。 将 HOSTNAME 替换为 你的 GitHub Enterprise Server 实例 的主机名,并将 OWNER 替换为拥有该存储库的个人帐户或组织的名称。 由于不支持大写字母,因此,即使您的 GitHub 用户或组织名称中包含大写字母,也必须对仓库所有者使用小写字母。

如果要与多个存储库交互,可以将每个存储库添加到 repositories 标记中的独立 repository 子节点,并将每个存储库的 id 映射到 servers 标记中的凭据。

Apache Maven 支持快照版本。

如果实例启用了子域隔离:

<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://repo.maven.apache.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>

如果实例禁用了子域隔离:

<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://repo.maven.apache.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>

发布包

默认情况下,GitHub 将包发布到名称与包相同的现有仓库中。 例如,GitHub 会将名为 com.example:test 的包发布到名为 OWNER/test 的存储库中。

警告

Apache Maven 包必须遵循命名约定,因此 artifactId 字段应仅包含小写字母、数字或连字符。 有关详细信息,请参阅 maven.apache.org 文档中 Maven 坐标的命名约定。 如果在项目名称中使用大写字母,将收到 422 Unprocessable Entity 响应。

如果要将多个包发布到同一存储库,可以在 pom.xml 文件的 <distributionManagement> 元素中包含该存储库的 URL。 GitHub 将根据该字段匹配仓库。 由于存储库名称也是 distributionManagement 元素的一部分,因此将多个包发布到同一存储库无需额外步骤。

有关创建包的详细信息,请参阅 maven.apache.org 文档

  1. 编辑位于包目录中的 pom.xml distributionManagement文件的 __ 元素,将 HOSTNAME 替换为 你的 GitHub Enterprise Server 实例 的主机名,将 OWNER 替换为拥有该存储库的个人帐户或组织的名称,并将 REPOSITORY 替换为包含项目的存储库的名称。

    如果您的实例启用了子域隔离功能:

    <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 Packages 安装 Apache Maven 包,请编辑 pom.xml 文件以包含该包作为依赖项。 如果您想从某个指定存储库所有者的任意存储库中安装包,请使用存储库 URL,例如 https://maven.HOSTNAME/OWNER/*。 有关在项目中使用 pom.xml 文件的详细信息,请参阅 Apache Maven 文档中的 POM 简介

  1. 向 GitHub Packages 验证。 有关详细信息,请参阅向 GitHub Packages 进行身份验证

  2. 将包依赖项添加到项目 pom.xml 文件的 dependencies 元素中,并将 com.example:test 替换为您的包标识符。

    <dependencies>
     <dependency>
        <groupId>com.example</groupId>
        <artifactId>test</artifactId>
        <version>1.0.0-SNAPSHOT</version>
      </dependency>
    </dependencies>
    
  3. 安装此包。

    mvn install
    

其他阅读材料

  •         [AUTOTITLE](/packages/working-with-a-github-packages-registry/working-with-the-gradle-registry)
    
  •         [AUTOTITLE](/packages/learn-github-packages/deleting-and-restoring-a-package)