In my multi-module project I have a code and tests in separate modules. When I run maven command 'mvn clean install -DskipTests=false'
, jacoco.exec file is generated in test module but Jacoco complains about missing classes directory. In jacoco-report module, the report is generated but its empty.
在我的多模块项目中,我将代码和测试放在不同的模块中。当我运行maven命令‘MVN CLEAN INSTALL-DskipTest=FALSE’时,在测试模块中生成了Jacoco.exec文件,但Jacoco报告缺少CLASS目录。在Jacoco-Report模块中,报告是生成的,但它是空的。
How can I configure it to generate report or to be more specific how can I point Jacoco to the classes directory?
我如何配置它以生成报告,或者更具体地说,我如何将Jacoco指向CLASS目录?
Here is structure of my project:
以下是我的项目的结构:
My pom file from 'multi-module':
我的POM文件来自‘多模块’:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<argLine>
--illegal-access=permit
</argLine>
<argLine>@{argLine}</argLine>
<trimStackTrace>false</trimStackTrace>
<skipTests>${skipTests}</skipTests>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<configuration>
<destFile>${project.basedir}/../multi-module-core/target/jacoco.exec</destFile>
</configuration>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.basedir}/../multi-module-tests/target/jacoco.exec</dataFile>
<includes>
<include>${project.basedir}/../multi-module-core/target/classes/**</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>`
Pom file from 'multi-module-core':
来自‘多模块核心’的POM文件:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
Pom file from 'multi-module-tests':
多模块测试中的POM文件:
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>11</release>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18</version>
<configuration>
<argLine>@{argLine}</argLine>
<skipTests>${skipTests}</skipTests>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<configuration>
<filesets>
<fileset>
<directory>.</directory>
<includes>
<include>**/*.log</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</build>
Pom file from 'jacoco-report':
来自‘Jacoco-Report’的POM文件:
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
<configuration>
<dataFileIncludes>
<dataFileInclude>**/jacoco.exec</dataFileInclude>
</dataFileIncludes>
<outputDirectory>
${project.reporting.outputDirectory}/jacoco-aggregate
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So far I tried multiple configurations from all over the internet but with the same result. Appreciate any help or suggestions.
到目前为止,我从互联网上尝试了多种配置,但结果都是一样的。感谢任何帮助或建议。
EDIT 1:
As suggested I run mvn command in debug mode:
编辑1:按照建议,我在调试模式下运行MVN命令:
mvn clean verify -DskipTests=false -X
IT turns out that the following path is wrong:
事实证明,以下路径是错误的:
<include>${project.basedir}/../multi-module-core/target/classes/**</include>
and it points to : [C:\Projects\jococo\test\multi-module\multi-module-tests/../multi-module-core/target/classes/]** .
它指向:[C:\Projects\jococo\test\multi-module\multi-module-tests/../multi-module-core/target/classes/]**。
But even when I hard coded the path to C:\Projects\jococo\test\multi-module\multi-module-core\target\classes
但即使我硬编码了通往C:\Projects\jococo\test\multi-module\multi-module-core\target\classes的路径
Here is what I got during jacoco:report phase:
以下是我在Jacoco:报告阶段得到的信息:
includes = [C:\Projects\jococo\test\multi-module\multi-module-core\target\classes]
It looks like the paths pointing to the correct directory with classes but I still got the same error message: Skipping JaCoCo execution due to missing classes directory.
它看起来像是指向包含类的正确目录的路径,但我仍然收到相同的错误消息:由于缺少类目录,正在跳过JaCoCo执行。
EDIT 2:
编辑2:
For the reference here is GitHub repo to the project: https://github.com/KonarzewskiP/jacoco-multi-module
此处引用的是GitHub对该项目的回购:https://github.com/KonarzewskiP/jacoco-multi-module
更多回答
First why do you have unit tests in a separate module instead of the module where the code lives? What is the reason? What kind of problem are you trying to solve?
首先,为什么将单元测试放在单独的模块中,而不是代码所在的模块中?原因是什么?你想解决什么样的问题?
Also using old versions of maven-surefire-plugin, maven-compiler-plugin etc. I strongy recommend to upgrade to the most recent version for all plugin check here: maven.apache.org/plugins
我也使用了旧版本的maven-surefire-plugin,maven-编译器-plugin等,我强烈建议升级到最新版本以查看所有插件:maven.apache.org/plugins
This is simplified version of the project which I am working on. The real project has junit SOAP tests under '.src.test.java' folder and xml files with SOAP tests under 'test.souapui' folder. In the 'multi-module' project, I intentionally used the old versions of the dependencies, because they are the same ones I use in my work project.
这是我正在工作的项目的简化版本。真正的项目在‘.src.test.java’文件夹下有junit soap测试,在‘test.Souapui’文件夹下有带有Soap测试的XML文件。在“多模块”项目中,我故意使用依赖项的旧版本,因为它们与我在工作项目中使用的版本相同。
So this means first SOAP tests are integration tests and should be executed by failsafe-plugin with the appropriate naming convention (*IT.java).. also that means you should update your project. Apart from that it does not answer my question why you have unit tests in a separate module which makes no sense ... unit tests should be in the same module as your production code which you are testing.
因此,这意味着第一个SOAP测试是集成测试,应该由具有适当命名约定(*IT.java)的FailSafe插件执行。这也意味着你应该更新你的项目。除此之外,它没有回答我的问题,为什么你在一个单独的模块中进行单元测试,这是没有意义的……单元测试应该与您正在测试的产品代码位于相同的模块中。
You are right, having unit tests in separate project does not make sense. The only reason why i did this in 'multi-module' project, was to simplify the structure of my work project and highlight the "Skipping JaCoCo execution due to missing classes directory." error. So if I understand it correct, the only possibility to generate JaCoCo report in case of 'multi-module' project, is to configure it as a integration test?
您是对的,在单独的项目中进行单元测试是没有意义的。我之所以在“多模块”项目中这样做,唯一的原因是简化我的工作项目的结构,并突出显示“由于缺少类目录而跳过JaCoCo执行”。错误。所以,如果我理解正确的话,在‘多模块’项目中生成JaCoCo报告的唯一可能性是将其配置为集成测试?
There are many reasons for the problem, but consider this step to find out the configuration missing section.
出现此问题的原因有很多,但请考虑此步骤以找出配置缺少的部分。
Check Data File Location:
In your multi-module-tests
module's pom.xml
, ensure that the jacoco.exec
file is being generated in the correct location. The destFile
property should specify the full path to where the jacoco.exec
file should be saved, which is typically within the target directory of the module.
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
</configuration>
Check Include Patterns:
In the jacoco-report
module's pom.xml
, your <dataFileIncludes>
pattern should match the location of jacoco.exec
files from all your modules. Make sure it's set correctly to include all the modules.
<dataFileIncludes>
<dataFileInclude>/target/jacoco.exec</dataFileInclude>
</dataFileIncludes>
Ensure Correct Execution Order:
When running mvn clean install -DskipTests=false
, make sure that the modules are executed in the correct order. Dependencies between modules must be resolved in the right sequence to ensure that coverage data is collected properly.
Run verify
Phase:
Ensure that you run the verify
phase to generate the reports in the jacoco-report
module, as specified in your jacoco-report
module's pom.xml
.
mvn clean verify
Verify Relative Paths:
Double-check that relative paths are correctly configured in your POM files, especially for properties like destFile
. Relative paths should be based on the current module's location.
Plugin Versions Compatibility:
Verify that you are using compatible versions of Maven, JaCoCo, and related plugins. Incompatibilities can lead to issues with report generation.
Debugging Output:
Enable debugging output (-X
flag) when running Maven (mvn clean install -DskipTests=false -X
) to get more detailed information about what is happening during the build process. This might help pinpoint the issue.
Check Classpath Configuration:
Ensure that the classes and test classes directories are correctly configured in your POM files for both the jacoco-maven-plugin
and the maven-surefire-plugin
. Verify that the includes
section in jacoco-maven-plugin
covers the classes you want to include in the report.
After making these adjustments, try running the build again (mvn clean verify
) and check if the JaCoCo report is generated correctly in the jacoco-report
module with the expected coverage data.
在进行这些调整之后,尝试再次运行构建(MVN CLEAN VERIFY),并检查JaCoCo报告是否使用预期的覆盖率数据在Jacoco-Report模块中正确生成。
Hope I was helpful.
希望我能帮上忙。
更多回答
Thank you for your suggestions. Unfortunately, I double checked everything and it seems all good.
谢谢你的建议。不幸的是,我仔细检查了一切,一切似乎都很好。
Can you explain that part: Verify that you are using compatible versions of Maven, JaCoCo, and related plugins.
?
您能解释一下这一部分吗:验证您使用的是兼容版本的Maven、JaCoCo和相关插件。
To address this, you should consult the documentation for each tool and library you're using in your project to find information on their compatible versions. Typically, these documents will specify which versions are known to work well together. You may need to update or downgrade one or more of these components to ensure compatibility.
要解决此问题,您应该参考项目中使用的每个工具和库的文档,以查找有关它们的兼容版本的信息。通常,这些文档将指定已知哪些版本可以很好地协同工作。您可能需要更新或降级其中的一个或多个组件以确保兼容性。
我是一名优秀的程序员,十分优秀!