This page describes how to integrate Sonar into a build script for Apache Ant.
You must have previously installed the Ant Task analyzer.
Analyzing a Project
Define a new sonar Ant target in your Ant build script:
Execute the following command from the project base directory :
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:
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:
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":
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.
- Main sources and unit test sources can not be mixed in the same directory