JaCoCo:缺少类目录

2024-02-06

我对 JaCoCo 相当陌生,在生成代码覆盖率报告时遇到问题。

这是我的项目结构:

我的集成测试位于“...-integration-tests”模块中。当我使用 mvn 构建项目时,我在日志记录中收到以下内容:

[INFO] Skipping JaCoCo execution due to missing classes directory: ...-integration-tests\target\classes

这是真的,因为我编译的代码仅在相应模块的 target>classes 中可用。

使其发挥作用的最佳方法是什么?提前致谢!


发生这种情况是因为 JaCoCo“报告”mojo 试图在“默认”maven 项目布局中查找源和类:

@Override
boolean canGenerateReportRegardingClassesDirectory() {
    return new File(getProject().getBuild().getOutputDirectory()).exists();
}

具有与您类似的布局,我能够通过显式设置来规避 JaCoCo 配置限制构建源目录 and 构建.输出目录指向您测试的模块的内部结构。之后,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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JaCoCo:缺少类目录 的相关文章