使用Gradle访问私有Maven Github程序包注册表 [英] Accessing Private Maven Github Package Registry with Gradle

查看:260
本文介绍了使用Gradle访问私有Maven Github程序包注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户 X 下有一个私有存储库,存储库名称为 Y :

I have a private repository under user X and repository name Y:

https://github.com/X/Y

这是一个用Gradle构建的Java项目.

This a Java project built with Gradle.

Gradle配置文件已按照官方 Github程序包注册表文档中的说明进行配置,并且我能够成功发布我的程序包:

The Gradle configuration file has been configured as explained in the official Github Package Registry documentation and I am able to publish my package with success:

https://help.github.com/en/articles/configuring-gradle-for-use-with-github-package-registry#publishing-a-package

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/X/Y")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("GITHUB_PACKAGE_REGISTRY_USER")
                password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_PACKAGE_REGISTRY_API_KEY")
            }
        }
    }
    publications {
        github(MavenPublication) {
            groupId = 'A'
            artifactId = 'B'
            version = '1.0.0'

            from(components.java)
        }
    }
}

如您在上面的配置中所见,我分别将 A B 用作groupId和artifactId.版本是1.0.0.

As you can see in the configuration above, I used respectively A and B for the groupId and artifactId. The version is 1.0.0.

我的问题是要从另一个Gradle项目中检索此私有依赖项.该项目具有特定的存储库配置,如下所示:

My issue is about retrieving this private dependency from another Gradle project. This project has a specific repositories configuration as follows:

repositories {
    jcenter()
    maven {
        url = 'https://maven.pkg.github.com/X/Y'
        credentials {
            username System.getenv("GITHUB_PACKAGE_REGISTRY_USER")
            password System.getenv("GITHUB_PACKAGE_REGISTRY_API_KEY")
        }
    }
}

我添加了如下依赖项:

implementation 'A:B:1.0.0'

但是Gradle无法解决依赖关系.

but Gradle could not resolve the dependency.

存储库URL( https://maven.pkg.github.com/X/Y )似乎还可以,因为我可以使用正确的凭据在浏览器中将其打开.不幸的是,我无法浏览层次结构.

The repository URL (https://maven.pkg.github.com/X/Y) seems OK since I can open it on my browser with the right credentials. Unfortunately, I cannot browse the hierarchy.

当我在Github网站上打开软件包摘要页面时,我发现了一些奇怪的东西.此外,该工件已发布在groupId A 和artifactId B 下,其中显示以下安装说明:

I noticed something weird when I open the package summary page on Github website. Also, the artifact was published under groupId A and artifactId B, it shows the following installation instructions:

<dependency>
  <groupId>com.github.X/Y</groupId>
  <artifactId>A.B</artifactId>
  <version>1.0.0</version>
</dependency> 

我试图将依赖项导入更改为:

I tried to change my dependency import to:

implementation 'com.github.X/Y:A.B:1.0.0'

但无法再次解决.

我还尝试了以下作为存储库URL的尝试:

I also tried with no success the following as a repository URL:

  • https://github.com/X/Y/packages
  • https://maven.pkg.github.com/X

如何使用另一个Gradle项目中的私有Github软件包存储库工件?我当前的设置有什么问题?

How could I use a private Github Package Repository artifact from another Gradle project? What is wrong with my current setup?

推荐答案

简而言之

分辨率存储库URL必须为

In Short

The resolution repository URL needs to be

https://maven.pkg.github.com/X/Y

https://maven.pkg.github.com/X/Y

(其中X是GitHub存储库所有者,而Y是GitHub存储库)

(where X is the GitHub repository owner and Y the GitHub repository, as usual)

请注意,无法浏览该Maven存储库.您只能从文件中下载之前已发布的确切文件,例如https://maven.pkg.github.com/X/Y/A/B/1.0.0/B-1.0.0.pom.

Note that this Maven repository cannot be browsed. You can only download the exact files from it that have been published before, like https://maven.pkg.github.com/X/Y/A/B/1.0.0/B-1.0.0.pom.

公认的是,GitHub Package Registry文档仍然有些混乱(甚至可能是错误的).

The GitHub Package Registry documentation is admittedly still a bit confusing (and maybe even wrong).

我可以使用以下Gradle构建配置(和Gradle 5.6.2)成功下载依赖项:

I could successfully get the dependency downloaded with the following Gradle build configuration (and Gradle 5.6.2):

plugins {
    id 'java'
}

repositories {
    maven {
        url = 'https://maven.pkg.github.com/X/Y'
        credentials {
            username = System.getenv("GITHUB_PACKAGE_REGISTRY_USER")
            password = System.getenv("GITHUB_PACKAGE_REGISTRY_API_KEY")
        }
    }
}

dependencies {
    implementation 'A:B:1.0.0'
}

task foo() {
    doLast {
        println configurations.runtimeClasspath.files
    }
}

使用此独立的build.gradle脚本,您可以运行以下命令在Gradle用户主目录中查看下载的依赖文件:

With this self-contained build.gradle script, you can run the following to see the downloaded dependency file(s) in your Gradle user home:

GITHUB_PACKAGE_REGISTRY_USER=my_user_name GITHUB_PACKAGE_REGISTRY_API_KEY=my_private_token ./gradlew foo

这篇关于使用Gradle访问私有Maven Github程序包注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