Jenkins+sonar+jcoco+ivy做代码覆盖率
首先,研发写的代码里要有单元测试相关的桩内容。
1.需要下载可用的jacoco.jar包配置到build.xml中
2.jenkins配置:
sonar.projectKey=${JOB_NAME}
sonar.projectName=$JOB_NAME
sonar.projectVersion=$SVN_REVISION
sonar.sources=.
sonar.language=java
sonar.sourceEncoding=UTF-8
sonar.dynamicAnalysis=reuseReports
sonar.core.codeCoveragePlugin=jacoco
sonar.surefire.reportsPath=${WORKSPACE}/billtrunk/target/junit
sonar.java.binaries=${WORKSPACE}/billtrunk/bin/classes
sonar.jacoco.reportPath=${WORKSPACE}/billtrunk/target/sonar/jacoco.exec
sonar.junit.reportPaths=${WORKSPACE}/billtrunk/target/junit
sonar.jacoco.itReportPath=${WORKSPACE}/billtrunk/target/ITCoverageReport/jacocoIT.exec
3.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" xmlns:ivy="antlib:org.apache.ivy.ant">
<!-- ========= Define the main properties of this project ========= -->
<property name="src.dir" value="src" />
<property name="build.dir" value="bin" />
<property name="classes.dir" value="${build.dir}/classes" />
<property name="lib.dir" value="${build.dir}/lib/"/>
<property name="reports.junit.xml.dir" value="${basedir}/target/junit"/>
<property name="sonar.coverage.lib" value="/opt/lib_coverage"/>
<property name="ivysettings.dir" value="/opt/ivysettings"/>
<property name="jacoco.dir" value="${basedir}/target/sonar"/>
<property name="reports.ITCoverageReport.exec" value="${basedir}/target/ITCoverageReport"/>
<property name="reports.ITCoverageReport.dir" value="${reports.ITCoverageReport.exec}/report"/>
<path id="project.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<pathelement path="${classes.dir}"/>
</path>
<ivy:settings file="${ivysettings.dir}/ivysettings.xml" />
<target name="resolve" description="--> parse ivy.xml">
<ivy:resolve file="ivy.xml" conf="*" />
<ivy:retrieve pattern="${lib.dir}/[artifact].[ext]"/>
</target>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${jacoco.dir}"/>
</target>
<target name="init">
<mkdir dir="${build.dir}" />
<mkdir dir="${classes.dir}" />
<mkdir dir="${reports.junit.xml.dir}" />
<mkdir dir="${jacoco.dir}" />
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${classes.dir}" source="1.7" target="1.7" debug="true"
deprecation="false" optimize="false" failonerror="true" >
<compilerarg line="-encoding UTF-8"/>
<classpath refid="project.classpath"></classpath>
</javac>
<copy todir="${classes.dir}" overwrite="true">
<fileset dir="${src.dir}/main/java/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/test/java/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/main/resources/">
<exclude name="**/*.java"/>
</fileset>
<fileset dir="${src.dir}/test/resources/">
<exclude name="**/*.java"/>
</fileset>
</copy>
</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="${sonar.coverage.lib}/jacocoant0.7.9.jar" />
</taskdef>
<!-- Run your unit tests, adding the JaCoCo agent -->
<jacoco:coverage destfile="${jacoco.dir}/jacoco.exec">
<junit fork="on" forkmode="once" printsummary="on" showoutput="true">
<formatter type="xml" usefile="true"/>
<classpath>
<path refid="project.classpath"/>
</classpath>
<batchtest todir="${reports.junit.xml.dir}">
<fileset dir="${classes.dir}">
<include name="test/**/*Test.class" />
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<junitreport todir="${reports.junit.xml.dir}">
<fileset dir="${reports.junit.xml.dir}">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${reports.junit.xml.dir}" />
</junitreport>
<jacoco:report>
<executiondata>
<file file="${jacoco.dir}/jacoco.exec" />
</executiondata>
<structure name="ma project coverage report">
<classfiles>
<fileset dir="${classes.dir}" />
</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>
<target name="it coverage" depends="test">
<jacoco:dump address="192.168.126.7" reset="true" destfile="${reports.ITCoverageReport.exec}/jacocoIT.exec" port="8080" append="true">
</jacoco:dump>
</target>
<target name="it coverage report" depends="it coverage">
<delete dir="${reports.ITCoverageReport.dir}" />
<mkdir dir="${reports.ITCoverageReport.dir}" />
<jacoco:report>
<executiondata>
<file file="${reports.ITCoverageReport.exec}/jacocoIT.exec" />
</executiondata>
<structure name="integration test coverage rate report">
<group name="Check Order Related">
<classfiles>
<fileset dir="${classes.dir}" />
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${basedir}/src" />
</sourcefiles>
</group>
</structure>
<html footer="baby-console it coverage report" destdir="${reports.ITCoverageReport.dir}" />
<csv destfile="${reports.ITCoverageReport.dir}/coverage-report.csv" />
<xml destfile="${reports.ITCoverageReport.dir}/coverage-report.xml" />
</jacoco:report>
</target>
<!-- ========= The main target "all" ========= -->
<target name="all" depends="clean,resolve,compile,it coverage report" />
</project>
4.单元测试要显示在sonarqube中,需要在web端配置
5.可以在设计,链接中加入详情
可以tomcat的conf/server.xml中配置链接 <host>标签内加上访问url
<Context path="/coverage" docBase="/opt/jenkins/workspace/UNIT_TEST_srpbase-common/billtrunk/target/report" debug="0" privileged="true">
</Context>
<Context path="/junit" docBase="/opt/jenkins/workspace/UNIT_TEST_srpbase-common/billtrunk/target/junit" debug="0" privileged="true">
</Context>
---------------------
原文:https://blog.csdn.net/aaf02002/article/details/81507410
版权声明:本文为博主原创文章,转载请附上博文链接!