jacoco 离线模式配置jvm 参数

来源:互联网 发布:淘宝幸运抽奖 编辑:程序博客网 时间:2024/06/08 13:55

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

在使用这个实现的时候遇到了一个搞笑的问题问题,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";
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这个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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

解决了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>


原创粉丝点击