# SonarScanner for Ant

<details>

<summary>SonarScanner for Ant — 2.7.1 | <a href="https://sonarsource.atlassian.net/jira/software/c/projects/ANTTASK/issues">Issue Tracker</a></summary>

**2.7.1** <sup><sub>**2021-04-30**<sub></sup>\ <sup>Update dependencies</sup>\
[Download](https://binaries.sonarsource.com/Distribution/sonarqube-ant-task/sonarqube-ant-task-2.7.1.1951.jar)\
\
[Release notes](https://sonarsource.atlassian.net/issues/?jql=project+%3D+10136+AND+fixVersion+%3D+12372)

***

**2.7** <sup><sub>**2019-10-01**<sub></sup>\ <sup>Support SONAR\_HOST\_URL environment variable to configure the server URL</sup>\
[Download](https://binaries.sonarsource.com/Distribution/sonarqube-ant-task/sonarqube-ant-task-2.7.0.1612.jar)\
\
[Release notes](https://sonarsource.atlassian.net/issues/?jql=project+%3D+10136+AND+fixVersion+%3D+12371)

</details>

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

The SonarScanner for Ant is an Ant Task that is a wrapper of [SonarScanner](/sonarqube-server/9.8/analyzing-source-code/scanners/sonarscanner.md), which works by invoking SonarScanner and passing to it all [Analysis parameters](/sonarqube-server/9.8/analyzing-source-code/analysis-parameters.md) 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 SonarQube. Therefore, successful use of the SonarScanner for Ant requires strict adherence to the property names shown below.

## Using the SonarScanner for Ant <a href="#using-sonarscanner-for-ant" id="using-sonarscanner-for-ant"></a>

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

```css-79elbk
<!-- 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 SonarScanner 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 SonarScanner 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 SonarScanner for Ant Analysis -->
    <sonar:sonar />
</target>
```

Run the following command from the project base directory to launch the analysis. You need to pass an [Generating and using tokens](/sonarqube-server/9.8/user-guide/user-account/generating-and-using-tokens.md) using the `sonar.login` property in your command line:

```css-79elbk
ant sonar -Dsonar.login=yourAuthenticationToken
```

## Sample project <a href="#sample-project" id="sample-project"></a>

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

## Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

**Enable debug logs**

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

```css-79elbk
ant sonar -v
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sonarsource.com/sonarqube-server/9.8/analyzing-source-code/scanners/sonarscanner-for-ant.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
