Maven task

Azure Pipelines | Azure DevOps Server 2019 | TFS 2018 | TFS 2017 | TFS 2015

Use this task in a build or release pipeline to build your Java code.

Demands

The build agent must have the following capability:

  • Maven

YAML snippet

# Maven
# Build, test, and deploy with Apache Maven
- task: Maven@3
  inputs:
    #mavenPomFile: 'pom.xml' 
    #goals: 'package' # Optional
    #options: # Optional
    #publishJUnitResults: true 
    #testResultsFiles: '**/surefire-reports/TEST-*.xml' # Required when publishJUnitResults == True
    #testRunTitle: # Optional
    #codeCoverageToolOption: 'None' # Optional. Options: none, cobertura, jaCoCo. Enabling code coverage inserts the `clean` goal into the Maven goals list when Maven runs.
    #codeCoverageClassFilter: # Optional. Comma-separated list of filters to include or exclude classes from collecting code coverage. For example: +:com.*,+:org.*,-:my.app*.*
    #codeCoverageClassFilesDirectories: # Optional
    #codeCoverageSourceDirectories: # Optional
    #codeCoverageFailIfEmpty: false # Optional
    #javaHomeOption: 'JDKVersion' # Options: jDKVersion, path
    #jdkVersionOption: 'default' # Optional. Options: default, 1.11, 1.10, 1.9, 1.8, 1.7, 1.6
    #jdkDirectory: # Required when javaHomeOption == Path
    #jdkArchitectureOption: 'x64' # Optional. Options: x86, x64
    #mavenVersionOption: 'Default' # Options: default, path
    #mavenDirectory: # Required when mavenVersionOption == Path
    #mavenSetM2Home: false # Required when mavenVersionOption == Path
    #mavenOptions: '-Xmx1024m' # Optional
    #mavenAuthenticateFeed: false 
    #effectivePomSkip: false 
    #sonarQubeRunAnalysis: false 
    #sqMavenPluginVersionChoice: 'latest' # Required when sonarQubeRunAnalysis == True# Options: latest, pom
    #checkStyleRunAnalysis: false # Optional
    #pmdRunAnalysis: false # Optional
    #findBugsRunAnalysis: false # Optional

Arguments

ArgumentDescription
Maven POM file(Required) Relative path from the repository root to the Maven POM file. See Introduction to the POM.
Goal(s)In most cases, set this to package to compile your code and package it into a .war file. If you leave this argument blank, the build will fail. See Introduction to the Maven build lifecycle.
Options(Optional) Specify any Maven command-line options you want to use.
Publish to Azure Pipelines(Required) Select this option to publish JUnit test results produced by the Maven build to Azure Pipelines. Each test results file matching Test Results Files will be published as a test run in Azure Pipelines.
Test results files(Required) Specify the path and pattern of test results files to publish. Wildcards can be used (more information). For example, */TEST-.xml for all XML files whose name starts with TEST-. If no root path is specified, files are matched beneath the default working directory, the value of which is available in the variable: $(System.DefaultWorkingDirectory). For example, a value of '/TEST-.xml' will actually result in matching files from '$(System.DefaultWorkingDirectory)//TEST-.xml'.
Test run title(Optional) Provide a name for the test run.
Code coverage tool(Optional) Select the code coverage tool. Enabling code coverage inserts the clean goal into the Maven goals list when Maven runs.
Class inclusion/exclusion filters(Optional) Comma-separated list of filters to include or exclude classes from collecting code coverage. For example: +:com.,+:org.,-:my.app..
Class files directories(Optional) This field is required for a multi-module project. Specify a comma-separated list of relative paths from the Maven POM file to directories containing class files and archive files (JAR, WAR, etc.). Code coverage is reported for class files in these directories. For example: target/classes,target/testClasses.
Source files directories(Optional) This field is required for a multi-module project. Specify a comma-separated list of relative paths from the Maven POM file to source code directories. Code coverage reports will use these to highlight source code. For example: src/java,src/Test.
Fail when code coverage results are missing(Optional) Fail the build if code coverage did not produce any results to publish.
Set JAVA_HOME by(Required) Sets JAVA_HOME either by selecting a JDK version that will be discovered during builds or by manually entering a JDK path.
JDK version(Optional) Will attempt to discover the path to the selected JDK version and set JAVA_HOME accordingly.
JDK path(Required) Sets JAVA_HOME to the given path.
JDK architecture(Optional) Optionally supply the architecture (x86, x64) of the JDK.
Maven version(Required) Uses either the default Maven version or the version in the specified custom path.
Maven path(Required) Supply the custom path to the Maven installation (e.g., /usr/share/maven).
Set M2_HOME variable(Required) Sets the M2_HOME variable to a custom Maven installation path.
Set MAVEN_OPTS to(Optional) Sets the MAVEN_OPTS environment variable, which is used to send command-line arguments to start the JVM. The -Xmx flag specifies the maximum memory available to the JVM.
Authenticate built-in Maven feeds(Required) Automatically authenticate Maven feeds from Azure Artifacts. If built-in Maven feeds are not in use, deselect this option for faster builds.
Skip generating effective POM while authenticating built-in feeds(Required) Authenticate built-in Maven feeds using the POM only, allowing parent POMs in Azure Artifacts/Azure DevOps Server [Package Management] feeds.
Run SonarQube or SonarCloud analysis(Required) This option has changed from version 1 of the **Maven task to use the SonarQube and SonarCloud marketplace extensions. Enable this option to run SonarQube or SonarCloud analysis after executing goals in the Goals field. The install or package goal should run first. You must also add a Prepare Analysis Configuration task from one of the extensions to the build pipeline before this Maven task.
SonarQube scanner for Maven version(Required) The SonarQube Maven plugin version to use. You can use latest version, or rely on the version in your pom.xml.
Run Checkstyle(Optional) Run the Checkstyle tool with the default Sun checks. Results are uploaded as build artifacts.
Run PMD(Optional) Use the PMD static analysis tool to look for bugs in the code. Results are uploaded as build artifacts.
Run FindBugs(Optional) Use the FindBugs static analysis tool to look for bugs in the code. Results are uploaded as build artifacts.
Control options

Example

Build and Deploy your Java application to an Azure Web App

Open source

This task is open source on GitHub. Feedback and contributions are welcome.

Q & A

Do I need an agent?

You need at least one agent to run your build or release. Get an agent for Linux, macOS, or Windows.

I'm having problems. How can I troubleshoot them?

See Troubleshoot Build and Release.

I can't select a default agent pool and I can't queue my build or release. How do I fix this?

See Agent pools.

I use TFS on-premises and I don't see some of these features. Why not?

Some of these features are available only on Azure Pipelines and not yet available on-premises. Some features are available on-premises if you have upgraded to the latest version of TFS.