woshikid / blog

Apache License 2.0
8 stars 1 forks source link

JaCoCo学习笔记 #159

Open woshikid opened 3 years ago

woshikid commented 3 years ago

Maven

on-the-fly模式(-javaagent)

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <configuration>
        <excludes>
            <exclude>**/model/*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>default-prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <!-- mvn test -->
        <execution>
            <id>default-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <!-- mvn verify -->
        <execution>
            <id>default-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <!-- BUNDLE, PACKAGE, CLASS, SOURCEFILE or METHOD -->
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <!-- INSTRUCTION: 字节码指令 -->
                                <!-- LINE: 代码行 -->
                                <!-- BRANCH: 分支条件 -->
                                <!-- COMPLEXITY: 圈复杂度 -->
                                <!-- METHOD: 方法 -->
                                <!-- CLASS: 类 -->
                                <counter>COMPLEXITY</counter>
                                <!-- TOTALCOUNT: 总数 -->
                                <!-- COVEREDCOUNT: 覆盖数量 -->
                                <!-- MISSEDCOUNT: 缺失数量 -->
                                <!-- COVEREDRATIO: 覆盖率 -->
                                <!-- MISSEDRATIO: 缺失率 -->
                                <value>COVEREDRATIO</value>
                                <!-- minimum: 最少 -->
                                <!-- maximum: 最多 -->
                                <minimum>0.60</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

offline模式(修改class文件)

<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <version>0.8.7</version>
    <classifier>runtime</classifier>
    <scope>test</scope>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    <configuration>
        <systemPropertyVariables>
            <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
        </systemPropertyVariables>
    </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>
        <!-- mvn test -->
        <execution>
            <id>default-restore-instrumented-classes</id>
            <phase>test</phase>
            <goals>
                <goal>restore-instrumented-classes</goal>
            </goals>
        </execution>
        <execution>
            <id>default-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <!-- mvn verify -->
        <execution>
            <id>default-check</id>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule>
                        <element>BUNDLE</element>
                        <limits>
                            <limit>
                                <counter>COMPLEXITY</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>0.60</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

Eclipse插件

JaCoCo的Eclipse插件叫EclEmma