使用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="src" />
        <property name="build.dir" value="target" />
        <property name="classes.dir" value="${build.dir}/classes" />
        <property name="webRoot.dir" value="${basedir}/WebRoot"/>
        <property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib"/>
        <property name="reports.junit.xml.dir" value="${basedir}/target/junit"/>
    
        <path >
            <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="com.ceair.ma:ma" />
        <property name="sonar.projectName" value="ma" />
        <property name="sonar.projectVersion" value="1.0" />
        <property name="sonar.language" value="java" />
        <property name="sonar.sources" value="src" />
        <property name="sonar.binaries" value="target/classes" />
  
        <property name="sonar.working.directory" value="target/sonar" />
        <!--property name="sonar.surefire.reportsPath" value="sonar/build/surefire-reports/findbugs-result.xml" /-->
        <property name="sonar.sourceEncoding" value="UTF-8" />
    
        <!-- sonar使用jacoco配置: -->
        <property name="sonar.dynamicAnalysis" value="reuseReports" />
        <property name="sonar.java.coveragePlugin" value="jacoco" />
        <property name="sonar.jacoco.reportPath" value="jacoco.exec" />

        <!-- Add your basic Sonar configuration below: sonar.jdbc.url, sonar.jdbc.username, etc. properties -->
        <property name="sonar.jdbc.url" value="jdbc:mysql://172.31.65.167:3306/test?useUnicode=true&amp;characterEncoding=utf8" />
        <property name="sonar.jdbc.username" value="test" />
        <property name="sonar.jdbc.password" value="000000" />
        <property name="sonar.junit.reportsPath" value="target/sonar" />
    
        <!-- 控制台打印sonar分析的详细信息-->
        <!--<property name="sonar.verbose" value="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 ref></classpath>
            </javac>
        </target>
    
        <target name="test" depends="compile">

            <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
                <classpath>
                    <path ref/>
                </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 ref />-->

                <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

相关文章: