155

我已经在互联网上上下搜索了这个。那里有很多半答案,与 Maven 属性有关,例如,${sonar.jacoco.reportPath}org.jacoco:jacoco-maven-plugin:prepare-agent设置maven-surefire-plugin argLine.-javaagent

不知何故,这些答案(无论是单独的还是组合的)都没有产生我所追求的: 覆盖率报告,如果在堆栈更高的测试中使用某个类(例如正在使用的实体),则显示该类已被覆盖由 DAO,即使它自己的模块中的测试没有完全覆盖它。

请问在某处有明确的配置来实现这一点吗?

4

13 回答 13

189

我和你的情况一样,网上散落的一半答案很烦人,因为似乎很多人都有同样的问题,但没有人愿意完全解释他们是如何解决的。

Sonar 文档引用了 一个 GitHub 项目,其中包含有用的示例。我为解决这个问题所做的是将集成测试逻辑应用于常规单元测试(尽管正确的单元测试应该是特定于子模块的,但情况并非总是如此)。

在父 pom.xml 中,添加以下属性:

<properties>
    <!-- Sonar -->
    <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>
</properties>

这将使 Sonar 在同一位置(父项目中的目标文件夹)中获取所有子模块的单元测试报告。它还告诉 Sonar 重​​用手动运行的报告,而不是自己滚动。我们只需要将 jacoco-maven-plugin 放在 build/plugins 中的父 pom 中,即可为所有子模块运行:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.6.0.201210061924</version>
    <configuration>
        <destFile>${sonar.jacoco.reportPath}</destFile>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
    </executions>
</plugin>

destFile将报告文件放在 Sonar 将查找它的位置,并append使其附加到文件而不是覆盖它。这将在同一文件中合并所有子模块的所有 JaCoCo 报告。

Sonar 将为每个子模块查看该文件,因为这就是我们在上面指出的内容,为我们提供了 Sonar 中多模块文件的组合单元测试结果。

于 2013-03-20T22:24:44.443 回答
27

自 0.7.7 版以来的新方式

从 0.7.7 版开始,有一种创建汇总报告的新方法:

您创建一个单独的“报告”项目,该项目收集所有必要的报告(聚合器项目中的任何目标都在其模块之前执行,因此无法使用)。

aggregator pom
  |- parent pom
  |- module a
  |- module b
  |- report module 

pom看起来是这样的(不要忘记在 modules 下添加新的报告模块):

<build>
<plugins>
  <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.8</version>
    <executions>
      <execution>
        <id>prepare-agent</id>
        <goals>
          <goal>prepare-agent</goal>
        </goals>
      </execution>
    </executions>
  </plugin>
</plugins>

每个子模块的 pom 根本不需要更改。报告模块中的 pom如下所示:

<!-- Add all sub modules as dependencies here -->
<dependencies>
  <dependency>
    <module a>
  </dependency>
  <dependency>
    <module b>
  </dependency>
 ...

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.8</version>
        <executions>
          <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
              <goal>report-aggregate</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

一个完整的例子可以在这里找到。

于 2017-01-13T15:03:54.657 回答
25

常问问题

自从那次我对 jacoco 发疯以来,我脑海中浮现的问题。

我的应用程序服务器(jBoss、Glassfish..)位于伊拉克、叙利亚等等。在其上运行集成测试时是否可以获得多模块覆盖?Jenkins 和 Sonar 也在不同的服务器上。

是的。您必须使用在 mode 下运行的jacoco 代理output=tcpserver,jacoco ant lib。基本上是两个jars。这会给你99%的成功。

jacoco 代理是如何工作的?

你追加一个字符串

-javaagent:[your_path]/jacocoagent.jar=destfile=/jacoco.exec,output=tcpserver,address=*

到您的应用程序服务器 JAVA_OPTS 并重新启动它。在此字符串中,只需[your_path]将其替换为 jacocoagent.jar 的路径,并将其存储(存储!)在运行应用程序服务器的 VM 上。自从您启动应用服务器后,所有部署的应用程序都将被动态监控,并且它们的活动(即代码使用)将准备好通过 tcl 请求以 jacocos .exec 格式获取。

