当子目录中存在某些模块时,多模块项目的 Jacoco maven 配置

Jacoco maven configuration for multi module project when some modules are present in sub directory

我的项目包含以下结构

pom.xml
|
|
-----sub module A pom.xml 
|
|
-----sub module B pom.xml
|
|
-----sub module plugins/C pom.xml
|
| 
-----sub module plugins/D pom.xml

我已经像这样配置了 Main pom

<modules>
    <module>A</module>
    <module>B</module>
    <module>plugins/C</module>
    <module>plugins/D</module>
</modules>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.7.201606060606</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
    </executions>
</plugin>

当我 运行 "mvn verify" jacoco 报告仅针对模块 A 和 B 生成。对于模块 plugin/C 和 plugin/D Jacoco 未执行,因此没有报告.

为您的插件文件夹添加另一个 pom.xml,让它从您的父 pom 继承并将 C & D 定义为那里的子模块,然后从您的父 pom 中删除模块并添加插件文件夹。并且不要忘记将父 pom 从 C & D 更改为插件 pom,应该这样做。

这个问题不是关于 jacoco-maven-plugin,而是关于 Maven 中的 aggregation and inheritance

鉴于example/pom.xml

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>A</module>
    <module>plugins/C</module>
  </modules>

  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.7.201606060606</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

指定 example/Aexample/plugins/C 应该作为 example 构建的一部分构建,但没有指定任何关于插件配置继承、属性等的内容。

对于 example/plugins/C/pom.xmlexample/pom.xml 的继承,第一个应该将第二个声明为 parent:

<project>
  <modelVersion>4.0.0</modelVersion>

  <parent>
    <groupId>org.example</groupId>
    <artifactId>example</artifactId>
    <version>0.1-SNAPSHOT</version>
    <relativePath>../..</relativePath>
  </parent>

  <groupId>org.example</groupId>
  <artifactId>C</artifactId>
  <version>0.1-SNAPSHOT</version>
</project>

在这种情况下,插件的配置将被继承并执行:

Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T21:39:06+02:00)
...
[INFO] ------------------------------------------------------------------------
[INFO] Building C 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:prepare-agent (default) @ C ---
...

P.S。 JaCoCo 的最新版本是 0.7.9。