简体   繁体   中英

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. Everything works well except one thing. I divided project in many maven sub modules:

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. It just contains integration tests for impl module. It's clear for me why it doesn't work. I'm getting such info in Jacoco logs:

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:

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

coverage analysis of it still fails, but message in Jacoco logs changes to:

[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. I also checked that compiled classes in D:\\project\\moduleA\\impl\\target\\classes contain debug data (all lines, vars and source).

I was trying with different paths inside sonar.java.binaries but result is always the same. 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?

Based on this question , I resorted to the Maven Ant plugin with JaCoCo's ant task library, which allows richer inclusion trees:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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