我可以重置 jacoco 代理以仅从我的测试开始时开始收集执行数据吗?

是的,为此,您需要位于 jenkins 工作区中的 jacocoant.jar 和 ant 构建脚本。

所以基本上我从 http://www.eclemma.org/jacoco/ 需要的是 jacocoant.jar 位于我的 jenkins 工作区中,而 jacocoagent.jar 位于我的应用服务器 VM 上?

这是正确的。

我不想用ant,我听说jacoco maven插件也可以做所有的事情。

不对,jacoco maven 插件可以收集单元测试数据和一些集成测试数据(请参阅Arquillian Jacoco),但是如果您有例如放心的测试作为 jenkins 中的单独构建,并且想要显示多模块覆盖率,我可以'看不到 maven 插件如何帮助你。

jacoco 代理究竟产生了什么?

只有覆盖数据.exec格式。然后声纳可以读取它。

jacoco 是否需要知道我的 java 类所在的位置?

不,声纳可以,但 jacoco 不行。当您mvn sonar:sonar进行课程路径时,就会发挥作用。

那么蚂蚁脚本呢?

它必须呈现在您的詹金斯工作区中。我的蚂蚁脚本,我称之为jacoco.xml

<project name="Jacoco library to collect code coverage remotely" xmlns:jacoco="antlib:org.jacoco.ant">
    <property name="jacoco.port" value="6300"/>
    <property name="jacocoReportFile" location="${workspace}/it-jacoco.exec"/>

    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="${workspace}/tools/jacoco/jacocoant.jar"/>
    </taskdef>

    <target name="jacocoReport">
            <jacoco:dump address="${jacoco.host}" port="${jacoco.port}" dump="true" reset="true" destfile="${jacocoReportFile}" append="false"/>
    </target>

    <target name="jacocoReset">
            <jacoco:dump address="${jacoco.host}" port="${jacoco.port}" reset="true" destfile="${jacocoReportFile}" append="false"/>
        <delete file="${jacocoReportFile}"/>
    </target>
</project>

调用此脚本时应传递的两个强制性参数 -Dworkspace=$WORKSPACE 使用它来指向您的詹金斯工作区和-Djacoco.host=yourappserver.com主机http://

还要注意我把我jacocoant.jar放到 ${workspace}/tools/jacoco/jacocoant.jar

接下来我该怎么办?

您是否使用 jacocoagent.jar 启动了您的应用服务器?

您是否将 ant 脚本和 jacocoant.jar 放入您的 jenkins 工作区?

如果是,最后一步是配置 jenkins 构建。这是策略:

  1. 调用 ant 目标jacocoReset以重置所有以前收集的数据。
  2. 运行你的测试
  3. 调用 ant 目标jacocoReport获取报告

如果一切正常,您将it-jacoco.exec在构建工作区中看到。

看截图,我也ant安装在我的工作区的$WORKSPACE/tools/antdir 中,但你可以使用安装在你的 jenkins 中的一个。

在此处输入图像描述

如何在声纳中推送此报告?

Mavensonar:sonar将完成这项工作(不要忘记配置它),将其指向 main pom.xml,以便它将运行所有模块。使用sonar.jacoco.itReportPath=$WORKSPACE/it-jacoco.exec参数告诉声纳您的集成测试报告所在的位置。每次它分析新的模块类时,都会在it-jacoco.exec.

我的 `target` 目录中已经有 jacoco.exec,`mvn sonar:sonar` 忽略/删除它

默认情况下mvn sonar:sonarclean删除您的目标目录,使用sonar.dynamicAnalysis=reuseReports它来避免它。

于 2014-05-23T13:42:00.023 回答
16

我为新的 Sonar 版本找到了另一个解决方案,其中 JaCoCo 的二进制报告格式 (*.exec) 已被弃用,首选格式是 XML(SonarJava 5.12 及更高版本)。该解决方案非常简单,类似于以前的解决方案,在此主题的父目录中报告 *.exec 报告:https ://stackoverflow.com/a/15535970/4448263 。

假设我们的项目结构是:

