Sonar & Ant

Sonar is made for maven. Nothing to do and it works.

There’s now a ant task to use sonar on your project build by ant. You’ll have the same results, except that by default, you will not have the results of your tests (passed/failed) and the coverage of your tests. To add them in your sonar pass, you have to :

  • add a cobertura task in your ant task (just follow cobertura how to, installation and adding the task). With maven, the coverage is automatically calculated when sonar is launched (mvn sonar:sonar).

  • add 2 properties (<code>sonar.cobertura.reportPath</code> and <code>sonar.surefire.reportsPath</code>) in the sonar task to include reports (test and coverage) generated by junit and cobertura tasks. They must point to the reports generated.

Here’s the sonar task that will include the two reports generated by junit and cobertura  tasks :

<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
    <!-- classpath path="path/to/sonar/ant/task/lib" / -->
    <!-- This sonar Ant task library can also be put in the ${ANT_HOME\}/lib directory-->
    <!-- In such case this classpath node is no more mandatory -->
</taskdef>

<!-- Out-of-the-box those parameters are optional -->
<!-- EXAMPLE FOR MYSQL -->
<property name="sonar.jdbc.url" value="jdbc:mysql://192.168.1.3:3306/sonar?useUnicode=true&amp;characterEncoding=utf8" />
<property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver" />
<property name="sonar.jdbc.username" value="sonar" />
<property name="sonar.jdbc.password" value="sonar" />

<target name="sonar">
    <!-- list of mandatories Sonar properties -->
    <property name="sonar.sources" value="src" />

    <!-- list of optional Sonar properties -->
    <!-- property name="sonar.projectName" value="this value overrides the name defined in Ant root node" / -->
    <!-- property name="sonar.binaries" value="list of directories which contain for example the Java bytecode" / -->
    <!-- property name="sonar.tests" value="unittest" / -->
    <!-- property name="sonar.libraries" value="list of paths to libraries separated by a comma (These libraries are for example used by the Sonar Findbugs plugin)" / -->
    <property name="sonar.cobertura.reportPath" value="${cobertura.report.dir}/coverage.xml"/>
    <property name="sonar.surefire.reportsPath" value="${build.test.dir}" />

    <sonar:sonar key="groupId:aretefactId version="0.1-SNAPSHOT" xmlns:sonar="antlib:org.sonar.ant">
        <tests>
            <path location="test" />
        </tests>
    </sonar:sonar>
</target>

I’ve put the jar of the sonar plugin in ant library directory, so the classpath to sonar is left blank. And if you’ve install the sonar plugin in eclipse, you’ll have to remember the groupId and artefactId to link the project with the sonar results. And the advantage is that you’ll have the link betwwen the violations and the source line directly in eclipse.

comments powered by Disqus