Build Java apps

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018

Note

The following guidance uses YAML-based pipelines available in Azure Pipelines. Use tasks that correspond to those used in the following YAML.

You can use a pipeline to automatically build and test your Java projects. After you build and test your app, you can deploy your app to Azure App Service, Azure Functions, or Azure Kubernetes Service. If you're working on an Android project, see Build, test, and deploy Android apps.

Prerequisites

You must have the following items in Azure DevOps:

Create a pipeline

  1. Fork the following repo at GitHub:

    https://github.com/MicrosoftDocs/pipelines-java
    
  2. Sign in to your Azure DevOps organization and go to your project.

  3. Go to Pipelines, and then select New pipeline.

  4. Perform the steps of the wizard by first selecting GitHub as the location of your source code. You might be redirected to GitHub to sign in. If so, enter your GitHub credentials.

  5. Select your repo. You might be redirected to GitHub to install the Azure Pipelines app. If so, select Approve & install.

  6. When you see the Configure tab, select Maven or Gradle or Ant depending on how you want to build your code.

  7. When you're ready, select Save and run.

  8. Commit a new azure-pipelines.yml file to your repo. Select Save and run again.

    If you want to watch your pipeline in action, select the build job.

    You just created and ran a pipeline, because your code appeared to be a good match for the Maven template that we automatically created for you.

    You now have a working YAML pipeline (azure-pipelines.yml) in your repo that's ready for you to customize!

  9. When you're ready to make changes to your pipeline, select it in the Pipelines page, and then Edit the azure-pipelines.yml file.

  1. Import the following repo into your Git repo in Azure DevOps Server 2019:

    https://github.com/MicrosoftDocs/pipelines-java
    
  2. Save the pipeline and queue a build. When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action. You now have a working pipeline that's ready for you to customize anytime!

  1. Import the following repo into your Git repo in TFS:

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

    This template automatically adds the tasks you need to build the code in the sample repo.

  2. Save the pipeline and queue a build. When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action. You now have a working pipeline that's ready for you to customize anytime!

Read further to learn some of the more common ways to customize your pipeline.

Build environment

You can use Azure Pipelines to build Java apps without needing to set up any infrastructure of your own. You can build on Windows, Linux, or macOS images. The Microsoft-hosted agents in Azure Pipelines have modern JDKs and other tools for Java pre-installed. To know which versions of Java are installed, see Microsoft-hosted agents.

Update the following snippet in your azure-pipelines.yml file to select the appropriate image.

pool:
  vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'

See Microsoft-hosted agents for a complete list of images.

As an alternative to using Microsoft-hosted agents, you can set up self-hosted agents with Java installed. You can also use self-hosted agents to save more time if you have a large repo or you can run incremental builds.

Your builds run on a self-hosted agent. Make sure that you have Java installed on the agent.

Build your code

Maven

With your Maven build, the following snippet gets added to your azure-pipelines.yml file. You can change values, such as the path to your pom.xml file, to match your project configuration. See the Maven task for more information about these options.

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

For Spring Boot, you can use the Maven task as well. Make sure that your mavenPomFile value reflects the path to your pom.xml file. For example, if you're using the Spring Boot sample repo, your path will be complete/pom.xml.

Customize the build path

Adjust the mavenPomFile value if your pom.xml file isn't in the root of the repo. The file path value should be relative to the root of the repo, 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.

Gradle

With the Gradle build, the following snippet gets added to your azure-pipelines.yml file. For more information about these options, see the Gradle task.

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

Choose the version of Gradle

The version of Gradle installed on the agent machine will be used unless your repo'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 repo. The directory value should be relative to the root of the repo, such as IdentityService or $(system.defaultWorkingDirectory)/IdentityService.

Adjust the gradleWrapperFile value if your gradlew file isn't in the root of the repo. The file path value should be relative to the root of the repo, 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 more information about common Java Plugin tasks for Gradle, see Gradle's documentation.

Ant

With Ant build, the following snippet is added to your azure-pipelines.yml file. Change values, such as the path to your build.xml file to match your project configuration. For more information about these options, see the Ant task.

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

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 repo. For details, see the Shell Script, Batch script, or PowerShell task.

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

Next steps

After you've built and tested your app, you can upload the build output to Azure Pipelines, create and publish a Maven package, or package the build output into a .war/jar file to be deployed to a web application.