Skip to content

Latest commit

 

History

History
214 lines (159 loc) · 7.89 KB

publish-with-gradle.md

File metadata and controls

214 lines (159 loc) · 7.89 KB
title description ms.service ms.topic ms.date ms.author author monikerRange recommendations
Publish artifacts using Gradle
Learn how to connect to a feed and publish your packages with Gradle.
azure-devops-artifacts
conceptual
10/16/2024
rabououn
ramiMSFT
<= azure-devops
true

Publish artifacts with Gradle

[!INCLUDE version-lt-eq-azure-devops]

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.

    :::image type="content" source="media/create-pat.png" alt-text="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.

    :::image type="content" source="media/gradle-pat.png" alt-text="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]
    

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 'net.linguica.maven-settings' version '0.5'
  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>'
        authentication {
            basic(BasicAuthentication)
        }
    }

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

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

    // Repositories to publish artifacts 
    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>'
            authentication {
                basic(BasicAuthentication)
            }
        }
    } 
} 
    
// Repositories to fetch dependencies
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>'
            authentication {
                basic(BasicAuthentication)
            }
        }
} 

Configure settings.xml

  1. Open the settings.xml file located in the .m2 directory of your home folder (typically found at ~/.m2/settings.xml on macOS and Linux, and at Users<YourUsername>.m2\settings.xml on Windows). If the file doesn't exist, you can 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.

    <server>
        <id>[FEED_NAME]</id>
        <username>[ORGANIZATION_NAME]</username>
        <password>[PERSONAL_ACCESS_TOKEN]</password>
    </server>

Publish your packages

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

    gradle publish

:::image type="content" source="media/publish-package-gradle.png" alt-text="A screenshot displaying a package successfully published to the feed.":::

Related content