Edit

Share via


Connect to an Azure Artifacts feed - Gradle

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

Azure Artifacts enables developers to manage project dependencies from a single feed while controlling who can view, publish, or install packages. This article walks you through setting up your project and connecting to an Azure Artifacts feed using Gradle.

Prerequisites

Product Requirements
Azure DevOps - An Azure DevOps organization.
- An Azure DevOps project.
- An Azure Artifacts feed.
- Download and install Gradle.

Project setup

  1. Make sure you've installed Gradle, then add the Maven Settings plugin to your build.gradle file:

    plugins {
      id 'maven-publish'
    }
    
  2. Sign in to your Azure DevOps organization, and then navigate to your project.

  3. Select Artifacts, select your feed from the dropdown menu, and then select Connect to feed.

  4. Select Gradle from the left navigation pane.

  5. If you don't have a build.gradle file in the root of your project, create one and name it: build.gradle.

  6. Add the snippet from the Project setup section to your build.gradle file under both the repositories and publishing.repositories blocks. Your file should look similar to the following:

    repositories {
        mavenCentral()
    
        maven {
        url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_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'  
            name '<FEED_NAME>'
            credentials(PasswordCredentials)
            authentication {
                basic(BasicAuthentication)
                }
            }
        }
    }
    
  7. Generate a Personal Access Token with Packaging > Read & write scopes, Copy it to your clipboard; you’ll use it in the next step.

  8. Open the gradle.properties file in the .gradle directory of your home folder (~/.gradle/gradle.properties). If it doesn’t exist, create a new file, then add the snippet from the Project setup section replacing the placeholder with the personal access token you just created:

    ## Substitute FEED_NAME with the same name used in your build.gradle
    ## The username value can be any non-blank string
    [FEED_NAME]Username=[ORGANIZATION_NAME]
    [FEED_NAME]Password=[PERSONAL_ACCESS_TOKEN]