Skip to end of metadata
Go to start of metadata
By SonarSource – GNU LGPL 3 – Issue TrackerSources
SonarQube Scanner for Ant 2.5 – Compatible with SonarQube 5.6+ (LTS)
Download

Table of Contents

Features

The SonarQube Scanner for Ant provides a task to allow integration of SonarQube analysis into an Apache Ant build script.

Compatibility

The SonarQube Scanner for Ant version 2.5 is compatible with SonarQube 5.6+.

Prerequisites

  • SonarQube is already installed
  • At least the minimal version of Java supported by your SonarQube server is in use
  • The language plugins for each of the languages you wish to analyze are installed
  • You have read Analyzing Code Source
  • Ant 1.7.1 or higher

Installation

Just download the SonarQube Scanner for Ant on your machine.

Usage

The SonarQube Scanner for Ant is an Ant Task that is wrapper of SonarQube Scanner, which works by invoking SonarQube Scanner 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 Scanner for Ant 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:

build.xml
<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.host.url" value="http://localhost:9000" />

...
 
<!-- Define the SonarQube project properties -->
<property name="sonar.projectKey" value="org.sonarqube:sonarqube-scanner-ant" />
<property name="sonar.projectName" value="Example of SonarQube Scanner for Ant Usage" />
<property name="sonar.projectVersion" value="1.0" />
<property name="sonar.sources" value="src" />
<property name="sonar.java.binaries" value="build" />
<property name="sonar.java.libraries" value="lib/*.jar" />
...

<!-- Define SonarQube Scanner for Ant Target -->
<target name="sonar">
	<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
		<!-- Update the following line, or put the "sonarqube-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
		<classpath path="path/to/sonar/ant/task/lib/sonarqube-ant-task-*.jar" />
	</taskdef>

	<!-- Execute SonarQube Scanner for Ant Analysis -->
	<sonar:sonar />
</target>
...

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

ant sonar

Security

Any user who's granted Execute Analysis permission can run an analysis.

If the Anyone group is not granted Execute Analysis permission or if the SonarQube instance is secured (the sonar.forceAuthentication property is set to true), the analysis token of a user with Execute Analysis permission must be provided through the sonar.login property. Example: sonar-scanner -Dsonar.login=[my analysis token]

Sample Project

To help you get started, a simple project sample is available here: https://github.com/SonarSource/sonar-scanning-examples/tree/master/sonarqube-scanner-ant

Multi-Module Project

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

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

Enable Debug Logs

To enable debug logs, use regular Ant verbose option: -v 

ant sonar -v

Advanced Usage

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

 

  • No labels