ドキュメントには頻繁に更新が加えられ、その都度公開されています。本ページの翻訳はまだ未完成な部分があることをご了承ください。最新の情報については、英語のドキュメンテーションをご参照ください。本ページの翻訳に問題がある場合はこちらまでご連絡ください。

Configuring Gradle for use with GitHub Packages

You can configure Gradle to publish packages to GitHub パッケージレジストリ and to use packages stored on GitHub パッケージレジストリ as dependencies in a Java project.

GitHub パッケージレジストリ is available with GitHub Free, GitHub Pro, GitHub Team, and GitHub Enterprise Cloud.

In this article

Authenticating to GitHub パッケージレジストリ

You need an access token to publish, install, and delete packages in GitHub パッケージレジストリ. You can use a personal access token to authenticate with your username directly to GitHub パッケージレジストリ or the GitHub API. You can use a GITHUB_TOKEN to authenticate using a GitHub Actions workflow.

Authenticating with a personal access token

You must use a personal access token with the appropriate scopes to publish and install packages in GitHub パッケージレジストリ. For more information, see "About GitHub パッケージレジストリ."

You can authenticate to GitHub パッケージレジストリ 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. 大文字はサポートされていないため、仮にGitHubのユーザあるいはOrganization名が大文字を含んでいても、リポジトリオーナーには小文字を使わなければなりません。

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("PASSWORD")
            }
        }
    }
    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("PASSWORD")
                }
            }
        }
        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("PASSWORD")
            }
        }
    }
    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("PASSWORD")
                }
            }
        }
        publications {
            register("gpr") {
                from(components["java"])
            }
        }
    }
}

Authenticating with the GITHUB_TOKEN

You can also use a GITHUB_TOKEN to publish and consume packages in the GitHub パッケージレジストリ in a GitHub Actions workflow without storing and managing a personal access token. For more information about GITHUB_TOKEN, see "GITHUB_TOKEN secret."

You can use a script to inject your GITHUB_TOKEN into the appropriate configuration file for GitHub パッケージレジストリ. Add GitHub パッケージレジストリ as an alernative registry or repository in the configuration file for the package client, as well as your GitHub Enterprise username and a personal access token with the appropriate scopes. The exact steps for creating a configuration file vary by package type.

Publishing a package

By default, GitHub uses the package name to create a repository where the package will be published. For example, GitHub will publish a package named com.example.test in the OWNER/test GitHub パッケージレジストリ repository.

After you publish a package, you can view the package on GitHub. For more information, see “Viewing a repository's packages.”

  1. Authenticate to GitHub パッケージレジストリ. For more information, see "Authenticating to GitHub パッケージレジストリ."

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

    $ gradle publish

Installing a package

You can install a package by adding the package as a dependency to your project. For more information, see "Declaring dependencies" in the Gradle documentation.

  1. Authenticate to GitHub パッケージレジストリ. For more information, see "Authenticating to GitHub パッケージレジストリ."

  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. Install the package.

    $ gradle install

Further reading

担当者にお尋ねください

探しているものが見つからなかったでしょうか?

弊社にお問い合わせください