参考博客:

https://blog.csdn.net/qq_26392615/article/details/82228972 

大概步骤:

1、远程服务器上安装jacoco

2、本地安装ant + jacoco

3、重启远端服务,加上javaagent 参数,nohup java -javaagent:/root/jacoco/lib/jacocoagent.jar=includes=*,output=tcpserver,port="未被占用的端口",address="服务器的地址",如果服务部署在docker容器里,此处的port和address是服务器上对外的port 和 address,不是docker里面的port和ip

4、build.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<project name="JaCoCo" default="run" xmlns:jacoco="antlib:org.jacoco.ant">
<!--Jacoco的安装路径-->
<!--
<property name="jacocoAntPath" value="/home/yunpan/src/jacoco/lib/jacocoant.jar"/> -->
<property name="jacocoAntPath" value="D:\work\jacoco\lib\jacocoant.jar"/>

<!--生成.exec文件的路径,Jacoco就是根据这个文件生成HTML报告的-->
<!--<property name="jacocoExecPath" value="/home/yunpan/test/exec"/> -->
<property name="jacocoExecPath" value="D:\work\jacoco\work-space\exec"/>

<!--生成覆盖率报告的路径-->
<!--<property name="jacocoReportPath" value="/home/yunpan/test/report"/>-->
<property name="jacocoReportPath" value="D:\work\jacoco\work-space\report"/>


<!--远程服务的ip地址,如果用的是docker部署的,此处填的ip和端口不是docker内部的ip而是服务器的ip和端口,
服务器的端口需要与docker容器内java应用的端口做映射 -->
<property name="server_ip" value=""/>
<!--前面javaagent配置的远程服务打开的端口,要跟上面配置的一样-->
<property name="server_port_yunpan" value=""/>

<!--源代码路径-->
<!--yunpan项目-->
<property name="yunpan_apiSrcPath" value="D:\work\jacoco\work-space\source\test\cloud-storage-server\service-api\src\main\java"/>
<property name="yunpan_providerSrcPath" value="D:\work\jacoco\work-space\source\test\cloud-storage-server\service-provider\src\main\java"/>
<property name="yunpan_webSrcPath" value="D:\work\jacoco\work-space\source\test\cloud-storage-server\web-start\src\main\java"/>

<!--.class文件路径-->
<!--yunpan项目-->
<!--
<property name="yunpan_apiClassesPath" value="/home/yunpan/test/target/service-api/target/classes"/>
<property name="yunpan_providerClassesPath" value="/home/yunpan/test/target/service-provider/target/classes"/>
<property name="yunpan_webClassesPath" value="/home/yunpan/test/target/web-start/target/classes"/> -->
<property name="yunpan_ClassesPath" value="D:\work\jacoco\work-space\BOOT-INF\classes"/>


<!--让ant知道去哪儿找Jacoco-->
<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>

<!--dump任务:
根据前面配置的ip地址,和端口号,访问目标服务,并生成.exec文件。
reset=true时,会在dump出exec文件后,清空覆盖率数据;
append=false时,dump出的exec文件会覆盖原有的exec文件;append=true时,dump出的exec文件 
追加至原有的exec文件;
-->
<target name="dump">
<jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoExecPath}/jacoco_yunpan_api.exec" port="${server_port_yunpan}" append="true"/>
<jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoExecPath}/jacoco_yunpan_provider.exec" port="${server_port_yunpan}" append="true"/>
<jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoExecPath}/jacoco_yunpan_web.exec" port="${server_port_yunpan}" append="true"/>
</target>

<target name="merge">
<jacoco:merge destfile="${jacocoExecPath}/merged.exec">
<fileset dir="${jacocoExecPath}" includes="*.exec"/>
</jacoco:merge>
</target>

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

<structure name="JaCoCo Report">
<!--group name 对应生成的报告中的列表名-->
<group name="yunpan api">
<sourcefiles encoding="UTF-8">
<fileset dir="${yunpan_apiSrcPath}"/>
</sourcefiles>
<classfiles>
<fileset dir="${yunpan_ClassesPath}"/>
</classfiles>
</group>
<group name="yunpan provider">
<sourcefiles encoding="UTF-8">
<fileset dir="${yunpan_providerSrcPath}"/>
</sourcefiles>
<classfiles>
<fileset dir="${yunpan_ClassesPath}"/>
</classfiles>
</group>
<group name="yunpan web">
<sourcefiles encoding="UTF-8">
<fileset dir="${yunpan_webSrcPath}"/>
</sourcefiles>
<classfiles>
<fileset dir="${yunpan_ClassesPath}"/>
</classfiles>
</group>

</structure>

<html destdir="${jacocoReportPath}" encoding="utf-8"/>
<csv destfile="${jacocoReportPath}/report.csv"/>
<xml destfile="${jacocoReportPath}/report.xml"/>
</jacoco:report>
</target>
</project>
View Code

 


5、需要拉取覆盖率的时候,执行对于的ant任务就可以

生成覆盖率报告
执行ant dump。成功的话,应会有如下输出。

[work@st01-ecom-jn2.st01.baidu.com ant]$ ant dump Buildfile: /home/work/local/hudson_home/workspace/wg_merchant_oc_regression/ant/build.xml dump: [jacoco:dump] Connecting to /10.81.14.77:8893 [jacoco:dump] Dumping execution data to /home/work/local/hudson_home/workspace/wg_merchant_oc_regression/jacoco.exec BUILD SUCCESSFUL Total time: 0 seconds

 

附录

最后执行ant report。jacoco就会在你指定的路径生成覆盖率报告了。

更多关于Jacocoagent以及各种task可以参考官方文档

Jenkins的Jacoco plugin可以根据.exec文件直接生成覆盖率报告,并在Jenkins中生成图表等等。那样的话,ant report这个任务就没用了。