Java

Note

This guidance uses YAML-based pipelines available in Azure Pipelines. For TFS, use tasks that correspond to those used in the YAML below.

This guidance explains how to build Java projects. See Android for Android-specific projects.

Example

To get started using a sample Java project, fork this repository in GitHub, or import it into Azure Repos or TFS:

https://github.com/MicrosoftDocs/pipelines-java

The sample code includes an azure-pipelines.yml file at the root of the repository. You can use this file to build the project.

Follow instructions in Create your first pipeline to create a build pipeline for the sample project.

The rest of this topic describes ways to customize your Java build pipeline.

Choose an agent

You can use Azure Pipelines to build your Java projects on Microsoft-hosted agents that include modern JDKs and other tools for Java. Or, you can use self-hosted agents with specific tools that you need.

Create a file named azure-pipelines.yml in the root of your repository. Then, add the following snippet to your azure-pipelines.yml file to select the appropriate agent pool:

# https://docs.microsoft.com/azure/devops/pipelines/languages/java
pool:
  vmImage: 'Ubuntu 16.04' # Other options: 'macOS 10.13', 'VS2017-Win2016'

Build your code with Maven

To build with Maven, add the following snippet to your azure-pipelines.yml file. Change values, such as the path to your pom.xml file, to match your project configuration. See the Maven task for more about these options.

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.10'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    goals: 'package'

Customize Maven

Customize the build path

Adjust the mavenPomFile value if your pom.xml file isn't in the root of the repository. The file path value should be relative to the root of the repository, such as IdentityService/pom.xml or $(system.defaultWorkingDirectory)/IdentityService/pom.xml.

Customize Maven goals

Set the goals value to a space-separated list of goals for Maven to execute, such as clean package.

For details about common Java phases and goals, see Apache's Maven documentation.

Build your code with Gradle

To build with Gradle, add the following snippet to your azure-pipelines.yml file. See the Gradle task for more about these options.

steps:
- task: Gradle@2
  inputs:
    workingDirectory: ''
    gradleWrapperFile: 'gradlew'
    gradleOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.10'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'
    tasks: 'build'

Customize Gradle

Choose the version of Gradle

The version of Gradle installed on the agent machine will be used unless your repository's gradle/wrapper/gradle-wrapper.properties file has a distributionUrl property that specifies a different Gradle version to download and use during the build.

Adjust the build path

Adjust the workingDirectory value if your gradlew file isn't in the root of the repository. The directory value should be relative to the root of the repository, such as IdentityService or $(system.defaultWorkingDirectory)/IdentityService.

Adjust the gradleWrapperFile value if your gradlew file isn't in the root of the repository. The file path value should be relative to the root of the repository, such as IdentityService/gradlew or $(system.defaultWorkingDirectory)/IdentityService/gradlew.

Adjust Gradle tasks

Adjust the tasks value for the tasks that Gradle should execute, such as build or check.

For details about common Java Plugin tasks for Gradle, see Gradle's documentation.

Build your code with Ant

To build with Ant, add the following snippet to your azure-pipelines.yml file. Change values, such as the path to your build.xml file, to match your project configuration. See the Ant task for more about these options.

steps:
- task: Ant@1
  inputs:
    workingDirectory: ''
    buildFile: 'build.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.10'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: false
    testResultsFiles: '**/TEST-*.xml'

Build your code using a command line or script

To build with a command line or script, add one of the following snippets to your azure-pipelines.yml file.

Inline script

The script: step runs an inline script using Bash on Linux and macOS and Command Prompt on Windows. For details, see the Bash or Command line task.

steps:
- script: |
    echo Starting the build
    mvn package
  displayName: 'Build with Maven'

Script file

This snippet runs a script file that is in your repository. For details, see the Shell Script, Batch script, or PowerShell task.

steps:
- task: ShellScript@2
  inputs:
    scriptPath: 'build.sh'