在Maven站点中包括Jacoco报告

r7s23pms  于 10个月前  发布在  Maven
关注(0)|答案(3)|浏览(107)

我已经在我的项目中集成了jacoco-maven-plugin,基于这个优秀的指南:http://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/
Jacoco插件运行良好。但是,maven-site-plugin不包括网站中的Jacoco报告。更具体地说:“项目报告”部分不列出Jacoco报告。Jacoco报告本身在target/site/jacoco-uttarget/site/jacoco-it目录中可用。
这就是我所做的(到目前为止没有成功)。
首先,将jacoco-maven-plugin作为插件包含在我的pom.xmlbuild部分中,如上面引用的指南中所述。我使用的是Jacoco版本0.6.4.201312101107。

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
  <executions>
    <!-- scissors... -->
    <!-- report goal is bound to the pre-site phase -->
  </executions>
</plugin>

字符串
第二,将jacoco-maven-plugin包含在我的pom.xmlreport部分:没有成功。

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
</plugin>


第三,尝试在report部分中添加reportsets部分到jacoco-maven-plugin:没有成功。

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>${jacoco.plugin.version}</version>
  <reportSets>
    <reportSet>
      <reports>
        <report>report</report>
      </reports>
    </reportSet>
  </reportSets>
</plugin>


任何人都可以帮助我使maven-site-plugin引用的覆盖报告生成的Jacoco在'项目报告'部分的网站?

9lowa7mx

9lowa7mx1#

我发现jacoco是非常棘手的,下面的配置为我工作,我把它放在一起的点点滴滴从博客中的属性

<jacoco.reportPath>${main.basedir}/target/jacoco.exec</jacoco.reportPath>
    <jacoco.itReportPath>${main.basedir}/target/jacoco-it.exec</jacoco.itReportPath>

字符串
在建筑物内

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.6.4.201312101107</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <propertyName>utCoverageAgent</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${sonar.jacoco.reportPath}</dataFile>
                        </configuration>
                    </execution>
                    <!-- prepare agent for measuring integration tests -->
                    <execution>
                        <id>agent</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.itReportPath}</destFile>
                            <propertyName>itCoverageAgent</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${sonar.jacoco.itReportPath}</dataFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

       <!-- Reporting plugins -->
        <plugin>
            <artifactId>maven-site-plugin</artifactId>
            <version>${plugin.site.version}</version>
            <configuration>
                <attach>true</attach>
                <reportPlugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-changelog-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <type>range</type>
                            <range>1</range>
                            <!--<displayFileDetailUrl>${project.scm.url}/tree/master/%FILE%</displayFileDetailUrl>-->
                            <headingDateFormat>MM-dd-yyyy</headingDateFormat>
                            <outputEncoding>${project.reporting.outputEncoding}</outputEncoding>

                        </configuration>
                        <reports>
                            <report>changelog</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-project-info-reports-plugin</artifactId>
                        <version>2.7</version>
                        <configuration>
                            <dependencyDetailsEnabled>false</dependencyDetailsEnabled>
                            <dependencyLocationsEnabled>false</dependencyLocationsEnabled>
                        </configuration>
                        <!-- simpler configuration without reportSets available for usual cases -->
                        <!-- distribution-management, index, dependencies, help, issue-tracking, plugins, cim,
                        license, dependency-management, mailing-list, project-team, dependency-convergence,
                        scm, plugin-management, modules, summary -->
                        <reports>
                            <report>index</report>
                            <report>dependencies</report>
                            <report>issue-tracking</report>
                            <report>scm</report>
                            <report>summary</report>
                        </reports>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jxr-plugin</artifactId>
                        <version>2.4</version>
                        <configuration>
                            <aggregate>true</aggregate>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <version>2.9.1</version>
                            <reports>
                                <report>javadoc</report>
                                <report>aggregate</report>
                            </reports>
                        <configuration>
                            <failOnError>false</failOnError>
                        </configuration>
                    </plugin>

                    <plugin>
                        <groupId>org.jacoco</groupId>
                        <artifactId>jacoco-maven-plugin</artifactId>
                        <version>0.6.4.201312101107</version>
                    </plugin>

                </reportPlugins>
            </configuration>
            <executions>
                <execution>
                    <id>attach-descriptor</id>
                    <goals>
                        <goal>attach-descriptor</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