moduleC - aggregate project's pom
  |- moduleA - some classes without tests
  |- moduleB - some classes depending from moduleA and tests for classes in both modules: moduleA and moduleB

您需要在聚合项目的 pom 中进行以下 maven 构建插件配置:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <executions>
        <execution>
            <id>prepare-and-report</id>
            <goals>
                <goal>prepare-agent</goal>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>report-aggregate</id>
            <phase>verify</phase>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/../target/site/jacoco-aggregate</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

然后用maven构建项目:

mvn clean verify

对于声纳,您应该在管理 GUI 中设置属性:

sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml,../target/site/jacoco-aggregate/jacoco.xml

或使用命令行:

mvn sonar:sonar -Dsonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml,../target/site/jacoco-aggregate/jacoco.xml

描述

这将为默认目录中的每个模块创建二进制报告:target/jacoco.exec. 然后为默认目录中的每个模块创建 XML 报告:target/site/jacoco/jacoco.xml. 然后为自定义目录中的每个模块创建一个聚合报告,该目录${project.basedir}/../target/site/jacoco-aggregate/相对于每个模块的父目录。对于 moduleA 和 moduleB 这将是 common path moduleC/target/site/jacoco-aggregate/

由于 moduleB 依赖于 moduleA,moduleB 将最后构建,其报告将用作 Sonar 中模块 A 和 B 的聚合覆盖率报告。

除了聚合报告之外,我们还需要一个普通的模块报告,因为 JaCoCo 聚合报告仅包含依赖项的覆盖率数据。

这两种类型的报告共同为 Sonar 提供了全覆盖数据。

有一点限制:您应该能够在项目的父目录中编写报告(应该有权限)。或者,您可以jacoco.skip=true在根项目的 pom.xml (moduleC) 和jacoco.skip=false带有类和测试的模块(moduleA 和 moduleB)中设置属性。

于 2020-03-11T15:17:01.123 回答
12

I'll post my solution as it it subtly different from others and also took me a solid day to get right, with the assistance of the existing answers.

For a multi-module Maven project:

ROOT
|--WAR
|--LIB-1
|--LIB-2
|--TEST

Where the WAR project is the main web app, LIB 1 and 2 are additional modules the WAR depends on and TEST is where the integration tests live. TEST spins up an embedded Tomcat instance (not via Tomcat plugin) and runs WAR project and tests them via a set of JUnit tests. The WAR and LIB projects both have their own unit tests.

The result of all this is the integration and unit test coverage being separated and able to be distinguished in SonarQube.

ROOT pom.xml

<!-- Sonar properties-->
<sonar.jacoco.itReportPath>${project.basedir}/../target/jacoco-it.exec</sonar.jacoco.itReportPath>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
<sonar.language>java</sonar.language>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>

<!-- build/plugins (not build/pluginManagement/plugins!) -->
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.6.201602180812</version>
    <executions>
        <execution>
            <id>agent-for-ut</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.reportPath}</destFile>
            </configuration>
        </execution>
        <execution>
            <id>agent-for-it</id>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <append>true</append>
                <destFile>${sonar.jacoco.itReportPath}</destFile>
            </configuration>
        </execution>
    </executions>
</plugin>

WAR, LIB and TEST pom.xml will inherit the the JaCoCo plugins execution.

