我是 JaCoCo 的新手,我在生成代码覆盖率报告时遇到了问题。
这是我的项目结构:
我的集成测试位于“...-integration-tests”模块中。当我使用 mvn 构建我的项目时,我在日志记录中得到以下信息:
[INFO] Skipping JaCoCo execution due to missing classes directory: ...-integration-tests\target\classes
这是真的,因为我的编译代码只在相应模块的目标>类中可用。
实现此功能的最佳方法是什么?提前致谢!
最佳答案
发生这种情况是因为 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>
关于java - JaCoCo:缺少类目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30301596/