Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 49 Next »

This page describes how to integrate Sonar into a build script for Apache Ant.

Table of Contents



You must have previously installed the Ant Task analyzer.

Analyzing a Project

  1. Define a new sonar Ant target in your Ant build script:


    Declaring a XML namespace for Ant tasks is optional but always recommended if you mix tasks from different libraries.


    The Definition for key is groupId:artifactId. It has to consist of 2 values separated by colon (":").


    For advanced features such as reusing existing unit test reports, you can define advanced parameters. These parameters are set with the <property> nodes of Sonar task.

  2. Execute the following command from the project base directory :


    It's recommended to build the project before, usually to get bytecode and unit test reports.


    The Sonar Ant task stores all temporary files in a '.sonar' directory which is automatically created by the Ant task in the working directory.


    Since version 1.2 you can specify sonar.libraries using path-like structure as following :

Analyzing a multi-module project

Let's say that we have a project "Parent" containing two modules "Child1" and "Child2" and we want Sonar to be able to analyse the overall Parent multi-modules project :

The first thing to do is to declare all modules in the build.xml file of the Parent with the "sonar.modules" property:

Known limitation


Spaces and return carriages are not supported in the sonar.modules property value: see SONARPLUGINS-1853.


Then, in each build.xml file of module, the Sonar key and Sonar source directories properties must be defined:

Of course additional but optional Sonar properties can also be defined in each module to provide for instance the binary directories, the list of libraries, ...

You can ask the Sonar task to execute an Ant target on the parent and for each module before collecting those property values. In that case, a new attribute "initTarget" must be added to the Sonar task definition in the build.xml file of the Parent:


Here is the source code of a sample muli-module Ant project

Using Jacoco with Ant projects

Some additional configuration is required with Ant projects.

The Ant build.xml file must contain a dedicated target to launch either junit or testng unit tests. Let's take the following example with the target "test":


The nested task always has to declare fork="true", otherwise JaCoCo agent can't be attached. In addition task should declare forkmode="once" to avoid starting a new JVM for every single test case and decreasing execution performance dramatically (unless this is required by the nature of the test cases).

Then, in this example, the value of the parameter "sonar.jacoco.antTargets" must be set to "test". During Sonar analysis, the Sonar Jacoco plugin will take care to attach the Jacoco agent to the JVM and to launch the unit tests. 


Only specified targets would be executed. So for instance, if "test" target depends on a "compile" target, Jacoco plugin will only execute "test" target without executing first the "compile" target. In such case, a solution can be to update the "sonar" target in order declare a dependency on the "compile" target.


Here is the source code of a sample Ant+Jacoco project

Known Limitations

  • Main sources and unit test sources can not be mixed in the same directory
  • No labels