SonarScanner for Ant
SonarScanner for Ant
2.7.1 2021-04-30
The SonarScanner for Ant provides a task
to allow integration of SonarCloud analysis into an Apache Ant build script.
The SonarScanner for Ant is an Ant Task that wraps the SonarScanner CLI, which works by invoking the analysis 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 SonarCloud. Therefore, successful use of the SonarScanner for Ant requires strict adherence to the property names shown below.
Use
- Define a new
sonar
target in your Ant build script:
<!-- build.xml -->
<project name="My Project" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
...
<!-- Mandatory properties -->
<property name="sonar.host.url" value="https://sonarcloud.io" />
<property name="sonar.organization" value="my_organization" />
<property name="sonar.projectKey" value="my_project" />
...
<!-- Optional properties -->
<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>
- Set the environment variable
SONAR_TOKEN
with the personal access token generated on My account > Security > Generate Tokens. - Run the following command from the project base directory to launch the analysis:
ant sonar
Sample project
To help you get started, check out the sample project.
Troubleshooting
Enable Debug Logs To enable debug logs, use the regular Ant verbose option -v
:
ant sonar -v