最近看Jacoco源码并进行修改来实现增量覆盖率的功能,Jacoco的插桩使用分为on-the-fly和offline两种方式:
- on-the-fly:通过在程序运行时指定javaagent,运行产出只包含覆盖率的结果Jacoco.exec,对原始jar不做修改。
- offline:通过修改字节码生成插桩后的字节码,然后执行插桩后的字节码运行,同样产出覆盖率文件Jacoco.exec。
通过maven插件均可以应用on-the-fly和offline两种模式。
offline模式
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8-SNAPSHOT</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>default-report</id>
<goals>
<goal>report</goal>