5

I have the following Sonar Ant target defined:

<target name='sonar'>
    <property name='sonar.sources' value='${src.dir}'/>
    <property name='sonar.tests' value='${test.src.dir}'/>
    <property name='sonar.binaries' value='build/classes'/>
    <path id='jars'>
        <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
        <fileset dir='build/lib/test' includes='*.jar'/>
    </path>
    <pathconvert property='sonar.libraries' refid='jars' pathsep=','/>

    <exec executable='p4' outputproperty='p4.P4CLIENT'>
        <arg value='set'/>
        <arg value='P4CLIENT'/>
    </exec>
    <propertyregex
            property='p4client'
            input='${p4.P4CLIENT}'
            regexp='P4CLIENT=([^ ]+) *.*'
            replace='\1'/>
    <propertyregex
            property='sonar.timestamp'
            input='${build.time}'
            regexp='_'
            replace='T'/>
    <sonar:sonar key='com.netflix:${module.name}' version='${p4client}@${sonar.timestamp}' xmlns:sonar='antlib:org.sonar.ant'/>

    <property name='sonar.dynamicAnalysis' value='reuseReports'/>
    <property name='sonar.emma.reportPath' value='${coverage.dir}'/>
</target>

When I run 'ant sonar' and bring up Sonar in my browser, I see info about the classes in the src directory, but nothing about the stuff in the test directory.

If I add ${test.src.dir} to sonar.sources and not set sonar.tests, I see some info about the test classes, but Sonar still reports 0 Test Successes.

How do I get it so I can drill down to each test method and their stats?

mac
  • 2,672
  • 4
  • 31
  • 43
Noel Yap
  • 18,822
  • 21
  • 92
  • 144

2 Answers2

6

For anyone else that runs across this issue, I finally got Sonar to report on our Emma Code coverage. The first problem was that the Emma plugin did not come with the version of Sonar I was using (3.1.1). I had to download it and install it to the extensions/plugins directory of Sonar and restart it.

Then I had to set the following properties in my build.xml:

<property name="sonar.core.codeCoveragePlugin" value="emma" />
<property name="sonar.emma.reportPath" value="${coverage.dir}" />

After this, I atleast saw the following output after running the Sonar ant task:

[sonar:sonar] 13:41:49.705 WARN        org.sonar.INFO - No coverage (*.ec) file found in /my/local/path
[sonar:sonar] 13:41:49.708 WARN        org.sonar.INFO - No metadata (*.em) file found in /my/local/path

After some digging, I found that inside of the Sonar Emma plugin, it is hard-coded to look for a .ec (coverage) file and a .em (metadata) file. Unfortunately, my coverage file had a .emma extension as did my metadata file and I was unable to rename them as it would break other functionality. So I wrote the following Ant task to copy the files to match the naming standard that the Sonar Emma plugin expects.

<target name="createEmmaFilesWithSonarNamingStandard" depends="defineAntContribTasks">
    <if>
        <available file="${coverage.dir}/metadata.emma" />
        <then>
            <copyfile src="${coverage.dir}/metadata.emma" dest="${coverage.dir}/metadata.em" />
        </then>
    </if>
    <if>
        <available file="${coverage.dir}/coverage.emma" />
        <then>
            <copyfile src="${coverage.dir}/coverage.emma" dest="${coverage.dir}/coverage.ec" />
        </then>
    </if>
</target>

After running this again, I came across a new problem:

org.sonar.api.utils.SonarException: java.io.IOException: cannot read [/my/local/path/build/coverage/metadata.em]: created by another EMMA version [2.0.5312]

After some more digging, I found that the Sonar Emma 1.0.1 plugin was compiled against Emma 2.0.5312 and the Sonar Emma 1.1 and 1.2.x against Emma version 2.1.5320 as stated on the Sonar Emma plugin page.

I downloaded the 2.1.5320 version of Emma, replaced both emma.jar as well as emma_ant.jar in my Ant lib directory. After a clean re-compile and test, I was able to re-run the Sonar Ant task and have my code coverage reflected on Sonar.

mac
  • 2,672
  • 4
  • 31
  • 43
frankjl
  • 404
  • 3
  • 12
2

The property 'sonar.surefire.reportsPath' needs to be defined before the definition of the sonar target.

The following definition gets the test info exported (although it's still not exporting coverage info):

<property name='sonar.surefire.reportsPath' value='${test.dir}'/>

<property name='sonar.dynamicAnalysis' value='reuseReports'/>
<property name='sonar.emma.reportPath' value='${coverage.report.dir}'/>

<target name='sonar'>
    <property name='sonar.sources' value='${src.dir}'/>
    <property name='sonar.tests' value='${test.src.dir}'/>
    <property name='sonar.binaries' value='${build.dir}'/>
    <path id='jars'>
        <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
        <fileset dir='${ivy.lib.dir}/test' includes='*.jar'/>
    </path>
    <pathconvert property='sonar.libraries' refid='jars' pathsep=','/>

    <exec executable='p4' outputproperty='p4.P4CLIENT'>
        <arg value='set'/>
        <arg value='P4CLIENT'/>
    </exec>
    <propertyregex
            property='p4client'
            input='${p4.P4CLIENT}'
            regexp='P4CLIENT=([^ ]+) *.*'
            replace='\1'/>
    <propertyregex
            property='sonar.timestamp'
            input='${build.time}'
            regexp='_'
            replace='T'/>

    <sonar:sonar key='com.netflix:${module.name}' version='${p4client}@${sonar.timestamp}' xmlns:sonar='antlib:org.sonar.ant'/>
</target>
Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • Hi I'm having a [similar problem](http://stackoverflow.com/questions/21267948/sonarqube-test-coverage-report-for-android-built-with-ant) but not quite the same... how are you guys getting a .emma file out? – JaKXz Jan 21 '14 at 23:42
  • We've actually stopped using Sonar and will be building our own build data repository. – Noel Yap Jan 22 '14 at 17:18