How to add GitHub Package Registry package as a Gradle dependency
Asked Answered
P

2

15

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?

Paleobiology answered 6/8, 2019 at 9:42 Comment(2)
Your repository project page says ~"No packages published"Blaineblainey
@IgorGanapolsky, that's because I cleaned up all my unsed packages several times.Paleobiology
P
14

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.

Paleobiology answered 6/8, 2019 at 10:6 Comment(9)
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?Calm
Why "hardcoding"? You can keep the values in properties file or in the environment and get them with System.getenv or System.getPropertyPaleobiology
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.Calm
This does not seem to work for me. Either my packages but also your example package won't be found by gradle.Disappoint
Are there any security concerns letting your own username and read-only token in gradle property file?Hyponasty
it only works with gradle install for me, not refreshing the gradle in IntelliJ. Also my IntelliJ project doesn't find the "Library" class: github.com/madhead/so57323260/blob/master/src/main/kotlin/by/… gradle install doesnt find my package github.com/ttiganik/hrd-java-test-registry/packages/60280 with implementation 'hrd-java-test-registry:test-registry:1.0-snapshot' and maven("maven.pkg.github.com/ttiganik")Jacquelyn
"Could not get unknown property 'java' for SoftwareComponentInternal set of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer."Thetis
Should we follow your old answer or new answer?Blaineblainey
@IgorGanapolsky, follow the docs. I think these question and answers are outdated.Paleobiology
T
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

Thilda answered 27/3, 2021 at 3:24 Comment(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")Blaineblainey
doesn't work any more with the latest maven built-in studio plugin #76565826Refurbish

© 2022 - 2024 — McMap. All rights reserved.