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

时间:2017-05-11 08:27:17

标签: maven sonarqube code-coverage jacoco jacoco-maven-plugin

我已经使用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(声纳)以允许它在与jacoco.exec文件相关的不同maven子模块中查找类二进制文件?

1 个答案:

答案 0 :(得分:0)

基于this question,我求助于Maven Ant pluginJaCoCo'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>