Jacoco使用
1、前提
项目是微服务架构,CI: 使用jenkins+racher+docker。目前一整套使用已流畅。因需要在测试环境加代码覆盖率进行统计,在不影响正常测试和改变现有部署框架方法下,把Jacoco使用起来
2、了解现状部署方式
部署现状:Jenkins 从git拉取代码 编译是jar包 ,Jenkins使用docker插件生成镜像文件,推送到阿里云上;racher从阿里云上拉取镜像文件部署。
3、使用Jacoco
-
在官网上下载 https://www.jacoco.org/jacoco/ jacococli.jar 与agent使用官网下载的。选择0.8.7版本
-
每个微服务pom.xml Dockerfile需要添加Jacoco依赖。考虑到有几十个微服务,在尽可能不改变现有的部署方式,我们通过写一段java代码来完成。只需要在jenkins上配置一段shell来执行这段代码即可。
echo $WORKSPACE/ #把jacocoagent.jar 复制到Dockerfile目录下 cp -rf ../jacocoagent.jar oms-server/oms-order-server/jacocoagent.jar #能过jacocoTest.jar 传三个参数:Dockerfile文件,端口,pom.xml文件 修改Dockerfile 和pom.xml文件,加入jacoco插件进行编译 并在docker加入jacocoAgent。 java -jar ../jacocoTest.jar "oms-server/oms-order-server/Dockerfile" "9926" "oms-server/oms-order-server/pom.xml"
jacocoTest.jar自动会在pom.xml文件加入 如下两段内容。
<dependency> <groupId>org.jacoco</groupId> <artifactId>org.jacoco.agent</artifactId> <version>0.8.7</version> <classifier>runtime</classifier> </dependency>
在build模块加入:jacoco插件,参与如下
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.7</version> <executions> <execution> <id>default-instrument</id> <goals> <goal>instrument</goal> </goals> </execution> <execution> <id>default-restore-instrumented-classes</id> <goals> <goal>restore-instrumented-classes</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>check</id> <goals> <goal>check</goal> </goals> </execution> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> </executions> <configuration> <!-- rules里面指定覆盖规则 --> <rules> <rule implementation="org.jacoco.maven.RuleConfiguration"> <element>BUNDLE</element> <limits> <!-- 指定方法覆盖到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>METHOD</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定指令覆盖到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>INSTRUCTION</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定行覆盖到80% --> <limit implementation="org.jacoco.report.check.Limit"> <counter>LINE</counter> <value>COVEREDRATIO</value> <minimum>0.80</minimum> </limit> <!-- 指定类覆盖到100%,不能遗失任何类 --> <limit implementation="org.jacoco.report.check.Limit"> <counter>CLASS</counter> <value>MISSEDCOUNT</value> <maximum>0</maximum> </limit> </limits> </rule> </rules> </configuration> </plugin> </plugins> </build>
-
jacocoTest.jar自动会修改Dockerfile 文件加入: jacocoagent.jar 和一个新端口 ,EXPOSE 6478 ; 修改ENTRYPOINT 命令。参考如下
from registry.cn-hangzhou.aliyuncs.com/wljs-ops/openjdk:8u212-b03 VOLUME /home/app ADD target/oms-order-server.jar oms-order-server.jar ADD jacocoagent.jar /tmp/jacocoagent.jar EXPOSE 9113 EXPOSE 9926 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone ENV JAVA_OPTS="$JAVA_OPTS-Dfile.encoding=UTF8 -Duser.timezone=GMT+08" ENTRYPOINT exec java -javaagent:/tmp/jacocoagent.jar=includes=*,output=tcpserver,port=9926,address=0.0.0.0 -Xms500m -Xmx2g $JAVA_OPTS -Djava.security.edg=file:/dev/./urandom -jar /server.jar
4、把jacocoagent.jar、jacococli.jar、jacocoTest.jar 传到jenkins机器上。
5、编译项目成功后,在racher上部署
【注】在端口Racher映射时,主机监听端口不要与已有项目的冲突。
6、项目部署成功后,通过在Jenkins机器上执行以下命令
dump覆盖率数据:java -jar jacococli.jar dump --address 192.168.20.214 --destfile b.exec --port 30202
生成报告:java -jar jacococli.jar report b.exec --classfiles oms-server/oms-order-server/target/classes/ --html report
-