Build and publish artifacts with Gradle and Azure Pipelines

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

Gradle is a popular build tool for Java applications and the primary build tool for Android. Using Azure Pipelines, we can add the gradle task to our build definition and build and publish our build artifacts.

Prerequisites

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

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

    Screenshot showing how to create a personal access token

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

    Screenshot showing how to create a new personal access token.

  3. Select Create when you're done.

  1. Copy your token and save it in a secure location.

  2. Create a new file in your .gradle folder and name it gradle.properties. The path to your gradle folder is usually in %INSTALLPATH%/gradle/user/home/.gradle/.

  3. Open the gradle.properties file with a text editor and add the following snippet:

    vstsMavenAccessToken=<PASTE_YOUR_PERSONAL_ACCESS_TOKEN_HERE>
    
  4. Save your file when you're done.

Build projects with Gradle CLI

  1. Open your build.gradle file and make sure it starts with the following:

    apply plugin: 'java'
    
  2. Add the following snippet to your build.gradle file to download your artifact during the build. Replace the placeholders with your groupID, artifactID, and versionNumber. For example: `compile(group: 'siteOps', name: 'odata-wrappers', version: '1.0.0.0')

    dependencies { 
        compile(group: '<YOUR_GROUP_ID>', name: '<ARTIFACT_ID>', version: '<VERSION_NUMBER>')  
    } 
    

To test this, we can create a sample Java console app and build it with Gradle.

public class HelloWorld { 
    public static void main(String[] args) { 
        System.out.println("Hello, world!"); 
    } 
} 

Run the following command to build your project. Your build output should return: BUILD SUCCESSFUL

gradle build

Use Gradle in Azure Pipelines

  1. Run the following command to create the Gradle wrapper gradlew.

    gradle wrapper
    
  2. Push your changes to your remote branch. We will need this file later when we add the Gradle task.

  3. Navigate to your pipeline definition. If you don't have one, create a new pipeline, select Use the classic editor and then select the Gradle template.

    Screenshot showing how to use the Gradle pipeline template

  4. You can use the default settings with the gradlew build task.

    Screenshot showing the Gradle task

  5. The Publish build artifacts task will publish our artifact to Azure Pipelines.

    Screenshot showing the publish artifacts task.

  6. Select Save & queue when you're done.

  7. You can view your published artifact in your pipeline Summary once the run is complete.

    Screenshot showing the published artifact in pipeline summary.