Maven如何在反应器中排序模块

How does Maven order modules in the reactor

像这样的问题已经被问过一遍又一遍,但不知何故他们只关注依赖关系。所以根据 maven documentation 构建顺序确定如下。

  • a project dependency on another module in the build
  • a plugin declaration where the plugin is another modules in the build
  • a plugin dependency on another module in the build
  • a build extension declaration on another module in the build
  • the order declared in the <modules> element (if no other rule applies)

第一条规则是 quit clear 例如如果模块 A 依赖于模块 B,后者首先构建。

第五条规则(最后)也很清楚。如果前三个规则不适用,我们查看模块部分中的顺序。

另外两条规则我不是很清楚。英语不是我的母语,但我想知道第二条规则是否包含某种拼写错误。

我正在寻找一个简单的例子来详细解释这两个规则。

Let's go through them一一

  • a project dependency on another module in the build

这意味着如果模块 A 依赖于模块 B,那么 B 必须在 A 之前构建。这处理了这样的情况,在 A 的 POM 中,您将拥有:

<dependencies>
  <dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>B</artifactId>
    <version>${project.version}</version>
  </dependency>
</dependencies>
  • a plugin declaration where the plugin is another modules in the build

这意味着如果模块 A 使用 a Maven plugin 是模块 B,则 B 必须在 A 之前构建。这处理了这样的情况,在 A 的 POM 中,您将拥有:

<build>
  <plugins>
    <plugin>
      <groupId>${project.groupId}</groupId>
      <artifactId>B</artifactId>
      <version>${project.version}</version>
    </plugin>
  </plugins>
</build>
  • a plugin dependency on another module in the build

这意味着如果模块 A 在模块 B 上使用 Maven 插件 that has a dependency,则 B 必须在 A 之前构建。这处理了这样的情况,在 A 的 POM 中,你有:

<build>
  <plugins>
    <plugin>
      <groupId>some.plugin.groupId</groupId>
      <artifactId>some.plugin.artifactId</artifactId>
      <version>some.version</version>
      <dependencies>
        <dependency>
          <groupId>${project.groupId}</groupId>
          <artifactId>B</artifactId>
          <version>${project.version}</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

注意这个规则是在最后一个之后应用的,所以即使插件本身也是构建的模块,它也会在之前构建,确保解析依赖关系是安全的。

  • a build extension declaration on another module in the build

这意味着如果模块 A 声明使用 as extention 模块 B,则 B 必须在 A 之前构建。这处理了这样的情况,在 A 的 POM 中,您将拥有:

<build>
  <extensions>
    <extension>
      <groupId>${project.groupId}</groupId>
      <artifactId>B</artifactId>
      <version>${project.version}</version>
    </extension>
  </extensions>
</build>
  • the order declared in the <modules> element (if no other rule applies)

当应用前面规则的 none 时,顺序是 <modules> 的顺序,在聚合器项目的 POM 中看起来像:

<modules>
  <module>A</module>
  <module>B</module>
</modules>

如果应用 none 之前的规则,A 将在 B 之前构建。