反应堆模块收敛

此规则检查反应器内的版本是否一致,此外它将检查项目中的每个模块是否包含父级,并且父级是否是反应器构建的一部分。此外,将检查依赖项是否是模块间依赖项,它们使用与反应器给出的相同版本。

此规则支持以下参数:

  • message - 如果规则失败,则向用户发送可选的补充消息。
  • ignoreModuleDependencies - 忽略引用反应器内模块的模块依赖项(默认值:false)。

    注意:当前状态无法正确处理mvn -pl subproject validate之类的情况。这将在下一个主要版本 (2.X) 的强制执行器中得到正确处理。

示例插件配置:

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <id>enforce-no-snapshots</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <reactorModuleConvergence>
                  <message>The reactor is not valid</message>
                  <ignoreModuleDependencies>true</ignoreModuleDependencies>
                </reactorModuleConvergence>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

多模块构建中有不同的情况可能导致问题(例如不使用 maven-release-plugin 等)。该规则旨在防止此类问题。

让我们假设我们有以下(简单)项目结构用于多模块设置。

   root (pom.xml)
     +--- module1 (pom.xml)
     +--- module2 (pom.xml)

pom.xml如下所示:

  <groupId>com.mycompany.project</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  (..)

Maven 中的最佳实践是所有子级都从其父级继承版本,并且不定义如下所示的新版本:

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>module1</artifactId>
  (..)

但有时人们会犯错或违反最佳实践,如下所示:

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  
  <artifactId>module1</artifactId>
  <version>1.1-SNAPSHOT</version>

通过使用此规则,您将在构建期间收到一条消息,其中包含以下结果输出:

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
The reactor contains different versions.
 --> com.mycompany.project:myproject:pom:1.1-SNAPSHOT

接下来发生的情况是反应器中的父级有时是错误的,例如以下情况:

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <artifactId>module1</artifactId>
  <version>1.0-SNAPSHOT</version>
  (..)

这将由以下消息提示:

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
Reactor modules have parents which contain a wrong version.
 --> com.mycompany.project:myproject:pom:1.1-SNAPSHOT parent:com.mycompany.project:myproject:pom:1.0-SNAPSHOT

如果您只是偶然使用错误的版本更改了父母,如下所示:

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.1-SNAPSHOT</version>
  </parent>

  <artifactId>module1</artifactId>
  (..)

您将收到与上述相同的消息:

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
The reactor contains different versions.
 --> com.mycompany.project:myproject:pom:1.1-SNAPSHOT

发生的其他事情只是父母会被遗忘,这会产生如下消息:

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
Reactor contains modules without parents.
 module: com.mycompany.project:myproject:pom:1.2-SNAPSHOT

在较大的多模块构建中,也发生了定义的父级,但不属于这样的反应器:

  <parent>
    <groupId>org.apache.enforcer</groupId>
    <artifactId>something-different</artifactId>
    <version>1.1</version>
  </parent>

  <artifactId>module1</artifactId>
  (..)

通常已经有这样的Maven警告应该注意了:

[WARNING] 
[WARNING] Some problems were encountered while building the effective model for org.apache.enforcer:pom:1.0.4-SNAPSHOT
[WARNING] 'parent.relativePath' points at org.apache.enforcer:something-different instead of org.apache.enforcer:something-different, please verify your project structure @ line 7, column 11
[WARNING] 

但这会经常疏忽。因此,执行者规则将通过消息简单地破坏此类邮件形成的构建(这要求父级具有与发生的其余构建相同的版本):

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
Module parents have been found which could not be found in the reactor.
 module: org.apache.enforcer:something-different:pom:1.0.4-SNAPSHOT

发生的另一种情况(例如,通过从分支合并到主干/主控)是模块间依赖项包含错误的版本,如下所示:

  <parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.2-SNAPSHOT</version>
  </parent>

  <artifactId>module1</artifactId>
  
  <dependencies>
    <dependency>
      <groupId>com.mycompany.project</groupId>
      <artifactId>myproject</artifactId>
      <version>1.1</version>
    </dependency>
  (..)
  </dependencies>
  (..)

这将导致以下消息:

[WARNING] Rule 0: org.apache.maven.plugins.enforcer.ReactorModuleConvergence failed with message:
Reactor modules contains dependencies which do not reference the reactor.
 module: com.mycompany.project:myproject-x:jar:1.2-SNAPSHOT
    dependency: com.mycompany.project:myproject:1.1