首页 > 代码库 > ant+sonar+jacoco代码质量代码覆盖率扫描

ant+sonar+jacoco代码质量代码覆盖率扫描

使用ant构建的java web项目如何做sonar代码质量扫描?
以下就是实际遇到并成功使用的案例
一、做sonar扫描的准备工作
    1.给web项目增加build.xml构建脚本。
    2.下载jar包:jacocoant.jar;sonar-ant-task-2.2.jar
    3.搭建一个sonar服务器
二、在build.xml中编写jacoco和sonar的脚本

  案例的build.xml脚本

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Java Project analyzed with the Sonar Ant Task" default="all" basedir="." xmlns:jacoco="antlib:org.jacoco.ant" xmlns:sonar="antlib:org.sonar.ant">

        <!-- ========= Define the main properties of this project ========= -->
        <property name="src.dir" value="http://www.mamicode.com/src" />
        <property name="build.dir" value="http://www.mamicode.com/target" />
        <property name="classes.dir" value="http://www.mamicode.com/${build.dir}/classes" />
        <property name="webRoot.dir" value="http://www.mamicode.com/${basedir}/WebRoot"/>
        <property name="lib.dir" value="http://www.mamicode.com/${webRoot.dir}/WEB-INF/lib"/>
        <property name="reports.junit.xml.dir" value="http://www.mamicode.com/${basedir}/target/junit"/>
    
        <path id="project.classpath">
            <fileset dir="${lib.dir}">
                <include name="*.jar"/>
            </fileset>
            <!--<fileset dir="${basedir}/lib">
                       <include name="*.jar" />
            </fileset>  -->
            <pathelement path="${basedir}/WebRoot/WEB-INF/lib"/>
        </path>

        <property name="sonar.host.url" value="http://172.31.65.167:9000/" />

        <!-- Define the Sonar properties -->
        <property name="sonar.projectKey" value="http://www.mamicode.com/com.ceair.ma:ma" />
        <property name="sonar.projectName" value="http://www.mamicode.com/ma" />
        <property name="sonar.projectVersion" value="http://www.mamicode.com/1.0" />
        <property name="sonar.language" value="http://www.mamicode.com/java" />
        <property name="sonar.sources" value="http://www.mamicode.com/src" />
        <property name="sonar.binaries" value="http://www.mamicode.com/target/classes" />
  
        <property name="sonar.working.directory" value="http://www.mamicode.com/target/sonar" />
        <!--property name="sonar.surefire.reportsPath" value="http://www.mamicode.com/sonar/build/surefire-reports/findbugs-result.xml" /-->
        <property name="sonar.sourceEncoding" value="http://www.mamicode.com/UTF-8" />
    
        <!-- sonar使用jacoco配置: -->
        <property name="sonar.dynamicAnalysis" value="http://www.mamicode.com/reuseReports" />
        <property name="sonar.java.coveragePlugin" value="http://www.mamicode.com/jacoco" />
        <property name="sonar.jacoco.reportPath" value="http://www.mamicode.com/jacoco.exec" />

        <!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
        <property name="sonar.jdbc.url" value="http://www.mamicode.com/jdbc:mysql://172.31.65.167:3306/test?useUnicode=true&characterEncoding=utf8" />
        <property name="sonar.jdbc.username" value="http://www.mamicode.com/test" />
        <property name="sonar.jdbc.password" value="http://www.mamicode.com/000000" />
        <property name="sonar.junit.reportsPath" value="http://www.mamicode.com/target/sonar" />
    
        <!-- 控制台打印sonar分析的详细信息-->
        <!--<property name="sonar.verbose" value="http://www.mamicode.com/true" /> -->

        <!-- ========= Define "regular" targets: clean, compile, ... ========= -->
        <target name="clean">
                <delete dir="${build.dir}" />
        </target>
        <target name="init">
            <mkdir dir="${build.dir}" />
            <mkdir dir="${classes.dir}" />
            <mkdir dir="${reports.junit.xml.dir}" />
        </target>

        <target name="compile" depends="init">
            <javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.6" target="1.6" debug="on"
               deprecation="false" optimize="false" failonerror="true" >
                <compilerarg line="-encoding UTF-8"/>
                <classpath refid="project.classpath"></classpath>
            </javac>
        </target>
    
        <target name="test" depends="compile">

            <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
                <classpath>
                    <path refid="project.classpath"/>
                </classpath>
            </taskdef>

            <!-- Import the JaCoCo Ant Task -->
            <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
                <!-- Update the following line, or put the "jacocoant.jar" file in your "$HOME/.ant/lib" folder -->
                <classpath path="${basedir}/third/jacocoant.jar" />
            </taskdef>

            <!-- Run your unit tests, adding the JaCoCo agent -->
            <jacoco:coverage>
             <junit fork="true" forkmode="once" printsummary="on" showoutput="true">
                 <!--<classpath location="${classes.dir}" />
                <classpath refid="project.classpath" />-->

                <formatter type="xml" usefile="true"/>
                 <classpath>  
                  <fileset dir="${lib.dir}" includes="**/*.jar"/>  
                  <pathelement path="${classes.dir}"/>  
                </classpath>
                <batchtest todir="${reports.junit.xml.dir}">
                 <fileset dir="${classes.dir}">
                    <include name="**/*Test*.*" />
                 </fileset>
                </batchtest>
             </junit>
            </jacoco:coverage>    
            
            <!--<jacoco:report>
                <executiondata>
                    <file file="jacoco.exec" />
                </executiondata>
                <structure name="ma project coverage report">
                    <classfiles>
                        <fileset dir="${basedir}/target/classes" />
                    </classfiles>
                    <sourcefiles encoding="UTF-8">
                        <fileset dir="${src.dir}" />
                    </sourcefiles>
                </structure>
                <html destdir="${basedir}/target/report" />
                <csv destfile="${basedir}/target/report/report.csv" />
                <xml destfile="${basedir}/target/report/report.xml" />
            </jacoco:report> -->

        </target>

        <!-- ========= Define Sonar target ========= -->
        <target name="sonar" depends="compile">
                <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
                        <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
                        <classpath path="${basedir}/third/sonar-ant-task-2.2.jar" />
                </taskdef>

                <!-- Execute Sonar -->
                <sonar:sonar />
        </target>

        <!-- ========= The main target "all" ========= -->
        <target name="all" depends="clean,compile,test,sonar" />