TEST pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <skipTests>${skip.tests}</skipTests>
                <argLine>${argLine} -Duser.timezone=UTC -Xms256m -Xmx256m</argLine>
                <includes>
                    <includes>**/*Test*</includes>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

I also found Petri Kainulainens blog post 'Creating Code Coverage Reports for Unit and Integration Tests With the JaCoCo Maven Plugin' to be valuable for the JaCoCo setup side of things.

于 2016-06-17T00:04:39.893 回答
9

有一种方法可以做到这一点。神奇的是创建一个组合的 jacoco.exec 文件。使用 maven 3.3.1 有一个简单的方法来获得它。这是我的个人资料:

<profile>
    <id>runSonar</id>
    <activation>
        <property>
            <name>runSonar</name>
            <value>true</value>
        </property>
    </activation>
    <properties>
        <sonar.language>java</sonar.language>
        <sonar.host.url>http://sonar.url</sonar.host.url>
        <sonar.login>tokenX</sonar.login>
        <sonar.jacoco.reportMissing.force.zero>true</sonar.jacoco.reportMissing.force.zero>
        <sonar.jacoco.reportPath>${jacoco.destFile}</sonar.jacoco.reportPath>
        <jacoco.destFile>${maven.multiModuleProjectDirectory}/target/jacoco_analysis/jacoco.exec</jacoco.destFile>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <append>true</append>
                            <destFile>${jacoco.destFile}</destFile>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.sonarsource.scanner.maven</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <version>3.2</version>
                </plugin>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.7.8</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</profile>

如果您将此配置文件添加到您的父 pom 并打电话给您,mvn clean install sonar:sonar -DrunSonar您将获得完整的报道。

这里的魔力是maven.multiModuleProjectDirectory。此文件夹始终是您开始构建 maven 的文件夹。

于 2017-02-02T16:33:54.150 回答
6

我在父级 pom 中使用的配置,其中我有单独的单元和集成测试阶段。

我在父 POM 属性中配置以下属性

    <maven.surefire.report.plugin>2.19.1</maven.surefire.report.plugin>
    <jacoco.plugin.version>0.7.6.201602180812</jacoco.plugin.version>
    <jacoco.check.lineRatio>0.52</jacoco.check.lineRatio>
    <jacoco.check.branchRatio>0.40</jacoco.check.branchRatio>
    <jacoco.check.complexityMax>15</jacoco.check.complexityMax>
    <jacoco.skip>false</jacoco.skip>
    <jacoco.excludePattern/>
    <jacoco.destfile>${project.basedir}/../target/coverage-reports/jacoco.exec</jacoco.destfile>

    <sonar.language>java</sonar.language>
    <sonar.exclusions>**/generated-sources/**/*</sonar.exclusions>
    <sonar.core.codeCoveragePlugin>jacoco</sonar.core.codeCoveragePlugin>
    <sonar.coverage.exclusions>${jacoco.excludePattern}</sonar.coverage.exclusions>
    <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
    <sonar.jacoco.reportPath>${project.basedir}/../target/coverage-reports</sonar.jacoco.reportPath>

    <skip.surefire.tests>${skipTests}</skip.surefire.tests>
    <skip.failsafe.tests>${skipTests}</skip.failsafe.tests>

我将插件定义放在插件管理下。

请注意,我为surefire (surefireArgLine) 和failsafe (failsafeArgLine) 参数定义了一个属性,以允许jacoco 配置javaagent 以在每个测试中运行。

