【问题标题】:Jacoco code coverage show 0% coverage on JenkinsJacoco 代码覆盖率显示 Jenkins 的覆盖率为 0%
【发布时间】:2017-05-31 02:48:06
【问题描述】:

我已经阅读了类似问题的大部分可用答案,但没有一个适合回答我的问题。

我在 pom 文件中的个人资料如下所示:

            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.7.201606060606</version>
                    <executions>
                        <!-- Prepares the property pointing to the JaCoCo runtime agent which 
                            is passed as VM argument when Maven the Surefire plugin is executed. -->
                        <execution>
                            <id>pre-unit-test</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the path to the file which contains the execution data. -->
                                <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                                <!-- Sets the name of the property containing the settings for JaCoCo 
                                    runtime agent. -->
                                <propertyName>surefireArgLine</propertyName>
                            </configuration>
                        </execution>
                        <!-- Ensures that the code coverage report for unit tests is created 
                            after unit tests have been run. -->
                        <execution>
                            <id>post-unit-test</id>
                            <phase>test</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the path to the file which contains the execution data. -->
                                <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                                <!-- Sets the output directory for the code coverage report. -->
                                <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19.1</version>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                    <executions>
                        <execution>
                            <id>surefire-unit-tests</id>
                            <phase>test</phase>
                            <goals>
                                <goal>test</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the VM argument line used when unit tests are run. -->
                                <argLine>${surefireArgLine}</argLine>
                                <!-- Skips unit tests if the value of skip.unit.tests property is 
                                    true -->
                                <skipTests>${skip.unit.tests}</skipTests>
                                <!-- Excludes integration tests when unit tests are run. -->
                                <skip>false</skip>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.19.1</version>
                    <executions>
                        <!-- Ensures that both integration-test and verify goals of the Failsafe 
                            Maven plugin are executed. -->
                        <execution>
                            <id>integration-tests</id>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                            <configuration>
                                <!-- Sets the VM argument line used when integration tests are run. -->
                                <argLine>${failsafeArgLine}</argLine>
                                <!-- Skips integration tests if the value of skip.integration.tests 
                                    property is true -->
                                <skipTests>${skip.integration.tests}</skipTests>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>

当我以 maven 目标 clean install -P test-coverage 运行我的詹金斯工作时

我在我的 Jenkins 日志中看到了这一点:

`[JaCoCo plugin] Collecting JaCoCo coverage data...
[JaCoCo plugin] **/target/coverage-reports/jacoco-ut.exec;**/target/classes;**/src/main/java; locations are configured
[JaCoCo plugin] Number of found exec files for pattern **/target/coverage-reports/jacoco-ut.exec: 0
[JaCoCo plugin] Saving matched execfiles:  
[JaCoCo plugin] Saving matched class directories for class-pattern: **/target/classes: 
[JaCoCo plugin] Saving matched source directories for source-pattern: **/src/main/java: 
[JaCoCo plugin] Loading inclusions files..
[JaCoCo plugin] inclusions: []
[JaCoCo plugin] exclusions: []
[JaCoCo plugin] Thresholds: JacocoHealthReportThresholds [minClass=0, maxClass=0, minMethod=0, maxMethod=0, minLine=0, maxLine=0, minBranch=0, maxBranch=0, minInstruction=0, maxInstruction=0, minComplexity=0, maxComplexity=0]
[JaCoCo plugin] Publishing the results..
[JaCoCo plugin] Loading packages..
[JaCoCo plugin] Done.
[JaCoCo plugin] Overall coverage: class: 0, method: 0, line: 0, branch: 0, instruction: 0`

这是我为 Jacoco 配置的 Jenkins 的样子:

【问题讨论】:

  • 注意 Jenkins 日志中的 Number of found exec files for pattern **/target/coverage-reports/jacoco-ut.exec: 0 行 - 您需要了解为什么没有这样的文件,这需要查看远高于此行的构建日志。
  • @Godin 刚刚取得了成功。实际上构建目录与詹金斯工作的工作区不同。
  • 还将执行文件位置更改为**/jacoco.exec,最好按照插件的建议进行操作。
  • 那么要成为一个优秀的 StackOverflow 公民,请删除您的问题,或者在上面发布正确的答案 - meta.stackexchange.com/questions/17845/…
  • 这也可能发生在其他人身上,因此回答它会对其他人有所帮助。

标签: jenkins maven-2 code-coverage jacoco jacoco-maven-plugin


【解决方案1】:

就我而言,我使用的是 jacocoant 0.6,而 jenkins 的 jaCoCo 插件是 2.2.0。 来自 jaCoCo 插件 github 页面:

2.0.0 及更高版本需要使用 JaCoCo 0.7.5 或更高版本,如果您的项目仍使用 JaCoCo 0.7.4,插件将不再显示任何代码覆盖率数字!在这种情况下,请使用 1.0.19 版本,直到您可以在代码库中更新 jacoco。 https://github.com/jenkinsci/jacoco-plugin

【讨论】:

    【解决方案2】:

    在我的情况下,它无法获取 exec 文件。

    这是因为我的构建目录与 Jenkins 工作的 WORKSPACE 不同。当我将构建目录更改为 WORKSPACE 时,神奇的事情发生了。

    【讨论】:

    • 嗨,您能否具体说明您所做的更改。我收到同样的错误
    • @SoubhikBanerjee,我认为我的 Jenkins 工作区与默认的 $WORKSPACE 不同。您可以尝试提供 .exec 文件的绝对路径。
    【解决方案3】:

    就我而言,Jacoco 插件是 1.0.19,但我使用的是 Jacoco-maven-plugin 0.7.9,它不兼容。

    所以,在将 Jacoco-maven-plugin 更改为 0.7.4.x 之后。我得到了关于 Jenkins 的正确报道

    【讨论】:

      猜你喜欢
      • 2016-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2012-11-02
      • 1970-01-01
      • 2014-11-09
      • 2015-10-06
      相关资源
      最近更新 更多