</project>
build.xml


    1.因为ant中没有像maven那样天然集成sonar,所以在脚本中需要加入很多sonar需要的属性。
    2.需要一个target任务执行jacoco:coverage并生成代码覆盖率文件jacoco.exec;这里注意jacoco.exec是在<jacoco:coverage destfile="${basedir}/jacoco.exec">指定,如果没有destfile属性则默认在项目根目录生成名为jacoco.exec文件。这个生成的文件要和<property name="sonar.jacoco.reportPath" value="http://www.mamicode.com/jacoco.exec" />对应,这步是把jacoco.exec交给sonar去解析。

        <jacoco:coverage>
             <junit fork="true" forkmode="once" printsummary="on" showoutput="true">
                <formatter type="xml" usefile="true"/>
                 <classpath>  
                  <fileset dir="${lib.dir}" includes="**/*.jar"/>  
                  <pathelement path="${classes.dir}"/>  
                </classpath>
                <batchtest todir="${reports.junit.xml.dir}">
                 <fileset dir="${classes.dir}">
                    <include name="**/*Test*.*" />
                 </fileset>
                </batchtest>
             </junit>
        </jacoco:coverage>    

   

  3.这步非必须;如果要生成jacoco报表还可以添加一个<jacoco:report>,可以生成html、xml、csv类型的报表。

            <jacoco:report>
                <executiondata>
                    <file file="jacoco.exec" />
                </executiondata>
                <structure name="ma project coverage report">
                    <classfiles>
                        <fileset dir="${basedir}/target/classes" />
                    </classfiles>
                    <sourcefiles encoding="UTF-8">
                        <fileset dir="${src.dir}" />
                    </sourcefiles>
                </structure>
                <html destdir="${basedir}/target/report" />
                <csv destfile="${basedir}/target/report/report.csv" />
                <xml destfile="${basedir}/target/report/report.xml" />
            </jacoco:report>        

 


  4、代码覆盖率做完了就可以做sonar代码质量扫描了。

        <target name="sonar" depends="compile">
            <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
                    <classpath path="${basedir}/third/sonar-ant-task-2.2.jar" />
            </taskdef>

            <!-- 执行 Sonar -->
            <sonar:sonar />
    </target>

 

三、运行这个build脚本,成功后就可以去sonar服务器查看结果了。
具体参见 http://blog.chinaunix.net/uid-1835840-id-3616622.html

ant+sonar+jacoco代码质量代码覆盖率扫描