Working with the Gradle registry

You can configure Gradle to publish packages to the GitHub Packages Gradle registry 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 的更多信息,请参阅“工作流程中的身份验证”。 For more information about using GITHUB_TOKEN with Gradle, see "Publishing Java packages with Gradle."

Authenticating with a personal access token

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

You can authenticate to GitHub Packages with Gradle using either Gradle Groovy or Kotlin DSL by editing your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file to include your personal access token. You can also configure Gradle Groovy and Kotlin DSL to recognize a single package or multiple packages in a repository.

Replace REGISTRY-URL with the URL for your instance's Maven registry. If your instance has subdomain isolation enabled, use maven.HOSTNAME. If your instance has subdomain isolation disabled, use HOSTNAME/_registry/maven. In either case, replace HOSTNAME with the host name of your GitHub Enterprise Server instance.

Replace USERNAME with your GitHub username, TOKEN with your personal access token, REPOSITORY with the name of the repository containing the package you want to publish, and OWNER with the name of the user or organization account on GitHub 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.

Note: GitHub Packages 支持 Apache Maven 的 SNAPSHOT 版本。 要使用 GitHub Packages 仓库下载 SNAPSHOT 工件,请在所用项目的 POM 或 ~/.m2/settings.xml 文件中启用 SNAPSHOTS。 For an example, see "Configuring Apache Maven for use with GitHub Packages."

Example using Gradle Groovy for a single package in a repository

plugins {
    id("maven-publish")
}
publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
            }
        }
    }
    publications {
        gpr(MavenPublication) {
            from(components.java)
        }
    }
}

Example using Gradle Groovy for multiple packages in the same repository

plugins {
    id("maven-publish") apply false
}
subprojects {
    apply plugin: "maven-publish"
    publishing {
        repositories {
            maven {
                name = "GitHubPackages"
                url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
                credentials {
                    username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
                    password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
                }
            }
        }
        publications {
            gpr(MavenPublication) {
                from(components.java)
            }
        }
    }
}

Example using Kotlin DSL for a single package in the same repository

plugins {
    `maven-publish`
}
publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
            }
        }
    }
    publications {
        register<MavenPublication>("gpr") {
            from(components["java"])
        }
    }
}

Example using Kotlin DSL for multiple packages in the same repository

plugins {
    `maven-publish` apply false
}
subprojects {
    apply(plugin = "maven-publish")
    configure<PublishingExtension> {
        repositories {
            maven {
                name = "GitHubPackages"
                url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
                credentials {
                    username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                    password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
                }
            }
        }
        publications {
            register<MavenPublication>("gpr") {
                from(components["java"])
            }
        }
    }
}

Publishing a package

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

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

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

  2. After creating your package, you can publish the package.

    $ gradle publish

Using a published package

To use a published package from GitHub Packages, add the package as a dependency and add the repository to your project. For more information, see "Declaring dependencies" in the Gradle documentation.

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

  2. Add the package dependencies to your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file.

    Example using Gradle Groovy:

    dependencies {
        implementation 'com.example:package'
    }

    Example using Kotlin DSL:

    dependencies {
        implementation("com.example:package")
    }
  3. Add the repository to your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file.

    Example using Gradle Groovy:

    repositories {
        maven {
            url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
            }
        }
    }

    Example using Kotlin DSL:

    repositories {
        maven {
            url = uri("https://REGISTRY-URL/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
            }
        }
    }

Further reading

此文档对您有帮助吗?

隐私政策

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

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

做出贡献

或者, 了解如何参与。