我是JaCoCo的新手,我在生成代码覆盖率报告时遇到了麻烦。
这是我的项目结构:
我的集成测试存在于“...- integration-tests”模块中。当我使用mvn构建我的项目时,我在日志记录中得到以下内容:
[INFO] Skipping JaCoCo execution due to missing classes directory: ...-integration-tests\target\classes
这是正确的,因为我的编译代码仅在相应模块的目标>类中可用。
使这项工作的最佳方法是什么?提前谢谢!
答案 0 :(得分:1)
这是因为JaCoCo“报告”mojo试图在“默认”maven项目布局中查找源和类:
@Override
boolean canGenerateReportRegardingClassesDirectory() {
return new File(getProject().getBuild().getOutputDirectory()).exists();
}
布局与您的布局相似我通过明确设置 build.sourceDirectory 和 build.outputDirectory 来指向测试模块的内部,从而绕过了JaCoCo配置限制。之后,maven尝试第二次编译它,所以我还必须覆盖默认编译执行,我的测试模块pom.xml的重要(和可共享)部分现在看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<project ...
...
<parent>
...
</parent>
<dependencies>
...
</dependencies>
<properties>
...
</properties>
<build>
<sourceDirectory>../../Source</sourceDirectory> <!-- tested sources root, in proper layout: src/main/java -->
<outputDirectory>../bin</outputDirectory> <!-- tested classes root, in proper layout: target/classes -->
<testSourceDirectory>${project.basedir}/../../Test/java</testSourceDirectory> <!-- if tests code also taken from outside -->
<testResources>
...
</testResources>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<!-- disabling default-compile -->
<execution>
<id>default-compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
<configuration>
<source>1.7</source>
<target>1.7</target>
<includes/>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</configuration>
</execution>
...
</executions>
</plugin>
<!-- typical jacoco usage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
<configuration>
...
<argLine>${argLine} -XX:PermSize=512M -XX:MaxPermSize=512M -Xmx1024M</argLine>
...
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
<executions>
...
</executions>
</plugin>
</plugins>
</build>
</project>