简体   繁体   English

使用Sonar和maven进行Jacoco代码覆盖,以便在单独的模块中进行集成测试

[英]Jacoco code coverage with Sonar and maven for integration tests in separate module

I've configured my project with SonarQube and Jacoco for code coverage. 我已经使用SonarQube和Jacoco配置我的项目以进行代码覆盖。 Everything works well except one thing. 除了一件事,一切都很好。 I divided project in many maven sub modules: 我在许多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 ...

I start build using following command: 我使用以下命令开始构建:

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent -Dmaven.compiler.debug=true install sonar:sonar

The problem is that Jacoco can easily support Unit test coverage inside the same module (so in my case Unit Tests inside impl module) but cannot analyze coverage from it module which itself does not contain any implementation classes. 问题是,Jacoco可轻松支持在同一个模块内的单元测试覆盖率(所以在我的情况下,单元测试impl模块),但不能从分析覆盖率it模块,本身不包含任何实现类。 It just contains integration tests for impl module. 它只包含impl模块的集成测试。 It's clear for me why it doesn't work. 我很清楚为什么它不起作用。 I'm getting such info in Jacoco logs: 我在Jacoco日志中得到这样的信息:

No JaCoCo analysis of project coverage can be done since there is no class files.

After I've defined sonar.java.binaries property inside moduleA/it/pom.xml like this: 我在moduleA / it / pom.xml中定义了sonar.java.binaries属性,如下所示:

    <properties>
         <sonar.java.binaries>../impl/target/classes</sonar.java.binaries>
    </properties>

coverage analysis of it still fails, but message in Jacoco logs changes to: 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?

I must mention that D:\\project\\moduleA\\it\\target\\jacoco.exec file exists. 我必须提到D:\\project\\moduleA\\it\\target\\jacoco.exec文件存在。 I also checked that compiled classes in D:\\project\\moduleA\\impl\\target\\classes contain debug data (all lines, vars and source). 我还检查了D:\\project\\moduleA\\impl\\target\\classes中的编译类包含调试数据(所有行,变量和源)。

I was trying with different paths inside sonar.java.binaries but result is always the same. 我在sonar.java.binaries中尝试不同的路径,但结果总是一样的。 I've tried: 我试过了:

../impl/target/classes
../../impl/target/classes
../../../impl/target/classes
../impl/target
...

How can I configure Jacoco (Sonar) to allow it to find classes binaries in different maven sub module related to jacoco.exec file? 如何配置Jacoco(声纳)以允许它在与jacoco.exec文件相关的不同maven子模块中查找类二进制文件?

Based on this question , I resorted to the Maven Ant plugin with JaCoCo's ant task library, which allows richer inclusion trees: 基于这个问题 ,我使用了JaCoCo的ant任务库的Maven Ant插件 ,它允许更丰富的包含树:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM