OSGi 代码覆盖 - Jacoco + Easymock
OSGi Code coverage - Jacoco + Easymock
我目前正在开发一个 Java OSGi 项目(基于 Apache felix 运行时),其项目设置如下所示:
- 包装 | parent maven 项目
- 坚持 |真正的插件
- persistence.tests |测试插件(确实是一个片段项目,片段宿主上面的持久性插件)
- ...其他如上
基本上我使用maven + tycho来管理项目的生命周期。整个过程流经一个持续的集成管道,其中涉及 jenkins 的构建、测试、部署和转发代码分析到 Sonarqube 服务器。就像上面提到的,测试是通过指向要测试的 OSGi 包的 Fragment 项目来实现的。在这些测试中,我使用 EasyMock 库来生成模拟的 OSGi 包。
为了让 Sonarqube 知道测试覆盖率,我必须将 Jacoco(maven 插件)添加到我的工具集中。在对我的 parent pom.xml 文件的配置进行了一些调整之后,我最终得到了部分工作的东西:jacoco code coverage is only working for 类 包含在测试插件中(片段项目)。正如您可能猜到的那样——虽然聊胜于无——这个结果远没有用。我需要评估真实 OSGi 包的测试覆盖率。经过一些 googling 我明白问题可能与 EasyMock 库的使用有关,因为这在执行期间改变了原始 类 导致测试 类 和真实 类 之间的不匹配.根据我的理解,要解决我需要禁用 jacoco on-the-fly 检测并改用离线检测。
尽管如此,我还是无法理解:
- 这到底是什么意思
- 怎么做
有人可以回复吗?
这是我正在 运行 生成 jacoco 报告的 maven 命令
mvn -f com.mycompany.osgi.myproject.pkg/pom.xml clean test
低于我现在的parentpom.xml
<?xml version="1.0" encoding="UTF-8"?><project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.osgi</groupId>
<artifactId>com.mycompany.osgi.myproject.pkg</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<properties>
<tycho.version>1.0.0</tycho.version>
<surefire.version>2.16</surefire.version>
<main.basedir>${project.basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.7.9</jacoco.version>
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>${project.basedir}/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>
<modules>
<module>../com.mycompany.osgi.myproject.core.persistence</module>
<module>../com.mycompany.osgi.myproject.core.persistence.tests</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
...
</repositories>
<distributionManagement>
...
</distributionManagement>
,使用以下 jacoco 插件配置解决了我的问题
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>../com.mycompany.myproject.pkg/target/jacoco.exec</dataFile>
<destFile>../com.mycompany.myproject.pkg/target/jacoco.exec</destFile>
<outputDirectory>../com.mycompany.myproject.pkg/target/site/jacoco</outputDirectory>
</configuration>
</plugin>
和这个项目配置来指示 sonarqube 读取预期的资源
<properties>
...
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>com.mycompany.myproject.pkg/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>com.mycompany.myproject.pkg/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>
我目前正在开发一个 Java OSGi 项目(基于 Apache felix 运行时),其项目设置如下所示:
- 包装 | parent maven 项目
- 坚持 |真正的插件
- persistence.tests |测试插件(确实是一个片段项目,片段宿主上面的持久性插件)
- ...其他如上
基本上我使用maven + tycho来管理项目的生命周期。整个过程流经一个持续的集成管道,其中涉及 jenkins 的构建、测试、部署和转发代码分析到 Sonarqube 服务器。就像上面提到的,测试是通过指向要测试的 OSGi 包的 Fragment 项目来实现的。在这些测试中,我使用 EasyMock 库来生成模拟的 OSGi 包。 为了让 Sonarqube 知道测试覆盖率,我必须将 Jacoco(maven 插件)添加到我的工具集中。在对我的 parent pom.xml 文件的配置进行了一些调整之后,我最终得到了部分工作的东西:jacoco code coverage is only working for 类 包含在测试插件中(片段项目)。正如您可能猜到的那样——虽然聊胜于无——这个结果远没有用。我需要评估真实 OSGi 包的测试覆盖率。经过一些 googling 我明白问题可能与 EasyMock 库的使用有关,因为这在执行期间改变了原始 类 导致测试 类 和真实 类 之间的不匹配.根据我的理解,要解决我需要禁用 jacoco on-the-fly 检测并改用离线检测。
尽管如此,我还是无法理解:
- 这到底是什么意思
- 怎么做
有人可以回复吗?
这是我正在 运行 生成 jacoco 报告的 maven 命令
mvn -f com.mycompany.osgi.myproject.pkg/pom.xml clean test
低于我现在的parentpom.xml
<?xml version="1.0" encoding="UTF-8"?><project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.osgi</groupId>
<artifactId>com.mycompany.osgi.myproject.pkg</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<properties>
<tycho.version>1.0.0</tycho.version>
<surefire.version>2.16</surefire.version>
<main.basedir>${project.basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.version>0.7.9</jacoco.version>
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>${project.basedir}/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>
<modules>
<module>../com.mycompany.osgi.myproject.core.persistence</module>
<module>../com.mycompany.osgi.myproject.core.persistence.tests</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho.version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>${surefire.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<configuration>
<testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
</configuration>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<repositories>
...
</repositories>
<distributionManagement>
...
</distributionManagement>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<dataFile>../com.mycompany.myproject.pkg/target/jacoco.exec</dataFile>
<destFile>../com.mycompany.myproject.pkg/target/jacoco.exec</destFile>
<outputDirectory>../com.mycompany.myproject.pkg/target/site/jacoco</outputDirectory>
</configuration>
</plugin>
和这个项目配置来指示 sonarqube 读取预期的资源
<properties>
...
<!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>com.mycompany.myproject.pkg/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>com.mycompany.myproject.pkg/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>