Publish artifacts with Gradle

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

With Azure Artifacts, you can efficiently manage your dependencies from a single feed, storing various types of packages in one place. Azure Artifacts enables developers to publish and consume packages from different sources, and sharing them according to the feed's visibility settings. In this article, you'll learn how to connect to an Azure Artifacts feed and publish your packages using Gradle.

Prerequisites

Create a personal access token

To authenticate with your feed, you must first create a personal access token with packaging Read & Write scopes:

  1. Sign in to your Azure DevOps organization, and then navigate to your project.

  2. Select User settings, and then select Personal access tokens.

    A screenshot showing how to locate the personal access token button.

  3. Select New Token and fill out the required fields. Be sure to select the Packaging > Read & write scope.

  4. Select Create when you're done. Copy your token and save it in a secure location, as you'll need it for the next step.

    A screenshot demonstrating how to create a new personal access token with packaging read & write scopes.

Project setup

Before setting up your project, ensure Gradle is installed and that you've added the Maven Settings plugin to your build.gradle file as follows:

plugins {
  id 'maven-publish'
}

Configure build.gradle

  1. If a build.gradle file doesn't exist in the root of your project, create a new file and name it: build.gradle.

  2. Add the following section to your build.gradle file within both the repositories and publishing.repositories containers:

    maven {
        url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1'            //for organization-scoped feeds use this format: 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/maven/v1'
        name '<FEED_NAME>'
        credentials(PasswordCredentials)
        authentication {
            basic(BasicAuthentication)
        }
    }
    

Here's an example of what your build.gradle file should look like:

repositories {
    mavenCentral()

    maven {
    url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1'                //for organization-scoped feeds use this format: 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/maven/v1'
    name '<FEED_NAME>'
    credentials(PasswordCredentials)
    authentication {
        basic(BasicAuthentication)
        }
    }
}

publishing {
    publications {
        library(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
        url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1'           //for organization-scoped feeds use this format: 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/maven/v1'
        name '<FEED_NAME>'
        credentials(PasswordCredentials)
        authentication {
            basic(BasicAuthentication)
            }
        }
    }
}

Configure gradle.properties

  1. Open the gradle.properties file located in the .gradle directory of your home folder (~/.gradle/gradle.properties). If the file doesn't exist, create a new one.

  2. Add the following snippet, replacing the placeholders with your feed name, organization name, and the personal access token you created earlier.

    # Substitute FEED_NAME with the same name specified as the 'name' of the maven repository in build.gradle.
    # The value of the username is arbitrary but should not be blank.
    [FEED_NAME]Username=[ORGANIZATION_NAME]
    [FEED_NAME]Password=[PERSONAL_ACCESS_TOKEN]
    

Publish your packages

  1. Run the following command in your project directory to publish your package to your feed:

    gradle publish
    

A screenshot displaying a package successfully published to the feed.