Skip to content

Gradle plugin for adding maven repos on Github Packages in one line

License

Notifications You must be signed in to change notification settings

0ffz/gpr-for-gradle

Repository files navigation

gpr-for-gradle (Gradle Github Packages Plugin)

Gradle Plugin Portal

GitHub Packages introduced features for hosting Maven packages for free, but these require credentials even for public ones.

This grade plugin allows you to add a maven repository from GitHub Packages in one line. It also comes with a default personal access token, allowing the use of public repositories without any extra setup (see Notes on automatic authentication).

Usage

Add the plugin

Using the plugins DSL:

plugins {
    id("io.github.0ffz.github-packages") version "1.x.x"
}
Using legacy plugin application:

buildscript {
  repositories {
    maven {
      url = uri("https://plugins.gradle.org/m2/")
    }
  }
  dependencies {
    classpath("gradle.plugin.io.github.0ffz:gpr-for-gradle:1.x.x")
  }
}

apply(plugin = "io.github.0ffz.github-packages")

You may find it on Gradle's plugin plugin portal

Groovy

Within Groovy, you may add a package repository as follows:

repositories {
    maven githubPackage.invoke("owner/repo")
   // Or for all packages under the owner/org
    maven githubPackage.invoke("owner")
}

There is currently no support for further customization in Groovy.

Kotlin

Add the GitHub repo to the repositories block:

repositories {
    githubPackage("owner/repo")
    // Or for all packages under the owner/org
    githubPackage("owner")
}

Modifying default username/key

Use the githubPackages blocks above repositories to edit the template applied to every package below. Example to change credentials of every repo:

githubPackages {
    credentials {
        username = "name"
        password = "token"
    }
}

repositories {
    githubPackage("owner/repo") //now with different credentials!
}

You may also modify each package declaration as you would a regular maven repository:

repositories {
    githubPackage("owner/repo"){
        name = "anotherName"
        credentials {
            username = "name"
            password = "token"
        }
    }
}

Notes on automatic authentication

It appears sharing a PAT is currently the encouraged solution, as seen by this post from a staff member on the GitHub community forms. In a worst case scenario, the plugin will send a detailed message explaining exactly how to set up a token.

Manual token setup

  1. Generate a token at https://github.com/settings/tokens/new?scopes=read:packages&description=GPR%20for%20Gradle
  2. Open your global gradle.properties file at ~/.gradle/gradle.properties
  3. Add username and token:
    gpr.user=<GITHUB NAME>
    gpr.key=<GENERATED TOKEN>
  4. You may need to restart your IDE

For more info see this GitHub docs page.

GITHUB_ACTOR and GITHUB_TOKEN will be used within GitHub workflows, unless the username and password are manually changed.

Plans

  • Make extension functions work nicely with Groovy (feel free to open a PR for it)
  • Archive this project if Github Packages improve support for maven

About

Gradle plugin for adding maven repos on Github Packages in one line

Topics

Resources

License

Stars

Watchers

Forks

Languages