简体   繁体   中英

Maven with Jacoco and Sonar 0% coverage

I have a scala project (with a few java files) built using maven (scala-maven-plugin). We have jacoco plugged in for code coverage (jacoco-maven-plugin) and that generates good-ish scala code coverage. We can see the html / csv reports in the typical place in /target and the scala coverage is all there and good.

However we cannot get the code coverage with sonar to work on the scala files. The plugin runs and it sends the java coverage so I know it's picking up something from the jacoco output, but the scala coverage is missing.

In addition if I run the jacoco:check goal as part of the build, it fails on the coverage, again citing only java coverage as the total coverage figure. This leads me to believe the problem is something to do with the way I've configured jacoco not with sonar.

Any help appreciated.

Here are the relevant parts of the pom

            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.1.3</version>
                <configuration>
                    <args>
                        <arg>-g:vars</arg>
                    </args>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.6.2.201302030002</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
           <!-- disable surefire -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven.plugin.surefire.version}</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
            <!-- enable scalatest -->
            <plugin>
                <groupId>org.scalatest</groupId>
                <artifactId>scalatest-maven-plugin</artifactId>
                <version>1.0-M2</version>
                <executions>
                    <execution>
                        <id>test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                            <junitxml>.</junitxml>
                            <parallel>true</parallel>
                            <tagsToExclude>IntegrationTest</tagsToExclude>
                            <filereports>ScalaTestSuite.txt</filereports>
                        </configuration>
                    </execution>
                    <execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
                            <junitxml>.</junitxml>
                            <parallel>true</parallel>
                            <tagsToInclude>IntegrationTest</tagsToInclude>
                            <filereports>ScalaIntegrationTestSuite.txt</filereports>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.sonar</groupId>
                <artifactId>sonar-maven3-plugin</artifactId>
                <version>3.4.1</version>
            </plugin>




  <sonar.host.url>http://vltapp02:9000/</sonar.host.url>
    <sonar.jdbc.url>jdbc:h2:tcp://vltapp02:9092/sonar</sonar.jdbc.url>
    <sonar.language>java</sonar.language>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.jacoco.reportPath>target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.surefire.reportsPath>target/surefire-reports</sonar.surefire.reportsPath>

May be jacoco/sonar check against source folders. In this case you should try to expose the scala source folder to other plugins (via 'add-source' goal):

         <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>3.1.3</version>
            <configuration>
                <args>
                    <arg>-g:vars</arg>
                </args>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>add-source</goal>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Can you please follow below steps :

Step-1 : Add below JaCoCo configuration in the pom.xml file under properties section

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

Step-2 : Add below JaCoCo plugin in the pom.xml file under Plug In section

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco.version}</version>
    <configuration>
        <skip>${maven.test.skip}</skip>
        <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
        <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
        <output>file</output>
        <append>true</append>
        <excludes>
            <exclude>*MethodAccess</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <phase>test-compile</phase>
        </execution>
        <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Step-3 : Execute below mvn command from project root folder(where pom file exist)

mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false

This command will create JaCoCo reports for us which will contain the code coverage reports at scalable-web-json-comparator\\scalable-web\\target\\coverage-reports\\ path. the code coverage report will be in jacoco-unit.exec file.

Step-4 : Execute below mvn command to push the sonar report to our sonarQube server.

mvn sonar:sonar -Dsonar.jacoco.reportPaths=target/coverage-reports/jacoco-unit.exec
-Dsonar.projectName="scalable-web" -Dsonar.projectKey="scalable-web" 
-Dsonar.host.url=http://localhost:9000
 -Dsonar.login=1c01ff3138588827f552cc6e7d4971ed004f5874

Step-5 : Once above build is successful, launch Sonar dashboard using the below URL and verify code coverage and unit test case percentage. http://localhost:9000

You can follow below urls for more information:

https://onlyfullstack.blogspot.com/2019/02/code-coverage-with-jacoco-sonar-and-maven.html

https://onlyfullstack.blogspot.com/2019/02/integrate-jacoco-plugin-with-sonar-and-maven.html

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