Ant, Sonar - 0% code coverage

2019-07-20 17:35发布

I have problem with ant sonar task. This task end with success but don't run unit tests and don't show code coverage.

Test task

 <target name="test" depends=".....">
    <path id="classpath">
        <fileset dir="test/lib" includes="**/*.jar"/>
        <fileset dir="lib" includes="**/*.jar"/>
        <pathelement location="......."/>
    </path>

    <mkdir dir="build/tests"/>
    <javac srcdir="test/src" destdir="build/tests" includes="**/*.java" debug="${debug}" deprecation="${deprecation}" optimize="${optimize}" nowarn="${nowarn}" fork="true">
        <classpath refid="classpath"/>
    </javac>
    <copy todir="build/tests">
        <fileset dir="test/src" excludes="**/*.java"/>
    </copy>

<jacoco:coverage destfile="target/jacoco.exec" xmlns:jacoco="antlib:org.jacoco.ant">
     <junit printsummary="yes" fork="true" haltonfailure="false" showoutput="true" failureproperty="test.failed">
            <formatter type="xml"/>
         <classpath refid="classpath"/>
            <classpath>
                <pathelement location="build/tests"/>
         </classpath>
            <test name="com........MainTestSuite" todir="build"/>
     </junit>
</jacoco:coverage>
    <fail message="Test failure detected, check test results." if="test.failed"/>
</target>

and sonar task:

<target name="sonar" depends="build">
    <property name="sonar.tests" value="test" />
    <property name="sonar.libraries" value="" />
    <property name="sonar.surefire.reportsPath" value="sonarWorkDir" />

    <!-- The following properties are required to use JaCoCo: -->
    <!-- 1. Tells Sonar to run the unit tests -->
    <property name="sonar.dynamicAnalysis" value="true" />
    <!-- 2. Tells Sonar which "tests" targets to run -->
    <property name="sonar.jacoco.antTargets" value="test" />
    <!-- 3. Tells Sonar to use JaCoCo as the code coverage engine -->
    <property name="sonar.core.codeCoveragePlugin" value="jacoco" />

    <!-- Execute Sonar -->
    <sonar:sonar key="${JOB_NAME}" version="${VERSION}" xmlns:sonar="antlib:org.sonar.ant">
        <sources>
            <path location="...../src" />
        </sources>

         <binaries>
            <path location="build/....." />
        </binaries>

    </sonar:sonar>
</target>

until sonar task runs i got a warning 10:00:20.290 WARN o.s.p.j.JaCoCoPlugin - Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?

标签: ant sonarqube
2条回答
小情绪 Triste *
2楼-- · 2019-07-20 18:03

Kind of a late response, but I just recently ran into this issue and couldn't find a simple solution on the internet. Instead, to solve it I simply took the advice of the stack trace. Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?

And then went back to my ant file and made sure the source files (not test) were being compiled in debug mode. Apparently the jacoco plugin needs that extra info like line numbers in order to calculate the code coverage. Once you change your ant file you should finally see a code coverage percentage in sonar.

<javac srcdir="${source.dir}" target="1.6" source="1.6" destdir="${build.classes.dir}" debug="on">
    ...
</javac>

Also, make sure you include the following sonar properties :

sonar.binaries=build/classes
sonar.tests=junit/tests
sonar.dynamicAnalysis=reuseReports
sonar.junit.reportsPath=build/test-reports
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=build/test-reports/jacoco.exec

So you probably want to remove sonar.antTargets and change sonar.dynamicAnalysis's value to reuseReports

查看更多
爷、活的狠高调
3楼-- · 2019-07-20 18:07

I would advise you to:

查看更多
登录 后发表回答