Maven拥有Jacoco和Sonar的覆盖率达到0%

问题描述 投票:4回答:2

我有一个使用maven(scala-maven-plugin)构建的scala项目(带有一些Java文件)。我们已经插入了jacoco以进行代码覆盖(jacoco-maven-plugin),并且可以生成良好的Scala代码覆盖范围。我们可以在/ target中的典型位置看到html / csv报告,并且scala覆盖范围很广。

但是我们无法获得声纳的代码覆盖率来处理scala文件。该插件运行并发送Java覆盖,因此我知道它正在从jacoco输出中获取内容,但缺少scala覆盖。

另外,如果我在构建过程中运行了jacoco:check目标,它的覆盖率将失败,再次将Java覆盖率作为总覆盖率数字。这使我相信问题出在我没有用声纳配置jacoco的方式上。

感谢任何帮助。

这里是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>
scala maven sonarqube scalatest jacoco
2个回答
0
投票

可能是对源文件夹的雅各布/声纳检查。在这种情况下,您应该尝试将scala源文件夹暴露给其他插件(通过“ add-source”目标):

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

0
投票

您能否按照以下步骤操作:

[Step-1:在属性部分下的pom.xml文件中在JaCoCo配置下面添加]

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

在“插件”部分下的pom.xml文件的JaCoCo插件下面添加]
<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:

从项目根文件夹(存在pom文件的位置)执行以下mvn命令
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install -Dmaven.test.failure.ignore=false

此命令将为我们创建JaCoCo报告,其中将在scalable-web-json-comparator \ scalable-web \ target \ coverage-reports \路径中包含代码覆盖率报告。代码覆盖率报告将在jacoco-unit.exec文件中。

Step-4:

执行以下mvn命令将声纳报告推送到我们的sonarQube服务器。
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:

一旦以上构建成功,请使用以下URL启动Sonar仪表板,并验证代码覆盖率和单元测试用例百分比。http://localhost:9000
© www.soinside.com 2019 - 2024. All rights reserved.