Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add GitHub Package Registry package as a Gradle dependency

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?

like image 908
madhead - StandWithUkraine Avatar asked Aug 06 '19 09:08

madhead - StandWithUkraine


People also ask

How to integrate GitHub with Gradle?

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: Step2: Add the Github package registry with authentication in build.gradle:

Is it possible to declare Git repository as dependency in Android Gradle?

Is it possible to declare git repository as dependency in android gradle? Show activity on this post. Step 1. Add the JitPack repository to build.gradle at the end of repositories: Step 2. Add the dependency in the form It is possible to build the latest commit on the master branch, for example : Show activity on this post.

Is it possible to depend on a public package on GitHub?

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? Why "hardcoding"? You can keep the values in properties file or in the environment and get them with System.getenv or System.getProperty

How do I authenticate to a GitHub packages registry?

To authenticate to a GitHub Packages registry within a GitHub Actions workflow, you can use: GITHUB_TOKEN to publish packages associated with the workflow repository. a PAT to install packages associated with other private repositories (which GITHUB_TOKEN can't access).


2 Answers

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.

like image 173
madhead - StandWithUkraine Avatar answered Sep 20 '22 20:09

madhead - StandWithUkraine


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

like image 24
K. Symbol Avatar answered Sep 19 '22 20:09

K. Symbol