This page describes how to integrate Sonar into a build script for Apache Ant.
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 analyze the overall Parent multi-module project:
The first thing to do is to declare all the modules in the build.xml file of the Parent with the sonar.modules property:
Then, in each build.xml file of module, the project key and 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, etc.
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 Ant muli-module 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":
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.
Here is the source code of a sample Ant+Jacoco project
- Main sources and unit test sources can not be mixed in the same directory.