Skip to content

Latest commit

 

History

History
224 lines (183 loc) · 8.52 KB

working-with-the-gradle-registry.md

File metadata and controls

224 lines (183 loc) · 8.52 KB
title intro product redirect_from versions shortTitle
Working with the Gradle registry
You can configure Gradle to publish packages to the {% data variables.product.prodname_registry %} Gradle registry and to use packages stored on {% data variables.product.prodname_registry %} as dependencies in a Java project.
{% data reusables.gated-features.packages %}
/articles/configuring-gradle-for-use-with-github-package-registry
/github/managing-packages-with-github-package-registry/configuring-gradle-for-use-with-github-package-registry
/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages
/packages/using-github-packages-with-your-projects-ecosystem/configuring-gradle-for-use-with-github-packages
/packages/guides/configuring-gradle-for-use-with-github-packages
fpt ghes ghec
*
*
*
Gradle registry

{% ifversion ghec %}

[!NOTE] The Gradle registry is not available for {% data variables.enterprise.data_residency %}.

{% endif %}

{% data reusables.package_registry.packages-ghes-release-stage %}

{% data reusables.package_registry.admins-can-configure-package-types %}

Authenticating to {% data variables.product.prodname_registry %}

{% data reusables.package_registry.authenticate-packages %}

{% data reusables.package_registry.authenticate-packages-github-token %} For more information about using GITHUB_TOKEN with Gradle, see AUTOTITLE.

Authenticating with a {% data variables.product.pat_generic %}

{% data reusables.package_registry.required-scopes %}

You can authenticate to {% data variables.product.prodname_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 {% data variables.product.pat_v1 %}. You can also configure Gradle Groovy and Kotlin DSL to recognize a single package or multiple packages in a repository.

{% ifversion ghes %} 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 {% data variables.product.prodname_ghe_server %} instance. {% endif %}

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

Note

{% data reusables.package_registry.apache-maven-snapshot-versions-supported %} For an example, see AUTOTITLE.

Example using Gradle Groovy for a single package in a repository

plugins {
    id("maven-publish")
}
publishing {
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri("https://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/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://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/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://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/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<MavenPublication>("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<PublishingExtension> {
        repositories {
            maven {
                name = "GitHubPackages"
                url = uri("https://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/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<MavenPublication>("gpr") {
                from(components["java"])
            }
        }
    }
}

Publishing a package

{% data reusables.package_registry.default-name %} For example, {% data variables.product.prodname_dotcom %} will publish a package named com.example.test in the OWNER/test {% data variables.product.prodname_registry %} repository.

{% data reusables.package_registry.viewing-packages %}

{% data reusables.package_registry.authenticate-step %}

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

     gradle publish

Using a published package

To use a published package from {% data variables.product.prodname_registry %}, add the package as a dependency and add the repository to your project. For more information, see Declaring dependencies in the Gradle documentation.

{% data reusables.package_registry.authenticate-step %}

  1. 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")
    }
  2. Add the repository to your build.gradle file (Gradle Groovy) or build.gradle.kts file (Kotlin DSL) file.

    Example using Gradle Groovy:

    repositories {
        maven {
            url = uri("https://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") ?: System.getenv("TOKEN")
            }
       }
    }

    Example using Kotlin DSL:

    repositories {
        maven {
            url = uri("https://{% ifversion fpt or ghec %}maven.pkg.github.com{% else %}REGISTRY_URL{% endif %}/OWNER/REPOSITORY")
            credentials {
                username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
                password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
            }
        }
    }

Further reading