通过ant方式使用Jacoco远程统计tomcat服务的代码覆盖率

BitsPlayer 2020-02-13

1)修改远程tomcat下的bin/catalina.sh中JAVA_OPTS的配置
----------------------------------------------------------------
# -javaagent: 的后面跟jacoco的安装路径
# includes= 选项,选择你要覆盖率的服务
# port= 选项,选择你要打开的端口
# address= 选项,tomcat服务所在机器的ip地址(如果想在跟tomcat服务同一台机器上执行ant任务的话,需要改为127.0.0.1)
JAVA_OPTS="-javaagent:/path/to/your/jacoco_0.8.5/lib/jacocoagent.jar=includes=com.*,output=tcpserver,port=8893,address=10.81.14.77"
-------------------------------------------------
2)本地Ant任务配置build.xml。
-----------------------------------------------------
<?xml version="1.0" ?>
<project name="Lengyu" xmlns:jacoco="antlib:org.jacoco.ant" default="jacoco">
<!--Jacoco的安装路径-->
<property name="jacocoantPath" value="/home/work/software/jacoco_0.6.4/lib/jacocoant.jar"/>
<!--最终生成.exec文件的路径,Jacoco就是根据这个文件生成最终的报告的-->
<property name="jacocoexecPath" value="/home/work/local/hudson_home/workspace/wg_merchant_oc_regression/jacoco.exec"/>
<!--生成覆盖率报告的路径-->
<property name="reportfolderPath" value="E:/Libs/coverage_ant_task/report/"/>
<!--远程tomcat服务的ip地址-->
<property name="server_ip" value="10.81.14.77"/>
<!--前面配置的远程tomcat服务打开的端口,要跟上面配置的一样-->
<property name="server_port" value="8893"/>
<!--源代码路径-->
<property name="checkOrderSrcpath" value="E:/Src/ordercenter/ordercenter-biz/src/main/java/" />
<!--.class文件路径-->
<property name="checkOrderClasspath" value="E:/Src/ordercenter/ordercenter-biz/target/classes/com/baidu/ordercenter/service/Impl" />

<!--让ant知道去哪儿找Jacoco-->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath path="${jacocoantPath}" />
</taskdef>

<!--dump任务:
根据前面配置的ip地址,和端口号,
访问目标tomcat服务,并生成.exec文件。-->
<target name="dump">
<jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoexecPath}" port="${server_port}" append="true"/>
</target>

<!--jacoco任务:
根据前面配置的源代码路径和.class文件路径,
根据dump后,生成的.exec文件,生成最终的html覆盖率报告。-->
<target name="report">
<delete dir="${reportfolderPath}" />
<mkdir dir="${reportfolderPath}" />

<jacoco:report>
<executiondata>
<file file="${jacocoexecPath}" />
</executiondata>

<structure name="JaCoCo Report">
<group name="Check Order related">
<classfiles>
<fileset dir="${checkOrderClasspath}" />
</classfiles>
<sourcefiles encoding="gbk">
<fileset dir="${checkOrderSrcpath}" />
</sourcefiles>
</group>
</structure>

<html destdir="${reportfolderPath}" encoding="utf-8" />
</jacoco:report>
</target>
</project>
-----------------------------------------
3)在build.xml目录下执行ant dump命令生成jacoco.exec
4)执行ant report命令生成覆盖率报告

相关推荐