Publicamos atualizações frequentes em nossa documentação, e a tradução desta página ainda pode estar em andamento. Para obter as informações mais recentes, acesse a documentação em inglês. Se houver problemas com a tradução desta página, entre em contato conosco.

Configuring Gradle for use with GitHub Packages

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

GitHub Package Registry is available with GitHub Free, GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub One. GitHub Package Registry is not available for private repositories owned by accounts using legacy per-repository plans. For more information, see "GitHub's products."

Neste artigo

Autenticar-se no GitHub Package Registry

You need an access token to publish, install, and delete packages in GitHub Package Registry. You can use a personal access token to authenticate with your username directly to GitHub Package Registry 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 Package Registry. For more information, see "About GitHub Package Registry."

You can authenticate to GitHub Package Registry 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. Como não é permitido usar letras maiúsculas, é preciso usar letras minúsculas no nome do proprietário do repositório, mesmo que o nome do usuário ou da organização no GitHub contenha letras maiúsculas.

Note: GitHub Package Registry supports SNAPSHOT versions of Apache Maven. To use a repository for downloading this type of artifact, you must enable SNAPSHOTS in your ~/.m2/settings.xml file. For an example, see "Configuring Apache Maven for use with GitHub Package Registry."

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

If you are using a GitHub Actions workflow, you can use a GITHUB_TOKEN to publish and consume packages in GitHub Package Registry without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN."

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

Publicar um pacote

By default, GitHub publishes the package to an existing repository with the same name as the package. For example, GitHub will publish a package named com.example.test in the OWNER/test GitHub Package Registry repository.

After you publish a package, you can view the package on GitHub. For more information, see "Viewing packages."

  1. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

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

    $ gradle publish
  • Instalar um pacote

    You can install a package by adding the package as a dependency to your project. Para obter mais informações, consulte "Declarar dependências" na documentação do Gradle.

    1. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

  • 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")
    }
  • 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`
    }
  • Instale o pacote.

    $ gradle install
  • Leia mais

    Pergunte a uma pessoa

    Não consegue encontrar o que procura?

    Entrar em contato