...
This page describes how to integrate Sonar SonarQube into a build script for Apache Ant.
Prerequisites
You must have previously installed the Sonar SonarQube Ant Task and read Analyzing Code Source.
| Note | ||
|---|---|---|
| ||
This page describes how to use the Sonar Ant Task 2.0+ |
Usage
Simple Project
Define a new Sonar sonar Ant target in your Ant build script:
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
... <!-- Define the SonarSonarQube global properties (the most usual way is to pass these properties via the command line) --> <property name="sonar.jdbc.url" value="jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8" /> <property name="sonar.jdbc.username" value="sonar" /> <property name="sonar.jdbc.password" value="sonar" /> ... <!-- Define the SonarSonarQube project properties --> <property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" /> <property name="sonar.projectName" value="Simple Java Project analyzed with the SonarSonarQube Ant Task" /> <property name="sonar.projectVersion" value="1.0" /> <property name="sonar.language" value="java" /> <property name="sonar.sources" value="src" /> <property name="sonar.binaries" value="build/*.jar" /> ... <!-- Define the SonarSonarQube target --> <target name="sonar"> <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml"> <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder --> <classpath path="path/to/sonar/ant/task/lib/sonar-ant-task-*.jar" /> </taskdef> <!-- Execute Sonar --> <sonar:sonar /> </target> ... |
Run the following command from the project base directory to launch the Sonar analysis:
| Code Block |
|---|
ant sonar |
Security
Since Sonar SonarQube 3.4, if a project cannot be accessed anonymously, the 'sonar.login' and 'sonar.password' properties are required to run an analysis on this project. These properties have to be set to the credentials of a user having the 'User' role on this project. You can set them either:
- directly on the command line by adding
-Dsonar.login=myUser -Dsonar.password=myPassword - or in the build.xml file
A project cannot be anonymously accessed when either:
- the '
sonar.forceAuthentication' property is set to ' true' - or the 'the
sonar.forceAuthentication' property is set to 'false' and the 'Anyone' group has not been granted a 'User' role on the project
Sample Projects
To help you getting started, a simple project sample is available on github that can be browsed or downloaded: projects projects/languages/java/ant/java-ant-simple
Multi-module Project
The Sonar configuration for parent and modules must be done in the parent build.xml file:
| Code Block | ||||||
|---|---|---|---|---|---|---|
| ||||||
... <!-- Set modules IDs --> <property name="sonar.modules" value="module-one,module-two"/> <!-- For modules, properties are inherited from the parent. They can be overridden as shown below: --> <property name="module-one.sonar.projectName" value="Module One" /> <property name="module-one.sonar.sources" value="sources/java" /> <property name="module-one.sonar.binaries" value="target" /> <!-- Default module base directory is <curent_directory>/<module_ID>. It can be overridden if necessary --> <property name="module-one.sonar.projectBaseDir" value="Module 1" /> ... |
...
- Modules with the same structure: projects/multi-module/ant/java-ant-modules-same-structure
- Modules with different structures: projects projects/multi-module/ant/java-ant-modules-different-structures
Multi-module and Multi-language Project
Since Sonar SonarQube 3.3, it is possible to run an analysis on a multi-module project whose modules contains source code from different languages.
In addition to the multi-module configuration, the only mandatory property to set is the language for each module:
...
To help you getting started, a multi-language project sample is available on github that can be browsed or downloaded: projects projects/languages/multi-language/multi-language-java-javascript-ant
Advanced Usage
Additional analysis parameters can be defined in the build.xml file or through command-line parameters.
Known Limitations
- Main sources and unit test sources can not be mixed in the same directory
Migrating from
...
SonarQube Ant Task 1.X to
...
SonarQubeAnt Task 2.0
The Sonar Ant SonarQubeAnt Task 2.0 can run analyses from configuration files designed for Sonar SonarQube Ant Task 1.X.
This compatibility mode is automatically activated. But we highly recommend you to migrate to the new format as this compatibility mode will be dropped sooner or later in the next versions.

