5

我定义了以下 Sonar Ant 目标:

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

当我在浏览器中运行“ant sonar”并打开 Sonar 时,我看到了 src 目录中有关类的信息,但没有看到 test 目录中的内容。

如果我将 ${test.src.dir} 添加到 sonar.sources 而没有设置 sonar.tests,我会看到一些有关测试类的信息,但 Sonar 仍然报告 0 测试成功。

我如何获得它,以便深入了解每种测试方法及其统计信息?

4

2 回答 2

6

对于遇到此问题的其他任何人,我终于让 Sonar 报告我们的 Emma Code 覆盖率。第一个问题是我使用的 Sonar 版本(3.1.1)没有附带 Emma 插件。我必须下载它并将其安装到extensions/pluginsSonar 的目录并重新启动它。

然后我必须在我的 build.xml 中设置以下属性:

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

在此之后,我在运行 Sonar ant 任务后至少看到了以下输出:

[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

经过一番挖掘,我发现在Sonar Emma 插件内部,它被硬编码为查找 .ec(覆盖)文件和 .em(元数据)文件。不幸的是,我的覆盖文件和我的元数据文件一样有一个 .emma 扩展名,我无法重命名它们,因为它会破坏其他功能。所以我编写了以下 Ant 任务来复制文件以匹配 Sonar Emma 插件所期望的命名标准。

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

再次运行后,我遇到了一个新问题:

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]

经过一番挖掘,我发现 Sonar Emma 1.0.1 插件是针对 Emma 2.0.5312 编译的,Sonar Emma 1.1 和 1.2.x 是针对 Emma 版本 2.1.5320 编译的,如Sonar Emma 插件页面所述。

我下载了 Emma 的 2.1.5320 版本,替换了我的 Ant lib 目录中的emma.jaremma_ant.jar。经过干净的重新编译和测试后,我能够重新运行 Sonar Ant 任务并将我的代码覆盖率反映在 Sonar 上。

于 2012-06-28T18:29:19.890 回答
2

需要在定义声纳目标之前定义属性“sonar.surefire.reportsPath”。

以下定义获取导出的测试信息(尽管它仍未导出覆盖率信息):

<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>
于 2011-08-28T17:54:17.597 回答