ANT clover and sonar

1.3k Views Asked by At

I have an ANT build with clover and sonar targets, the sample command is

ant clover.all sonar_all

Currently i use the

 <clover-setup initstring="${clover.db.file}">
      <fileset dir="${src.dir}">
 ...

target to set my source directory for clover instrumentation. My tests run and i get a clover report as expected. My sonar target

  <target name="sonar_all">
    <pathconvert pathsep="," property="echo.path.compile" refid="core.class.path"/>
    <property name="sonar.libraries" value="${echo.path.compile}"/>
    <property name="sonar.sources" value="${src.dir}"/>
    <property name="sonar.projectName" value="Touchpoint"/>
    <property name="sonar.binaries" value="${build.classes.dir}"/>
    <property name="sonar.tests" value=""/>
    <property name="sonar.host.url" value="${sonar.host.url}"/>
    <property name="sonar.jdbc.url" value="${sonar.jdbc.url}"/>
    <property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="sonar.jdbc.username" value="${sonar.jdbc.username}"/>
    <property name="sonar.jdbc.password" value="${sonar.jdbc.password}"/>
    <property name="sonar.exclusions" value=""/>
    <property name="sonar.dynamicAnalysis" value="reuseReports"/>
    <property name="sonar.surefire.reportsPath" value="${build.dir}/utest"/>
    <property name="sonar.clover.reportPath" value="${build.dir}/clover/report/clover.xml"/>
    <property name="sonar.java.source" value="1.6"/>
    <property name="sonar.java.target" value="1.6"/>
    <sonar:sonar key="xx" version="xx" xmlns:sonar="antlib:org.sonar.ant"/>
  </target>

also works correctly but it seems that the clover instrumented code is being processed rather the original source code, this causes an issue with the findbugs report since the clover instrumentation code causes multiple violations in findbugs. My sonar metrics are off the wall. Currently as a work around we have two CI builds, one for clover and second for sonar but it means I can never get the code coverage metrics and sonar details in one.

I've tried to use the clover-instr target to ensure the instrumented code is placed in a different directory but since the compiled code is still generated into a single classes directory, the same issue arises. I'm wondering what strategy any other developers have used to get around this.

At the lowest level, is there a property (ie -Dclover.useCompile=true|false) that is set by the clover.all target to indicate to the ant compile target that the clover compiler should be used? I like to run the default clover.all target, then in the sonar target delete the instrumented source and class files, re-generate without the clover option and run the default sonar target.

 ant clover.all clover_off sonar_all

EDIT 1: If found this item on Clover, Ant and Findbugs which suggests compiling twice within the ANT scripts. Since the ant build will only load properties once i can't see how i can swtich a property mid-build to ensure the compile task is run twice, once with clover enabled and the second time without.

1

There are 1 best solutions below

0
On

The basic problem here is the clover generated source code and classes cause a headache for sonar, but it's not possible to seperate the generation paths via the ant tasks. My hacked solution is to copy the all the class files from the post clover phase to a new folder at the start of the sonar target, and then delete the clover classes on the assumption that they have the string "$__CLR" in their name. I then instruct sonar to generate its metrics using the original class set.

<target name="sonar">

   <delete dir="${build.dir}/sonarclasses"/>
   <mkdir dir="${build.dir}/sonarclasses/com"/>
   <copy todir="${build.dir}/sonarclasses/com" verbose="true" failonerror="false">
       <fileset dir="${build.classes.dir}/com" excludes="com/**/*$__CLR*.class"/>
   </copy>

   <pathconvert pathsep="," property="echo.path.compile" refid="core.class.path"/>
   <property name="sonar.libraries" value="${echo.path.compile}"/>
   <property name="sonar.sources" value="${src.dir}"/>
   <property name="sonar.projectName" value="Touchpoint"/>
   <property name="sonar.binaries" value="${build.dir}/sonarclasses"/>
   <property name="sonar.tests" value=""/>
   <property name="sonar.host.url" value="${sonar.host.url}"/>
   <property name="sonar.jdbc.url" value="${sonar.jdbc.url}"/>
   <property name="sonar.jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
   <property name="sonar.jdbc.username" value="${sonar.jdbc.username}"/>
   <property name="sonar.jdbc.password" value="${sonar.jdbc.password}"/>
   <property name="sonar.exclusions" value=""/>
   <property name="sonar.dynamicAnalysis" value="reuseReports"/>
   <property name="sonar.surefire.reportsPath" value="${build.dir}/utest"/>
   <property name="sonar.clover.reportPath" value="${build.dir}/clover/report/clover.xml"/>
   <property name="sonar.java.source" value="1.6"/>
   <property name="sonar.java.target" value="1.6"/>
   <!--<property name="sonar.findbugs.reportPath" value="${build.dir}/findbugs/findbugs.xml"/>-->
   <sonar:sonar