配置 ant 环境变量
export ANT_HOME=/Users/xxx/apache-ant-1.10.8
export PATH=${PATH}:${ANT_HOME}/bin
<?xml version="1.0" ?>
<project name="test_crm" xmlns:jacoco="antlib:org.jacoco.ant" default="report">
<property name="test.server.host" value="127.0.0.1" />
<property name="local.server.host" value="127.0.0.1" />
<!-- .class文件所在路径-->
<property name="sangClasspath" value="/xxx/classes" />
<!-- .java文件所在路径-->
<property name="sangSourcepath" value="/xxx/java" />
<!-- jacoco报告文件所在路径-->
<property name="reportfolderPath" value="/xxx/jacoco-0.8.5/jacoco_file/jacoco_report" />
<!--让ant知道去哪儿找Jacoco-->
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<!-- 本地jacocoant.jar路径 -->
<classpath path="/xxx/jacoco-0.8.5/lib/jacocoant.jar" />
</taskdef>
<!-- 四个命令,clean,dump,merge,report-->
<target name="clean">
<!-- 清空文件 -->
<delete dir="/xxx/jacoco-0.8.5/jacoco_file" />
</target>
<target name="dump">
<!-- 注意端口号,不被占用的端口号-->
<jacoco:dump address="${test.server.host}" reset="false" destfile="/xxx/jacoco-0.8.5/jacoco_file/jacoco_data/jacoco_dump.exec" port="9528" append="true"/>
</target>
<target name="merge">
<!-- 合并多份exec文件 -->
<jacoco:merge destfile="/xxx/jacoco-0.8.5/jacoco_file/exec/merged.exec">
<fileset dir="/xxx/jacoco-0.8.5/jacoco_file/jacoco_data/" includes="*.exec" />
</jacoco:merge>
</target>
<target name="report" depends="dump,merge">
<!-- Step 3: Create coverage report -->
<delete dir="${reportfolderPath}" />
<mkdir dir="${reportfolderPath}" />
<jacoco:report>
<!--
This task needs the collected execution data and ...
-->
<executiondata>
<file file="/xxx/jacoco-0.8.5/jacoco_file/exec/merged.exec"/>
</executiondata>
<!-- the class files and optional source files ... -->
<structure name="JaCoCo Report">
<group name="sang_related">
<sourcefiles encoding="UTF-8">
<fileset dir="${sangSourcepath}"/>
</sourcefiles>
<classfiles>
<fileset dir="${sangClasspath}"/>
</classfiles>
</group>
</structure>
<!-- 生成html报告文件地址 -->
<html destdir="${reportfolderPath}/report" encoding="utf-8" />
<csv destfile="${reportfolderPath}/report.csv" />
<xml destfile="${reportfolderPath}/report.xml" />
</jacoco:report>
</target>
</project>
在启动 shell 里添加如下:
java -javaagent:/xxx/jacoco-0.8.5/lib/jacocoagent.jar=includes=xxx.*,output=tcpserver,port=9528,address=127.0.0.1 -jar /xxx/java-project/xxx.jar
** 注意:** 端口号要与 build.xml 文件保持一致,-javaagent 最好紧跟在 java
进入 ant 安装目录,切换到 build.xml 所在文件夹,输入:ant report,生成报告
这里讲一下我自己的实现,因为测试服务器和 Jenkins 服务器不在一台机器上,所以大概有几个步骤。如果你有更好的方式请 @ 我(自我感觉这种实现方式挺 low 的)
rsync -av /var/lib/jenkins/workspace/xxx/target/classes/com/xxx/xxx/controller/ user@ip:xxx/target/classes
(不喜欢用 Jenkins 变量,直接怼绝对地址,太 low 了),这里说一下,两台机器远程传输,记得权限要有
2,拉取 build.xml
为了将来实现对 build.xml 的持续管理,把它放在 git 上,新建 job,并把它发送到 ant 环境,bin 文件夹下,并执行 ant 命令
贴一下脚本,(shell 也是个二把刀,只会最简单的)
#引一下环境变量,忘了为啥会有这段了
export JAVA_HOME=/usr/lib/java/jdk1.8.0_221
export ANT_HOME=/xxx/apache-ant-1.9.15/apache-ant-1.9.15
export PATH=${ANT_HOME}/bin:$PATH
cd /xxx/xxx
rm -rf ant_xml
mkdir ant_xml
tar -zxvf dist.tar.gz -C ant_xml
cd ant_xml
cp build.xml /xxx/apache-ant-1.9.15/apache-ant-1.9.15/bin/build.xml
cd /xxx/apache-ant-1.9.15/apache-ant-1.9.15/bin
ant report
rsync -av user@ip:/root/jacoco-0.8.5/jacoco_file/exec /var/lib/jenkins/workspace/xxx
cp -r /var/lib/jenkins/workspace/xxx/target/classes/com/xxx/xxx/controller/ /var/lib/jenkins/workspace/xxx/exec/classes
cp -r /var/lib/jenkins/workspace/xxx/xxx/src/ /var/lib/jenkins/workspace/qa_crm_test_jacoco/exec/src
注意:取 java 文件时,从 src 文件夹复制,不要直接从 src/main/java 开始