Frecuentemente publicamos actualizaciones de nuestra documentación. Es posible que la traducción de esta página esté en curso. Para conocer la información más actual, visita la documentación en inglés. Si existe un problema con las traducciones en esta página, por favor infórmanos.

Configuring Gradle for use with GitHub Packages

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

Paquetes de GitHub is available with GitHub Free, GitHub Pro, GitHub Free for organizations, GitHub Team, GitHub Enterprise Cloud, Servidor de GitHub Enterprise 2.22, and GitHub One.

En este artículo

Note: Paquetes de GitHub is currently in beta for Servidor de GitHub Enterprise 2.22. To join the beta for your instance, use the sign-up form.

Note: This package type may not be available for your instance, because site administrators can enable or disable each supported package type. For more information, see "Configuring packages support for your enterprise."

Authenticating to Paquetes de GitHub

Necesitas de un token de acceso para publicar, instalar, y borrar paquetes en Paquetes de GitHub. Puedes utilizar un token de acceso personal para autenticarte con tu nombre de usuario directamente en Paquetes de GitHub o en la API de GitHub. Cuando creas un token de acceso personal, puedes asignar al token diferentes ámbitos en función de tus necesidades.

To authenticate to Paquetes de GitHub using a GitHub Actions workflow, you must use GITHUB_TOKEN.

Authenticating with a personal access token

Debes utilizar un token de acceso personal con los alcances adecuados para publicar e instalar paquetes en Paquetes de GitHub. Para obtener más información, consulta "Acerca de Paquetes de GitHub".

You can authenticate to Paquetes de 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 REGISTRY-URL with the URL for your instance's Maven registry. If your instance has subdomain isolation enabled, use maven.HOSTNAME. If your instance has subdomain isolation disabled, use HOSTNAME/_registry/maven. In either case, replace HOSTNAME with the host name of your Servidor de GitHub Enterprise instance.

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: Paquetes de GitHub es compatible con versiones SNAPSHOT de Apache Maven. Para utilizar un repositorio para descargar este tipo de artefacto, debes habilitar las CAPTURAS (SNAPSHOTS) en tu archivo~/.m2/settings.xml file. For an example, see "Configuring Apache Maven for use with Paquetes de GitHub."

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

publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://REGISTRY-URL/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://REGISTRY-URL/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://REGISTRY-URL/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://REGISTRY-URL/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

Si estás utilizando un flujo de trabajo de GitHub Actions, puedes utilizar un GITHUB_TOKEN para publicar y consumir paquetes en Paquetes de GitHub sin que necesites almacenar y administrar un token de acceso personal. Para obtener más información, consulta la sección "Autenticarse con el GITHUB_TOKEN".

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

Publishing a package

Predeterminadamente, GitHub publica el paquete en un repositorio existente con el mismo nombre que éste. For example, GitHub will publish a package named com.example.test in the OWNER/test Paquetes de GitHub repository.

Después de que publiques un paquete, puedes verlo en GitHub. Para obtener más información, consulta "Visualizar paquetes".

  1. Autentícate en Paquetes de GitHub. Para obtener más información, consulta "Autenticar a Paquetes de 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. Autentícate en Paquetes de GitHub. Para obtener más información, consulta "Autenticar a Paquetes de GitHub."

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

    Example using Gradle 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 Gradle Groovy:

    plugins {
        id 'maven'
    }

    Example using Kotlin DSL:

    plugins {
        `maven`
    }
  4. Install the package.

    $ gradle install

Further reading

¿Te ayudó este documento?

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

O, learn how to contribute.