Share via


Build Java apps

Azure DevOps Services | Azure DevOps Server | Azure DevOps Server 2022 | Azure DevOps Server 2020

Use Azure Pipelines to automate the build, test, and deployment of Java applications. This article explains how to set up a pipeline for Java projects using tools like Maven, Gradle, or Ant. You also learn how to deploy your app to Azure services like App Service, Functions, or Kubernetes.

Use a pipeline to:

If you work on an Android projects, see Build, test, and deploy Android apps.

Prerequisites

Create a GitHub repository

Fork the following repository to your GitHub account:

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

Create a pipeline

You now have a working YAML pipeline (azure-pipelines.yml) in your repository that's ready for you to customize! To make changes to your pipeline, select it in the Pipelines page, and then Edit the azure-pipelines.yml file.

Build environment

Build your code

You can build your Java app with Maven, Gradle, Ant, or a script. The following sections show you how to add a build step to your pipeline for each method.

Maven

For a Maven build, add the following tasks to the azure-pipelines.yml file. Replace the values to match your project. For more information about the task options, see the Maven task.

steps:
- task: Maven@4
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: 'default'
    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 is complete/pom.xml.

Customize the build path

Set the mavenPomFile value if the pom.xml file isn't in the root of the repo. The file path value must 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

For a Gradle build, add the following task to the azure-pipelines.yml file. For more information about these options, see the Gradle task.

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

Gradle wrapper

Ensure the gradlew file is in the repo. If it isn't, generate it by running gradle wrapper in the project's root directory. For information about creating a Gradle wrapper, see the Gradle.

Choose the version of Gradle

The version of Gradle installed on the agent machine is 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

Set the workingDirectory value if the 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, add the following task 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. If using the sample repo, you need to provide a build.xml file in your repo.

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

Script

To build with a command line or script, add one of these snippets to the 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 task 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

Publish your build output to your pipeline. Package and publish your app in a Maven package or a .war/jar file to deploy it to a web application.

Learn more about creating a CI/CD pipeline for your deployment target: