共用方式為


Publish Maven artifacts with Azure Pipelines

TFS 2017

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 Maven 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 your profile icon, and then select Security.

  2. Select New Token, and then name your token and set its expiration date.

  3. Select the Packaging (Read & write) scope.

    Screenshot showing the available scopes for a pat.

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

  5. 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/.

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

    vstsMavenAccessToken=<PASTE_YOUR_PERSONAL_ACCESS_TOKEN_HERE>
    
  7. Save your file when you are 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 Pipeline

  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 are 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.