ant+sonar+jacoco代码质量代码覆盖率扫描
Posted talk less and do more!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="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 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="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&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 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>
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="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>
以上是关于ant+sonar+jacoco代码质量代码覆盖率扫描的主要内容,如果未能解决你的问题,请参考以下文章