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
An Azure DevOps organization. Create an organization, if you don't have one already.
An Azure Artifacts feed. Create a feed if you don't have one already.
Download and install Gradle.
Install Java SE.
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
Sign in to your Azure DevOps organization, and then navigate to your project.
Select User settings, and then select Personal access tokens.
Select New Token, and then fill out the required fields. Make sure you select the Packaging > Read & write scope.
Select Create when you are done. Copy your token and save it in a secure location.
Configure build.gradle
If a build.gradle file does not exist in the root of your project, create a new file and name it: build.gradle.
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) } }
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
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