最近家裏添丁,好久沒更新blog。現在終於又可以有時間繼續更新了。
繼續上一次的話題:Powermock and sonar jacoco的覆蓋率不兼容問題解決 1
在放棄了offline instrument之後,我們就轉戰普通的dymanic instrument,具體實現分兩步介紹,第一步是配置dymanic instrument,之前參考了官方配置:
http://www.eclemma.org/jacoco/trunk/doc/examples/build/pom.xml
<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生成ut和it對應的參數
<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方式解決覆蓋率不兼容問題。