在插件管理下

  <build>
     <pluginManagment>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <fork>true</fork>
                    <meminitial>1024m</meminitial>
                    <maxmem>1024m</maxmem>
                    <compilerArgument>-g</compilerArgument>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <forkCount>4</forkCount>
                    <reuseForks>false</reuseForks>
                    <argLine>-Xmx2048m ${surefireArgLine}</argLine>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                    <excludes>
                        <exclude>**/*IT.java</exclude>
                    </excludes>
                    <skip>${skip.surefire.tests}</skip>
                </configuration>
            </plugin>
            <plugin>
                <!-- For integration test separation -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19.1</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <forkCount>4</forkCount>
                    <reuseForks>false</reuseForks>
                    <argLine>${failsafeArgLine}</argLine>
                    <includes>
                        <include>**/*IT.java</include>
                    </includes>
                    <skip>${skip.failsafe.tests}</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <!-- Code Coverage -->
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.plugin.version}</version>
                <configuration>
                    <haltOnFailure>true</haltOnFailure>
                    <excludes>
                        <exclude>**/*.mar</exclude>
                        <exclude>${jacoco.excludePattern}</exclude>
                    </excludes>
                    <rules>
                        <rule>
                            <element>BUNDLE</element>
                            <limits>
                                <limit>
                                    <counter>LINE</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>${jacoco.check.lineRatio}</minimum>
                                </limit>
                                <limit>
                                    <counter>BRANCH</counter>
                                    <value>COVEREDRATIO</value>
                                    <minimum>${jacoco.check.branchRatio}</minimum>
                                </limit>
                            </limits>
                        </rule>
                        <rule>
                            <element>METHOD</element>
                            <limits>
                                <limit>
                                    <counter>COMPLEXITY</counter>
                                    <value>TOTALCOUNT</value>
                                    <maximum>${jacoco.check.complexityMax}</maximum>
                                </limit>
                            </limits>
                        </rule>
                    </rules>
                </configuration>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${jacoco.destfile}</destFile>
                            <append>true</append>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${jacoco.destfile}</dataFile>
                            <outputDirectory>${sonar.jacoco.reportPath}</outputDirectory>
                            <skip>${skip.surefire.tests}</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <destFile>${jacoco.destfile}</destFile>
                            <append>true</append>
                            <propertyName>failsafeArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                        <configuration>
                            <dataFile>${jacoco.destfile}</dataFile>
                            <outputDirectory>${sonar.jacoco.reportPath}</outputDirectory>
                            <skip>${skip.failsafe.tests}</skip>
                        </configuration>
                    </execution>
                    <!-- Disabled until such time as code quality stops this tripping
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <dataFile>${jacoco.destfile}</dataFile>
                        </configuration>
                    </execution>
                    -->
                </executions>
            </plugin>
            ...

在构建部分

 <build>
     <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>

        <plugin>
            <!-- for unit test execution -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
        <plugin>
            <!-- For integration test separation -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>
        <plugin>
            <!-- For code coverage -->
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
        </plugin>
        ....

在报告部分

    <reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>${maven.surefire.report.plugin}</version>
            <configuration>
                <showSuccess>false</showSuccess>
                <alwaysGenerateFailsafeReport>true</alwaysGenerateFailsafeReport>
                <alwaysGenerateSurefireReport>true</alwaysGenerateSurefireReport>
                <aggregate>true</aggregate>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.plugin.version}</version>
            <configuration>
                <excludes>
                    <exclude>**/*.mar</exclude>
                    <exclude>${jacoco.excludePattern}</exclude>
                </excludes>
            </configuration>
        </plugin>
     </plugins>
  </reporting>
于 2016-05-03T06:42:46.250 回答
3

作为 Sonars sonar.jacoco.reportPathsonar.jacoco.itReportPath并且sonar.jacoco.reportPaths都已被弃用,您现在应该使用sonar.coverage.jacoco.xmlReportPaths。如果您想使用 Sonar 和 Jacoco 配置多模块 maven 项目,这也会产生一些影响。

正如@Lonzak指出的那样,从Sonar 0.7.7 开始,您可以使用Sonars 报告聚合目标。只需将以下依赖项放入您的父 pom 中:

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.5</version>
    <executions>
        <execution>
            <id>report</id>
            <goals>
                <goal>report-aggregate</goal>
            </goals>
            <phase>verify</phase>
        </execution>
    </executions>
</plugin>

由于当前版本的 jacoco-maven-plugin 与 xml-reports 兼容,这将为它自己的目标文件夹中的每个模块创建一个包含文件的 site/jacoco-aggregate 文件夹jacoco.xml

要让 Sonar 组合所有模块,请使用以下命令:

mvn -Dsonar.coverage.jacoco.xmlReportPaths=full-path-to-module1/target/site/jacoco-aggregate/jacoco.xml,module2...,module3... clean verify sonar:sonar

为了使我的回答简短而准确,我没有提到maven-surefire-plugin maven-failsafe-plugin依赖项。您可以在没有任何其他配置的情况下添加它们:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.2</version>
    <executions>
        <execution>
        <id>integration-test</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2019-11-28T07:13:35.430 回答