希望有帮助!

izj3ouym

izj3ouym2#

我所做的是将两个执行文件(jacoco-ut.execjacoco-it.exec)合并到一个名为jacoco.exec的文件中,然后将该文件设置为插件的dataFile
首先,让我们定义这些属性:

<jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
<jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>
<jacoco.execution.data.file>${project.build.directory}/coverage-reports/jacoco.exec</jacoco.execution.data.file>

字符串
然后,沿着生成报告的配置,包括以下代码片段以创建合并文件:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-plugin.version}</version>
    <executions>
        ...
        <execution>
            <id>merge</id>
            <phase>verify</phase>
            <goals>
                <goal>merge</goal>
            </goals>
            <configuration>
                <fileSets>
                    <fileSet>
                        <directory>${project.build.directory}/coverage-reports</directory>
                        <includes>
                            <include>*.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>
                <destFile>${jacoco.execution.data.file}</destFile>
            </configuration>
        </execution>
        <execution>
            <id>report-merge</id>
            <phase>verify</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>${jacoco.execution.data.file}</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>


最后,配置reporting部分中的插件,以从合并后的文件生成报告:

<reporting>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <dataFile>${jacoco.execution.data.file}</dataFile>
            </configuration>
            <reportSets>
                <reportSet>
                    <reports>
                        <report>report</report>
                    </reports>
                </reportSet>
            </reportSets>
        </plugin>
        ...
    </plugins>
</reporting>

fnx2tebb

fnx2tebb3#

简短回答

你已经很接近解决方案了,但是

  • a section report does not exist in pom.xml,you need reporting(maybe just misspelled?)
  • reportSets也不是build -> plugins -> plugin的属性,而是reporting -> plugins -> plugin的属性。

因此,为了在生成的Maven站点中拥有JaCoCo报告,您需要指定JaCoCo Maven插件两次:
1.在build -> plugins -> plugin下启用Java代理(无需在此处添加report目标)。

  1. reporting -> plugins -> plugin指定reportSet
    最简单的代码片段是:
<!-- goes directly under tag "project" -->
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.plugin.version}</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

字符串

长应答

虽然也可以通过自定义maven-site-plugin的输出手动添加指向JaCoCo报告的链接,(例如通过site.xml),这不是最简单的方法,也不是预期的方法。JaCoCo的ReportMojo实现了MavenMultiPageReport,这允许maven-site-plugin将其检测为报告插件。然而,仅仅通过阅读文档,它的工作原理还远不明显。
当在build -> plugins下指定report目标时,maven-site-plugin不知道JaCoCo报告存在。只有在reporting -> plugins下给出时,它才被称为report而不是goal,尽管它实际上是一样的。当在这里指定时,(报告)插件将通过maven-site-plugin间接启动(而不是直接通过Maven构建)。
然而,为了使报告具有必要的信息,JaCoCo输出必须在报告之前存在,并且仅当目标prepare-agent(或集成测试的prepare-agent-integration)已添加到build -> plugins下的jacoco-maven-plugin时才会出现这种情况。😅
JaCoCo有三个报告(可以通过两种方式触发):

  • report用于单个Maven项目
  • report-aggregate用于多模块Maven项目
  • report-integration用于集成测试

所有这些都由maven-site-plugin自动检测(如果省略reportSet,也会执行):

[INFO] --- site:3.12.1:site (default-site) @ java-playground ---
[INFO] configuring report plugin org.jacoco:jacoco-maven-plugin:0.8.11
[INFO] 3 reports detected for jacoco-maven-plugin:0.8.11: report, report-aggregate, report-integration


长话短说,一个简单的Maven项目的完整pom.xml可能看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>java-playground</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <executions>
                    <execution>
                        <id>prepare</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <!-- no explicit report generation here!
                         (commented for illustration purposes)
                    <execution>
                        <id>report</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    -->
                </executions>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.11</version>
                <reportSets>
                    <reportSet>
                        <reports>
                            <report>report</report>
                        </reports>
                    </reportSet>
                </reportSets>
            </plugin>
        </plugins>
    </reporting>

    <!-- whatever you use for your tests -->
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.24.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>


结果如下:x1c 0d1x
另请参阅:

相关问题