我已经使用 SonarQube 和 Jacoco 配置我的项目以实现代码覆盖。除了一件事,一切都很好。我将项目分成许多 Maven 子模块:
project (pom.xml) -
|-moduleA (no pom here)
| |- it (pom.xml) - integration tests for "impl" module
| |- impl (pom.xml) - implementaion + Unit tests
|-moduelB
| |- it (pom.xml) - integration tests for "impl" module
| |- impl (pom.xml) - implementaion + Unit tests
|-moduleC ...
我使用以下命令开始构建:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent -Dmaven.compiler.debug=true install sonar:sonar
问题是 Jacoco 可以很容易地支持同一模块内的单元测试覆盖率(所以在我的例子中是 impl
模块内的单元测试)但无法分析来自 it
模块的覆盖率它本身不包含任何实现类。它只包含 impl
模块的集成测试。我很清楚为什么它不起作用。我在 Jacoco 日志中得到这样的信息:
No JaCoCo analysis of project coverage can be done since there is no class files.
在我像这样在 moduleA/it/pom.xml 中定义 sonar.java.binaries
属性之后:
<properties>
<sonar.java.binaries>../impl/target/classes</sonar.java.binaries>
</properties>
it
的覆盖率分析仍然失败,但 Jacoco 日志中的消息更改为:
[INFO] Analysing D:\project\moduleA\it\target\jacoco.exec
[WARNING] Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?
我必须提到 D:\project\moduleA\it\target\jacoco.exec
文件存在。我还检查了 D:\project\moduleA\impl\target\classes
中的已编译类是否包含调试数据(所有行、变量和源代码)。
我尝试在 sonar.java.binaries
中使用不同的路径,但结果始终相同。我试过:
../impl/target/classes
../../impl/target/classes
../../../impl/target/classes
../impl/target
...
我如何配置 Jacoco (Sonar) 以允许它在与 jacoco.exec 文件相关的不同 Maven 子模块中查找类二进制文件?
最佳答案
基于 this question , 我求助于 Maven Ant plugin与 JaCoCo's ant task库,它允许更丰富的包含树:
<project ...>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<dependencies>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.ant</artifactId>
<version>${jacoco.version}</version>
</dependency>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>20020829</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>report-it-coverage</id>
<phase>post-integration-test</phase>
<goals><goal>run</goal></goals>
<configuration>
<skip>${skipITs}</skip>
<target name="report-it-coverage">
<taskdef name="jacoco-report" classname="org.jacoco.ant.ReportTask" classpathref="maven.plugin.classpath" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.runtime.classpath" />
<available file="${jacoco.it-coverage.data}" property="jacoco.exec.file.exists" />
<if>
<equals arg1="${jacoco.exec.file.exists}" arg2="true" />
<then>
<echo>Analyzing ITs coverage data from '${jacoco.it-coverage.data}'</echo>
<jacoco-report>
<executiondata>
<file file="${jacoco.it-coverage.data}" />
</executiondata>
<structure name="ROOT">
<classfiles>
<fileset dir="${project.basedir}/${project.parent.relativePath}">
<include name="**/target/classes/my/company/package/**/*.class" />
<exclude name="**/target/classes/my/test/glue/**/*" />
</fileset>
</classfiles>
</structure>
<html destdir="${project.reporting.outputDirectory}/jacoco-it" />
<xml destfile="${project.build.directory}/jacoco.xml" />
</jacoco-report>
</then>
<else>
<echo>Missing ITs coverage data file '${jacoco.it-coverage.data}'</echo>
</else>
</if>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
关于maven - Jacoco 代码覆盖 Sonar 和 Maven,用于单独模块中的集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910246/