3

我有一个带有基于 PowerMock 的单元测试的大型 Maven 多项目构建。我在子模块中生成一个“target/jacoco.exec”,在子模块的整个父 pom 中配置插件。我有一个额外的子模块,它只使用“report-aggregate”,并将各种子模块指定为依赖项。这一切基本上都有效。

我现在正在尝试将这些数据与 SonarQube 集成。似乎安装的 SonarQube Java Analyzer 版本不知道如何处理多个 jacoco.exec 文件,它只会处理一个文件。所以,我试图让“合并”目标发挥作用。我阅读了有关此目标的文档,但我显然没有正确地做某事,因为它只是没有找到数据文件。

在我使用“report-aggregate”的同一个子模块中,我执行了以下操作:

         <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>
                <execution>
                   <id>merge</id>
                   <goals>
                       <goal>merge</goal>
                   </goals>
                   <configuration>
                       <fileSets>
                           <fileSet>
                               <directory>../childmodule1/target</directory>
                               <include>*.exec</include>
                           </fileSet>
                           <fileSet>
                               <directory>../childmodule2/target</directory>
                               <include>*.exec</include>
                           </fileSet>
                           ... several more
                       </fileSets>
                   </configuration>
                </execution>
            </executions>
        </plugin>

当我从顶层运行“mvn clean install”时,它最终处理了“jacoco-aggregate”子模块,并产生了以下输出:

[INFO] ------------------------------------------------------------------------
[INFO] Building jacoco-aggregate 2.3.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ jacoco-aggregate ---
[INFO] Deleting ...\jacoco-aggregate\target
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:merge (merge) @ jacoco-aggregate ---
[INFO] Skipping JaCoCo merge execution due to missing execution data files
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (filter) @ jacoco-aggregate ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory ...\jacoco-aggregate\src\main\resources
[INFO] 
[INFO] --- depends-maven-plugin:1.2:generate-depends-file (generate-depends-file) @ jacoco-aggregate ---
[INFO] Created: ...\jacoco-aggregate\target\classes\META-INF\maven\dependencies.properties
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:instrument (default-instrument) @ jacoco-aggregate ---
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:restore-instrumented-classes (default-restore-instrumented-classes) @ jacoco-aggregate ---
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:report (default-report) @ jacoco-aggregate ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO] 
[INFO] --- maven-javadoc-plugin:2.10.4:jar (module-javadoc-jar) @ jacoco-aggregate ---
[INFO] Not executing Javadoc as the project is not a Java classpath-capable package
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.8:report-aggregate (report-aggregate) @ jacoco-aggregate ---
[INFO] Loading execution data file ...\childmodule1\target\jacoco.exec
[INFO] Loading execution data file ...\childmodule2\target\jacoco.exec
... several more
[INFO] Analyzed bundle 'childmodule1' with 1 classes
[INFO] Analyzed bundle 'childmodule2' with 1 classes

如您所见,它执行了“合并”目标,但没有找到任何数据文件,尽管后来的“报告聚合”目标找到了。

所以我认为我指定文件集或与之相关的东西的方式在某种程度上是错误的。这里可能有什么问题?

4

3 回答 3

3

尝试将<fileSets>(以及<destFile/>如果有的话)移动到<plugin><configuration>而不是<execution><configuration>

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.8</version>
    <configuration>
        <fileSets>
            <fileSet>
                <directory>..</directory>
                <include>**/target/*.exec</include>
            </fileSet>
        </fileSets>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>merge</goal>
            </goals>
        </execution>
    </executions>
</plugin>
于 2018-03-22T09:51:00.260 回答
2

我在如何指定“合并”目标时遇到了两个问题:

  • “合并”的默认阶段是“生成资源”。我无法想象这有什么用,因为我看不到在那个阶段之前如何创建这些数据文件。我将其更改为“验证”并且效果更好。

  • 在文件集中,我指定了相对于这个子模块的目录路径,而我应该指定它们相对于项目的根目录。我从这些路径中删除了“../”,这就解决了这个问题。

于 2017-02-14T16:06:51.820 回答
0

我的配置略有不同,因为我有一个报告子模块,它应该处理所有其他模块,既不是孩子也不是兄弟姐妹,而是堂兄弟。我最终得到了一个配置文件:

                <fileSets>
                    <fileSet>
                        <directory>${project.build.directory}/../../../</directory>
                        <includes>
                            <include>**/*.exec</include>
                        </includes>
                    </fileSet>
                </fileSets>

执行以下操作有助于调查配置问题:

mvn org.jacoco:jacoco-maven-plugin:merge -X

但是,正如另一个答案中所建议的,它需要将配置从执行块移动到主插件配置。

于 2020-10-03T12:52:00.257 回答