Hallo, Entdecker! An dieser Seite wird aktiv gearbeitet, oder sie wird noch übersetzt. Die neuesten und genauesten Informationen findest Du in unserer englischsprachigen Dokumentation.

Configuring Gradle for use with GitHub Packages

You can configure Gradle to publish packages to GitHub Packages and to use packages stored on GitHub Packages as dependencies in a Java project.

GitHub Packages is available with GitHub Free, GitHub Pro, GitHub Free for organizations, GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server 2.22, GitHub One, and GitHub AE.


GitHub Packages ist nicht verfügbar für private Repositorys im Besitz von Konten mit älteren Pro-Repository-Plänen. GitHub Packages ist verfügbar mit GitHub Free, GitHub Pro, GitHub Free für Organisationen, GitHub Team, GitHub Enterprise Cloud, und GitHub One. For more information, see "[GitHub's products](/articles/github-s-products)."

Inhalt dieses Artikels

Note: When installing or publishing a docker image, GitHub Packages does not currently support foreign layers, such as Windows images.

Bei GitHub Packages authentifizieren

You need an access token to publish, install, and delete packages. Du kannst ein persönliches Zugriffstoken verwenden, um Dich mit Deinem Benutzernamen direkt bei GitHub Packages oder beim GitHub-API zu authentifizieren. When you create a personal access token, you can assign the token different scopes depending on your needs.

To authenticate using a GitHub Actions-workflow:

  • For package registries (PACKAGE-REGISTRY.pkg.github.com), you can use a GITHUB_TOKEN.
  • For the container registry (ghcr.io/OWNER/IMAGE-NAME), you can use a GITHUB_TOKEN or a personal access token. We strongly recommend you use a GITHUB_TOKEN to avoid unncessary access to your repositories.

For more information about GITHUB_TOKEN used in GitHub Actions workflows, see "Encrypted secrets" and "Authentication in a workflow."

Authenticating with a personal access token

Du musst ein persönliches Zugriffstoken mit dem entsprechenden Geltungsbereich verwenden, um Pakete in GitHub Packages zu veröffentlichen und zu installieren. Weitere Informationen findest Du unter „Über GitHub Packages."

You can authenticate to GitHub Packages with Gradle using either Gradle Groovy or Kotlin DSL by editing your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file to include your personal access token. You can also configure Gradle Groovy and Kotlin DSL to recognize a single package or multiple packages in a repository.

Replace USERNAME with your GitHub username, TOKEN with your personal access token, REPOSITORY with the name of the repository containing the package you want to publish, and OWNER with the name of the user or organization account on GitHub that owns the repository. Because uppercase letters aren't supported, you must use lowercase letters for the repository owner even if the GitHub user or organization name contains uppercase letters.

Note: GitHub Packages unterstützt SNAPSHOT Versionen von Apache Maven. To use the GitHub Packages repository for downloading SNAPSHOT artifacts, enable SNAPSHOTS in the POM of the consuming project or your ~/.m2/settings.xml file. For an example, see "Configuring Apache Maven for use with GitHub Packages."

Example using Gradle Groovy for a single package in a repository
plugins {
    id("maven-publish")
}

publishing {
    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.key") ?: System.getenv("TOKEN")
            }
        }
    }
    publications {
        gpr(MavenPublication) {
            from(components.java)
        }
    }
}
Example using Gradle Groovy for multiple packages in the same repository
plugins {
    id("maven-publish") apply false
}

subprojects {
    apply plugin: "maven-publish"
    publishing {
        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.key") ?: System.getenv("TOKEN")
                }
            }
        }
        publications {
            gpr(MavenPublication) {
                from(components.java)
            }
        }
    }
}
Example using Kotlin DSL for a single package in the same repository
plugins {
    `maven-publish`
}

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
            }
        }
    }
    publications {
        register("gpr") {
            from(components["java"])
        }
    }
}
Example using Kotlin DSL for multiple packages in the same repository
plugins {
  `maven-publish` apply false
  }

  subprojects {
  apply(plugin = "maven-publish")
  configure {
  repositories {
  maven {
  name = "GitHubPackages"
  url = uri("https://maven.pkg.github.com/OWNER/REPOSITORY")
  credentials {
  username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
  password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
  }
  }
  }
  publications {
  register("gpr") {
  from(components["java"])
  }
  }
  }
  }

Authenticating with the GITHUB_TOKEN

Wenn Du einen GitHub Actions-Workflow verwendest, kannst Du einen GITHUB_TOKEN verwenden, um Pakete in GitHub Packages zu veröffentlichen und zu konsumieren, ohne einen persönlichen Zugriffstoken speichern und verwalten zu müssen. Weitere Informationen findest Du unter „Authentifizieren mit dem GITHUB_TOKEN."

For more information about using GITHUB_TOKEN with Maven, see "Publishing Java packages with Maven."

Publishing a package

Standardmäßig veröffentlicht GitHub das Paket in ein vorhandenes Repository mit dem gleichen Namen wie das Paket. For example, GitHub will publish a package named com.example.test in the OWNER/test GitHub Packages repository.

Nachdem Du ein Paket veröffentlicht hast, kannst Du das Paket auf GitHub ansehen. Weitere Informationen findest Du unter „Anzeigen von Paketen."

  1. Authentifizieren bei GitHub Packages. Weitere Informationen findest Du unter „Bei GitHub Packages authentifizieren“.

  2. After creating your package, you can publish the package.

    $ gradle publish

Ein Paket installieren

You can install a package by adding the package as a dependency to your project. Weitere Informationen finden Sie unter „Declaring dependencies“ (Abhängigkeiten festlegen) in der Gradle-Dokumentation.

  1. Authentifizieren bei GitHub Packages. Weitere Informationen findest Du unter „Bei GitHub Packages authentifizieren“.

  2. Add the package dependencies to your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file.

    Example using Grady Groovy:

    dependencies {
    implementation 'com.example:package'
    }

    Example using Kotlin DSL:

    dependencies {
    implementation("com.example:package")
    }
  3. Add the maven plugin to your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file.

    Example using Grady Groovy:

    plugins {
    id 'maven'
    }

    Example using Kotlin DSL:

    plugins {
    `maven`
    }
  4. Installieren Sie das Paket.

    $ gradle install

Weiterführende Informationen

Did this doc help you?

Privacy policy

Help us make these docs great!

All GitHub docs are open source. See something that's wrong or unclear? Submit a pull request.

Make a contribution

Oder, learn how to contribute.