Accessing Private Maven Github Package Registry with Gradle
Asked Answered
J

1

10

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

https://github.com/X/Y

This a Java project built with Gradle.

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)
        }
    }
}

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.

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")
        }
    }
}

and I added the dependency as follows:

implementation 'A:B:1.0.0'

but Gradle could not resolve the dependency.

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.

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'

but it could not be resolved again.

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

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

Jepum answered 15/10, 2019 at 15:56 Comment(0)
M
14

In Short

The resolution repository URL needs to be

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

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

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.

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

Sample Build

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
    }
}

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

More information can be found in the GitHub docs.

Mingle answered 19/10, 2019 at 14:23 Comment(3)
Thanks for your answer. The issue was with the format used for implementation. The Github installation on Github pages instructions were wrong... hopefully, they fix them since I created and reported the issue.Jepum
This doesn't work in my case. I'm getting Unable to download maven package : "Your request could not be authenticated by the GitHub Packages service. Please ensure your access token is valid and has the appropriate scopes configured."Shelf
I think accessing the repo just with access token is required. Dont know how to use in gradle.Shelf

© 2022 - 2024 — McMap. All rights reserved.