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

Prerequisites

You must have previously installed the SonarQube Ant Task and read Analyzing Source Code.

Usage

The Ant task is a thin wrapper around the SonarQube Runner, which works by invoking the runner and passing to it all properties named following a sonar.* convention. This has the downside of not being very Ant-y, but the upside of providing instant availability of any new analysis parameter introduced by a new version of a plugin or of SonarQube itself. Therefore, successful use of the SonarQube Ant task requires strict adherence to the property names shown below.

You may also consult the list of analysis parameters for additional properties which will be honored in an Ant-based analysis.

Simple Project

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

<project name="My Project" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
...
 
<!-- Define the SonarQube 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&amp;characterEncoding=utf8" />
<property name="sonar.jdbc.username" value="sonar" />
<property name="sonar.jdbc.password" value="sonar" />
...
 
<!-- Define the SonarQube project properties -->
<property name="sonar.projectKey" value="org.codehaus.sonar:example-java-ant" />
<property name="sonar.projectName" value="Simple Java Project analyzed with the SonarQube 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 SonarQube 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 the SonarQube analysis -->
	<sonar:sonar />
</target>
...

Run the following command from the project base directory to launch the analysis:

ant sonar

Sample Projects

To help you get started, a simple project sample is available on github that can be browsed or downloadedprojects/languages/java/ant/java-ant-simple

Multi-language Project

Since SonarQube 4.2, it is possible to run an analysis on a multi-language project. To do so, the sonar.language property just has to be removed. Conversely, if for some reason you want to perform a single language-only analysis, make sure sonar.language is specified.

To help you get started, a multi-language project sample can be browsed or downloaded from github: projects/languages/multi-language/multi-language-java-javascript-ant

Multi-module Project

The configuration for parent and modules must be done in the parent build.xml file:

... 
<!-- 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" />	
...

To help you getting started, project samples are available on github that can be browsed or downloaded:

Advanced Usage

Additional analysis parameters can be defined in the build.xml file or through command-line parameters.