问题
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:
- https://github.com/X/Y/packages
 - https://maven.pkg.github.com/X
 
How could I use a private Github Package Repository artifact from another Gradle project? What is wrong with my current setup?
回答1:
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
    来源:https://stackoverflow.com/questions/58398575/accessing-private-maven-github-package-registry-with-gradle