共用方式為


Publish Maven artifacts using Gradle

TFS 2018

In this article, you will learn how to connect to an Azure Artifacts feed and publish Maven artifacts using Gradle.

Prerequisites

Project setup

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

plugins {
  id "net.linguica.maven-settings" version "0.5"
}

Create a personal access token

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

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

    Screenshot showing how to create a personal access token

  3. Select New Token, and then fill out the required fields. Make sure you select the Packaging > Read & write scope.

  4. Select Create when you are done. Copy your token and save it in a secure location.

    Screenshot showing how to create a new personal access token with packaging read & write scopes.

Configure build.gradle

  1. If a build.gradle file does not 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 in both the repositories and publishing.repositories containers.

    maven {
        url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1'
        name '<FEED_NAME>'
        authentication {
            basic(BasicAuthentication)
        }
    }
    
  3. Here's an example of what your build.gradle file should look like:

    publishing { 
        publications { 
            myPublication(MavenPublication) { 
                groupId '<GROUP_ID>' 
                artifactId '<ARTIFACT_ID>' 
                version '<VERSION_NUMBER>'           
                artifact '<PATH_TO_YOUR_JAR_FILE>'   
            } 
        } 
    
        // Repositories to publish artifacts 
        repositories { 
            maven {
                url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_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'
                name '<FEED_NAME>'
                authentication {
                    basic(BasicAuthentication)
                }
            }
    } 
    

Configure settings.xml

  1. Open your settings.xml file in your home directory and add the following snippet. Replace 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 artifacts

Run the following command in an elevated command prompt to publish your package to your feed. Your new package will be named: groupId:artifactId.

gradle publish