Publish Maven artifacts using Gradle
Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018
This topic covers creating and publishing a Maven artifact to an Azure Artifacts feed with Gradle.
Prerequisites
Install Gradle build tool.
Java SE Downloads required for Gradle.
To make sure you have all the prerequisites set up, run the following command in an elevated command prompt to check which Java version is installed on your machine.
java -version
If the above command doesn't return a java version, make sure you go back and install the Java JDK or JRE first.
To confirm the installation of Gradle, run the following command in an elevated command prompt:
gradle -v
Set up authentication
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.
Select your profile icon, and then select Security.
Select New Token, and then name your token and set its expiration date.
Select the Packaging (Read & write) scope.
Copy your token and save it in a secure location.
Create a new file in your
.gradlefolder and name it gradle.properties. The path to your gradle folder is usually in%INSTALLPATH%/gradle/user/home/.gradle/.Open the gradle.properties file with a text editor and add the following snippet:
vstsMavenAccessToken=<PASTE_YOUR_PERSONAL_ACCESS_TOKEN_HERE>Save your file when you are done.
Configure build.gradle
Create a new file in the root of your project and name it build.gradle.
Open the new file with a text editor, and paste in the following sample code. Replace the placeholders with the appropriate values.
apply plugin: 'java' apply plugin: 'maven-publish' publishing { publications { myPublication(MavenPublication) { groupId '<GROUP_ID>' artifactId '<ARTIFACT_ID>' version '<VERSION_NUMBER>' // Your package version artifact '<PATH_TO_YOUR_JAR_FILE>' //Example: *./target/myJavaClasses.jar* } } // Repositories *to* which Gradle can publish artifacts repositories { maven { url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1' credentials { username "<FEED_NAME>" password System.getenv("SYSTEM_ACCESSTOKEN") != null ? System.getenv("SYSTEM_ACCESSTOKEN") : vstsMavenAccessToken } } } } // Repositories *from* which Gradle can download dependencies repositories { maven { url 'https://pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/maven/v1' credentials { username "<FEED_NAME>" password System.getenv("SYSTEM_ACCESSTOKEN") != null ? System.getenv("SYSTEM_ACCESSTOKEN") : vstsMavenAccessToken } } }
In the above example, we are publishing and downloading artifacts from the same organization. You can also configure publishing and downloading to use separate organizations. See Predefined variables to lean more about pipeline variables and the System.AccessToken security token.
Publish Maven packages with Gradle
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
Related articles
Feedback
Issottometti u ara feedback għal