This page describes how to integrate Sonar into a build script for Apache Ant.
- Ant 1.7.1 or higher
- Java 1.5 or higher
- Sonar 2.8 or higher. The server must be up while running the Ant task.
but without support of multi-modules
The way to configure the Sonar Ant task changed between version 1.0 and version 1.1, so if you must use the Ant task version 1.0 please use this documentation page.
Older versions of Sonar are not supported.
- Download the Sonar Ant Task
Declare the Sonar Ant Task and define the configuration in a common Ant script file :
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.
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 :
Analyse multi-modules 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 :
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 the source code of a sample muli-module Ant project
- Main sources and unit test sources can not be mixed in the same directory