14

So, I have a GitHub project with a Package Registry configured. It has two packages:

Two packages published in Github Package Registry

Package pages have instructions only for Maven, besides that, the instructions are broken (maven install so57323260 is not a valid way to add a dependency in Maven):

Package page with broken Maven instructions

Question is: how do I add that package in a Gradle build?

madhead
  • 31,729
  • 16
  • 153
  • 201

2 Answers2

13

New answer:

GitHub has published the official guide: Configuring Gradle for use with GitHub Packages.


Old answer:

First, configure Github Package Registry as a Maven repository in your Gradle build config:

build.gradle.kts:

repositories {
    jcenter()
    maven("https://maven.pkg.github.com/madhead") {
        credentials {
            username = "madhead"
            password = "<token>"
        }
    }
}

You can generate a token in your account settings page.

Now, add a dependency like:

build.gradle.kts:

dependencies {
    implementation("so57323260:so57323260:1.0.0")
    implementation("so57323260:test:1.0.2")
}

Here groupId is the repo's name and artifactId is the name of the published package.

madhead
  • 31,729
  • 16
  • 153
  • 201
  • 4
    It's not possible to depend on a public package on GitHub Package Registry without hardcoding someone's GitHub username and token in the build file, is it? – sim642 Oct 06 '19 at 19:38
  • 1
    Why "hardcoding"? You can keep the values in properties file or in the environment and get them with `System.getenv` or `System.getProperty` – madhead Oct 06 '19 at 23:11
  • 24
    Still, that's significantly more work for anyone else trying to just clone and run a project with dependencies that publically exist on GitHub Package Registry as opposed to Maven Central or jCenter. I was hoping there wouldn't be such totally unneeded limitation, forcing everyone to set up some GitHub-specific stuff just to automatically make use of JARs that are accessible publically. – sim642 Oct 07 '19 at 09:45
  • 2
    This does not seem to work for me. Either my packages but also your example package won't be found by gradle. – cansik Oct 27 '19 at 19:53
  • 1
    Are there any security concerns letting your own username and read-only token in gradle property file? – elect Nov 14 '19 at 10:03
  • it only works with `gradle install` for me, not refreshing the gradle in IntelliJ. Also my IntelliJ project doesn't find the "Library" class: https://github.com/madhead/so57323260/blob/master/src/main/kotlin/by/dev/madhead/playgrounds/so57323260/Library.kt `gradle install` doesnt find my package https://github.com/ttiganik/hrd-java-test-registry/packages/60280 with `implementation 'hrd-java-test-registry:test-registry:1.0-snapshot'` and maven("https://maven.pkg.github.com/ttiganik") – tonisives Nov 21 '19 at 04:57
  • "Could not get unknown property 'java' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer." – Dejas Dec 10 '19 at 21:09
  • Should we follow your **old answer** or **new answer**? – IgorGanapolsky Jun 16 '21 at 14:36
  • 1
    @IgorGanapolsky, follow the docs. I think these question and answers are outdated. – madhead Jun 16 '21 at 17:28
6

For those who are worried about the security of personal access token, the official guide suggests to access the username and password through Gradle property or system property.

Step1: Set USERNAME and TOKEN as system property (with export or set), or create a gradle.properties file under the project root folder like this:

gpr.user=<USERNAME>
gpr.token=<TOKEN>

Step2: Add the Github package registry with authentication in build.gradle:

    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.token") ?: System.getenv("TOKEN")
            }
        }
    }

Step 3: Add the package you want to consume in build.gradle:

dependencies {
    implementation 'com.example:package:version'
}

For more details (including how to config Maven), see the instructions in the Wiki I contributed here: https://github.com/GumTreeDiff/gumtree/wiki/Getting-Started

K. Symbol
  • 3,330
  • 1
  • 21
  • 22
  • 2
    We don't need to hardcode **gpr.user** and **gpr.token**. Github already stores those securely for us, and we just need to do: `System.getenv("GITHUB_ACTOR")` and `System.getenv("GITHUB_TOKEN")` – IgorGanapolsky Jun 16 '21 at 14:42
  • doesn't work any more with the latest maven built-in studio plugin https://stackoverflow.com/questions/76565326/task-generatemetadatafileforreleasepublication-uses-this-output-of-task-androids – user924 Jul 14 '23 at 16:30