Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
titlebuild.xml
languagehtml/xml
langxml
<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 Sonarthe SonarQube analysis -->
	<sonar:sonar />
</target>
...

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

Code Block
ant sonar

Security

SonarQube 3.7+

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 credentials of a user having been granted Execute Analysis permission have to be provided through the sonar.login and sonar.password properties. Example: sonar-runner -Dsonar.login=myLogin -Dsonar.password=myPassword

SonarQube 3.4 to 3.6.3

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=myLogin -Dsonar.password=myPassword
  • or in the build.xml file

A project cannot be anonymously accessed when either:

Prior to SonarQube 3.4

There is no security restriction.

Sample Projects

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

...

Include Page
Include - Analysis - Security
Include - Analysis - Security

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

Include Page
Include - Language Plugins Compatible with Multi-language
Include - Language Plugins Compatible with Multi-language

Include Page
Include - Converting a Mono-language Project to a Multi-language Project
Include - Converting a Mono-language Project to a Multi-language Project

Multi-module Project

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

...

  • Modules with the same structure: projects/multi-module/ant/java-ant-modules-same-structure
  • Modules with different structures: projects/multi-module/ant/java-ant-modules-different-structures

Multi-module and Multi-language Project

Since 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:

Code Block
titlemodule.sonar.language
linenumberstrue
languagebash
<property name="module1.sonar.language" value="java"/>
<property name="module2.sonar.language" value="js"/>
...

To help you getting started, a multi-language project sample is available on github that can be browsed or downloadedprojects/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. 

Migrating from SonarQube Ant Task 1.X to SonarQubeAnt Task 2.0

The SonarQubeAnt Task 2.0 can run analyses from configuration files designed for 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.