2
    <sonar.language>java</sonar.language>
    <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.jacoco.reportPath>${user.dir}/target/jacoco.exec</sonar.jacoco.reportPath>
    <sonar.jacoco.itReportPath>${user.dir}/target/jacoco-it.exec</sonar.jacoco.itReportPath>
    <sonar.exclusions>
        file:**/target/generated-sources/**,
        file:**/target/generated-test-sources/**,
        file:**/target/test-classes/**,
        file:**/model/*.java,
        file:**/*Config.java,
        file:**/*App.java
    </sonar.exclusions>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.reportPath}</destFile>
                            <append>true</append>
                            <propertyName>surefire.argLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <destFile>${sonar.jacoco.itReportPath}</destFile>
                            <append>true</append>
                            <propertyName>failsafe.argLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report-integration</id>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>             
于 2018-03-28T10:53:55.007 回答
1

要进行单元测试和集成测试,您可以使用 maven-surefire-plugin 和 maven-failsafe-plugin 并限制包含/排除。我在与 sonar/jacoco 联系时正在玩 CDI,所以我最终参与了这个项目:

https://github.com/FibreFoX/cdi-sessionscoped-login/

也许它对你有一点帮助。在我的 pom.xml 中,我通过在指定测试插件的配置部分中设置 argLine 选项来隐式使用“-javaagent”。在 MAVEN 项目中明确使用 ANT 是我不会尝试的,对我来说它混合了两个世界。

我只有一个单模块 maven 项目,但也许它可以帮助你调整你的工作。

注意:也许不是所有的 maven-plugins 都是最新的,也许一些问题在以后的版本中得到了修复

于 2013-02-20T12:51:49.850 回答
1

您可以在 maven 上调用一个名为merge的 ant 任务,将所有覆盖文件 (*.exec) 放在同一个文件中。

如果您正在运行单元测试,请使用阶段prepare-package,如果您运行集成测试,请使用post-integration-test

这个站点有一个如何在 maven 项目中调用 jacoco ant 任务的示例

您可以在声纳上使用此合并文件。

于 2012-10-23T19:39:45.510 回答
1

这个示例对我来说效果很好:

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.2</version>
            <executions>
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>
                <execution>
                    <id>pre-integration-test</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                    <configuration>
                        <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                        <!--<excludes>
                            <exclude>com.asimio.demo.rest</exclude>
                            <exclude>com.asimio.demo.service</exclude>
                        </excludes>-->
                        <propertyName>testArgLine</propertyName>
                    </configuration>
                </execution>
                <execution>
                    <id>post-integration-test</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>merge-results</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>${project.build.directory}/coverage-reports/aggregate.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>post-merge-report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/coverage-reports/aggregate.exec</dataFile>
                        <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <argLine>${surefireArgLine}</argLine>
                <!--<skipTests>${skip.unit.tests}</skipTests>-->
                <includes>
                    <include>**/*Test.java</include>
                    <!--<include>**/*MT.java</include>
                    <include>**/*Test.java</include>-->
                </includes>
            <!--    <skipTests>${skipUTMTs}</skipTests>-->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12.4</version>
            <configuration>
                <!--<skipTests>${skipTests}</skipTests>
                <skipITs>${skipITs}</skipITs>-->
                <argLine>${testArgLine}</argLine>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
                <!--<excludes>
                    <exclude>**/*UT*.java</exclude>
                </excludes>-->
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
于 2019-10-04T13:02:26.830 回答
0

Jacoco Wiki中所述,您还可以生成一个新的报告模块:

策略:具有依赖关系的模块:聚合器项目的问题可以通过额外的“报告”模块来解决。在多模块 Maven 项目中,定义了一个单独的模块,该模块不提供实际内容,但会创建一个组合的覆盖率报告。它定义了对应该包含在组合报告中的所有模块的依赖关系。“报告”模块将在其依赖项之后构建,并且可以访问它所依赖的项目中的 exec 文件以及类和源文件。这种策略似乎最适合当前的 Maven 架构。从用户的角度来看,有人可能会争辩说这样一个单独的模块会使构建定义膨胀。或者,单独的模块不能有任何可以使用 exec 或 class 文件的子模块。然而,

如果您的模块化比具有一些子模块的父级更复杂,这将特别有用。

您的报告 pom 可能类似于:

 <dependencies>
    <dependency>
        <groupId>org.sonarqube</groupId>
        <artifactId>module1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.sonarqube</groupId>
        <artifactId>module2</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>report</id>
                    <goals>
                        <goal>report-aggregate</goal>
                    </goals>
                    <phase>verify</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
于 2021-03-25T17:37:07.117 回答