Cobertura不支持JDK8,又来整Jacoco。。。
一、配置pom文件方式
1.1 说明
在maven的pom文件中配置信息,在tomcat运行时会自动统计数据,但是需要在tomcat结束的时候生产 jacoco.exec报告。如果需要解析该报告,使用命令时指定的class文件不能为配置pom文件后的,需要原生的class文件来生产html或xml报告。
1.2 pom文件
1.2.1 插件
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.2</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
1.2.2 依赖
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<version>0.7.9</version>
<scope>test</scope>
</dependency>
1.3 命令
执行生成报告命令如下:
java -jar jacococli.jar report [*.exec] --classfiles [path] --html [path]
[*.exec] 填写生成的exec文件路径,可多个
--classfiles 后面的path路径填写原生的class文件路径,不能使用配置过pom文件的。
--html 后面的path路径填写需要生成html报告的路劲
二、修改JAVA_OPTS方式
2.1 说明
需要修改JVM的配置信息,添加启动命令。但是可以不停止JVM的情况下获取当前的代码覆盖率信息,并无需修改pom文件。
2.2 配置
2.2.1 JVM配置
添加JAVA_OPTS配置如下:
-javaagent:[yourpath/]jacocoagent.jar=includes=*,output=tcpserver,port=8494,address=10.10.10.10
[yourpath/] 填写下载的jaocoagent.jar包路径
Includes 执行分析的路径列表,一般使用通配符*,可以指定测试路径
output 使用tcpserver , 将输入输出到连接该地址的访问
port 获取数据的端口号
address 获取数据的访问地址
更多配置见:http://www.eclemma.org/jacoco/trunk/doc/agent.html
2.2.2 生成exec文件
命令如下:
java -jar jacococli.jar dump [--address <address>] --destfile [path] --port [port] --reset [--retry <number>]
--address <address> 填写目标地址(默认localhost)
--destfile [path] 文件生成地址
--port [port] 端口号(默认6300)
--reset 获取数据后,重置连接目标的数据
--retry <number> 重新连接次数(默认10)
2.2.3 生成报告
和上个方式相关:
java -jar jacococli.jar report [*.exec] --classfiles [path] --html [path]
[*.exec] 填写生成的exec文件路径,可多个
--classfiles 后面的path路径填写原生的class文件路径,不能使用配置过pom文件的。
--html 后面的path路径填写需要生成html报告的路劲
更多命令参见:http://www.jacoco.org/jacoco/trunk/doc/cli.html