jacoco 离线模式配置jvm 参数

本文详细介绍如何使用Jacoco Maven插件来正确配置单元测试和集成测试的代码覆盖率收集过程,包括如何设置参数、避免报告文件为空等问题。

 

<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

在使用这个实现的时候遇到了一个搞笑的问题问题,jacoco的report文件内容是0,很是好奇,仔细看了jacoco-maven-plugin的代码之后发现,

    @Override
    public void executeMojo() {
        final String name = getEffectivePropertyName();
        final Properties projectProperties = getProject().getProperties();
        final String oldValue = projectProperties.getProperty(name);
        final String newValue = createAgentOptions().prependVMArguments(
                oldValue, getAgentJarFile());
        getLog().info(name + " set to " + newValue);
        projectProperties.setProperty(name, newValue);
    }
    String getEffectivePropertyName() {
        if (isPropertyNameSpecified()) {
            return propertyName;
        }
        if (isEclipseTestPluginPackaging()) {
            return TYCHO_ARG_LINE;
        }
        return SUREFIRE_ARG_LINE;
    }
    static final String SUREFIRE_ARG_LINE = "argLine";

这个plugin只是帮忙生成jacoco agent参数。然后把参数赋值到指定的变量或者默认变量argLine。具体如何使用在JVM上还需要自己额外设置,具体做法如下: 
首先使用jacoco maven plugin 添加prepare-agent属性,且声明一个变量让后面surfire配置参数调用。

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
         <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${sonar.jacoco.reportPath}</destFile>
                                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->                <dataFile>${sonar.jacoco.reportPath}</dataFile>
                <!-- Sets the output directory for the code coverage report. --><outputDirectory>target/site/jacoco</outputDirectory>
            </configuration>
        </execution>
         <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the Failsafe plugin is executed.
        -->
        <execution>
            <id>pre-integration-test</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${sonar.jacoco.itReportPath}</destFile>
                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for integration tests after
            integration tests have been run.
        -->
        <execution>
            <id>post-integration-test</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->                <dataFile>${sonar.jacoco.itReportPath}</dataFile>
                <!-- Sets the output directory for the code coverage report. --><outputDirectory>target/site/jacoco-it</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

然后分别在ut和it使用设置的参数(surefireArgLine和failsafeArgLine)。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.15</version>
    <configuration>
        <!-- Sets the VM argument line used when unit tests are run. -->
        <argLine>-XX:-UseSplitVerifier ${surefireArgLine}</argLine>
        <!-- Skips unit tests if the value of skip.unit.tests property is true -->
        <skipTests>${skip.unit.tests}</skipTests>
        <!-- Excludes integration tests when unit tests are run. -->
        <excludes>
            <exclude>**/IT*.java</exclude>
        </excludes>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.16</version>
    <executions>
        <!--
            Ensures that both integration-test and verify goals of the Failsafe Maven
            plugin are executed.
        -->
        <execution>
            <id>integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <!-- Sets the VM argument line used when integration tests are run. -->
                <argLine>${failsafeArgLine}</argLine>
                <!--
                    Skips integration tests if the value of skip.integration.tests property
                    is true
                -->
                <skipTests>${skip.integration.tests}</skipTests>
                <includes>
                   <include>**/IT*.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

解决了jacoco的问题之后,下一篇介绍powermockito的agent方式解决覆盖率不兼容问题。

如果不使用maven-failsafe-plugin插件,那么:

 

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <executions>
         <!--
            Prepares the property pointing to the JaCoCo runtime agent which
            is passed as VM argument when Maven the surefire plugin is executed.
        -->
        <execution>
            <id>pre-unit-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->
                <destFile>${sonar.jacoco.reportPath}</destFile>
                                <!--
                    Sets the name of the property containing the settings
                    for JaCoCo runtime agent.
                -->                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <!--
            Ensures that the code coverage report for unit tests after
            unit tests have been run.
        -->
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <!-- Sets the path to the file which contains the execution data. -->              
  <dataFile>${sonar.jacoco.reportPath}</dataFile>
                <!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/site/jacoco</outputDirectory>
            </configuration></execution></executions></plugin>

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值