Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Module Maven Project Code Coverage Issue on Sonar

I have a question related to multi module maven project with JaCoCo and SONAR.

I have a parent and 3 child modules.

parent |-child1 - pom.xml |-child2 - pom.xml |-child3 - pom.xml |-pom.xml

I include the JaCoCo plugin in the parent pom.xml. When I run the mvn clean install sonar:sonar build from parent pom.xml, I see that each child generates its own jacoco.exec file. Something like this child1/target/jacoco.exec, child2/target/jacoco.exec etc . However, there is no jacoco.exec been generated in the parent level.

When I run my sonar analysis, I see that the unit test coverage is showing up as 0.0% on the sonar dashboard.

My question is
1. What should I do to see the unit test coverage for the entire project?
2. To show the one unit test coverage, Does SONAR pick the jacoco.exec file from the parent level or from the child level?

Please help. This is really a road blocker for me. Appreciate all your inputs.

like image 716
user3810601 Avatar asked Nov 01 '22 17:11

user3810601


1 Answers

I was facing the same issue, then I included the following in each module pom xml and it worked:

<!-- Sonar START -->
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
        <sonar.language>java</sonar.language>
        <jacoco.version>0.7.9</jacoco.version>
        <!-- Sonar START -->

Plugins :

<!-- SONAR config START -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <configuration>
                    <destFile>${sonar.jacoco.reportPath}</destFile>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Project Base Path (WEB):: ${project.basedir}</echo>
                                <echo>Jacoco exec target path (WEB):: ${sonar.jacoco.reportPath}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.sonarsource.scanner.maven</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>3.2</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sonar-maven-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <argLine>${argLine}</argLine>
                </configuration>
            </plugin>
            <!-- SONAR config END -->

And configure in Jenkins for "Goals and options" as:

sonar:sonar

For Running specific tests :

sonar:sonar -DfailIfNoTests=false -Dtest=<Test Class> test
like image 162
Shankhadeep Karmakar Avatar answered Nov 09 '22 13:11

Shankhadeep Karmakar