jacoco + ant远程统计(tomcat/spring boot)服务的代码覆盖率

什么是Jacoco?
Jacoco(java code coverage)是一个开源的Java代码覆盖率工具,Jacoco可以嵌入到Ant 、Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序。很多第三方的工具提供了对Jacoco的集成,如sonar、Jenkins等。

软件安装
Ant
Jacoco

Tomcat配置
1、进入你需要统计代码覆盖率的tomcat  
cd /data/tomcat-gateway/bin
2、先关闭这个tomcat的服务
正常情况下可以直接执行  ./shutdown.sh
执行结束后 ps -ef|grep tomcat名 查看是否已经正常关闭,如果还没有关闭 kill -9 pid(来源于ps的结果)
3、修改 catalina.sh  的JAVA_OPTS/CATALINA_OPTS配置
output=file时,需要shudown服务才能dump出jacoco.exec文件
declare -x CATALINA_OPTS="-javaagent:/data/jacoco/lib/jacocoagent.jar=includes=*,output=file,destfile=/data/jacocoExec/jacoco_gateway.exec"
output=tcpserver时,可以利用ant执行dump操作,无需shutdown服务;
address可以填写hostname或ip,填ip时需要注意,查看/etc/hosts的hostname映射的ip是什么
declare -x CATALINA_OPTS="-javaagent:/data/jacoco/lib/jacocoagent.jar=includes=*,output=tcpserver,port=8457,address=172.16.1.31"
具体参数的意思请查看官方文档
4、启动服务 ./startup.sh
5、查看一下JAVA_OPTS是否有修改正确,在这有个坑,有一种情况是你把JAVA_OPTS写在前面,在后面被别人的覆盖了
ps -ef|grep tomcat名 看出来的结果有没有刚才你修改的值

6、output=tcpserver时,查看tcpserver有没有启来

netstat -tunlp|grep 8457


Spring Boot服务
启动命令如下:
java -javaagent:/data/jacoco/lib/jacocoagent.jar=includes=*,output=tcpserver,port=8457,address=172.16.1.31 -jar springboot-xxx.jar

ant任务配置
配置jacocoAnt.xml文件

xml version="1.0" encoding="UTF-8"?>
<project name="JaCoCo" default="run" xmlns:jacoco="antlib:org.jacoco.ant">
        
    <property name="jacocoAntPath" value="/opt/jacoco/lib/jacocoant.jar"/>
        
    <property name="jacocoExecPath" value="/data/jacocoExec"/>
        
    <property name="jacocoReportPath" value="/data/jacocoReport"/>
        
    <property name="server_ip_test163" value="172.16.1.31"/>
        
    <property name="server_port_cybs" value="8456"/>
    <property name="server_port_gateway" value="8457"/>

        
    <property name="gatewaySrcPath" value="/data/jacocoSrc/Gateway/GATEWAY/fi-website/src/main/java"/>

    <property name="cybsSrcPath" value="/data/jacocoSrc/cybs/src/main/java"/>    

        
    <property name="gatewayClassesPath" value="/data/jacocoClasses/gateway/WEB-INF/classes"/>
<property name="cybsClassesPath" value="/data/jacocoClasses/cybs/WEB-INF/classes"/> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="${jacocoAntPath}"/> taskdef> <target name="run"> <echo message="start..."/> <echo message="dump..."/> <antcall target="dump"/> <echo message="merge..."/> <antcall target="merge"/> <echo message="report..."/> <antcall target="report"/> <echo message="end..."/> target> <target name="dump"> <jacoco:dump address="${server_ip_test163}" reset="true" destfile="${jacocoExecPath}/jacoco_gateway.exec" port="${server_port_gateway}" append="false"/> <jacoco:dump address="${server_ip_test163}" reset="true" destfile="${jacocoExecPath}/jacoco_cybs.exec" port="${server_port_cybs}" append="false"/> target> <target name="merge"> <jacoco:merge destfile="${jacocoExecPath}/merged.exec"> <fileset dir="${jacocoExecPath}" includes="*.exec"/> jacoco:merge> target> <target name="report"> <jacoco:report> <executiondata> <file file="${jacocoExecPath}/merged.exec"/> executiondata> <structure name="JaCoCo Report"> <group name="gateway coverage"> <sourcefiles encoding="UTF-8"> <fileset dir="${gatewaySrcPath}"/> sourcefiles> <classfiles> <fileset dir="${gatewayClassesPath}"/> classfiles> group> <group name="cybs coverage"> <sourcefiles encoding="UTF-8"> <fileset dir="${cybsSrcPath}"/> sourcefiles> <classfiles> <fileset dir="${cybsClassesPath}"/> classfiles> group> structure> <html destdir="${jacocoReportPath}" encoding="utf-8"/> <csv destfile="${jacocoReportPath}/report.csv"/> <xml destfile="${jacocoReportPath}/report.xml"/> jacoco:report> target> project>

执行ant任务
ant -f jacocoAnt.xml


reset=true时,会在dump出exec文件后,清空覆盖率数据;
append=false时,dump出的exec文件会覆盖原有的exec文件;append=true时,dump出的exec文件追加至原有的exec文件;


结果展示:

+ cd /data/jacocoAntXml
+ ant -f jacocoAnt.xml
Buildfile: /data/jacocoAntXml/jacocoAnt.xml

run:
     [echo] start...
     [echo] dump...

dump:
[jacoco:dump] Connecting to /172.16.1.15:8457
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_gateway.exec
[jacoco:dump] Connecting to /172.16.1.15:8456
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_cybs.exec
[jacoco:dump] Connecting to /172.16.1.15:8470
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_creditCardBank.exec
[jacoco:dump] Connecting to /172.16.1.15:8459
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_mps.exec
[jacoco:dump] Connecting to /172.16.1.15:8451
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_accounting.exec
[jacoco:dump] Connecting to /172.16.1.15:8458
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_ordercenter.exec
[jacoco:dump] Connecting to /172.16.1.15:8453
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_notification.exec
[jacoco:dump] Connecting to /172.16.1.15:8454
[jacoco:dump] Dumping execution data to /data/jacocoExec/jacoco_poss.exec
     [echo] merge...

merge:
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_accounting.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_creditCardBank.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_cybs.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_gateway.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_mps.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_notification.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_ordercenter.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/jacoco_poss.exec
[jacoco:merge] Loading execution data file /data/jacocoExec/merged.exec
[jacoco:merge] Writing merged execution data to /data/jacocoExec/merged.exec
     [echo] report...

report:
[jacoco:report] Loading execution data file /data/jacocoExec/merged.exec
[jacoco:report] Writing bundle 'webgate coverage' with 701 classes
[jacoco:report] Writing bundle 'gateway coverage' with 198 classes

[echo] end...
BUILD SUCCESSFUL

jacoco + ant远程统计(tomcat/spring boot)服务的代码覆盖率_第1张图片

 



转载于:https://www.cnblogs.com/dingtian/p/7754079.html

你可能感兴趣的:(jacoco + ant远程统计(tomcat/spring boot)服务的代码覆盖率)