如何在多模块 Maven 项目的子模块中禁用某些强制规则?

How to disable certain enforcer rules in a child module in a multi-module maven project?

我在多模块 maven 项目中使用 maven-enforcer 插件。假设我的项目结构如下

main
  - query
  - storage

我在 main pom 中的 enforcer 插件如下所示

<build>
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0-M2</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <DependencyConvergence/>
                                <requireJavaVersion>
                                    <version>[1.8,)</version>
                                    <message>*** This project requires JDK 1.8/J2SE 8 or later. ***</message>
                                </requireJavaVersion>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
</builds>

在子模块 (query) 中,如果我需要禁用其中一个执行器规则(比方说 DependencyConvergence),有人可以告诉我如何做到这一点吗?

Maven 版本 - 3.6.1

据我所知,您不能禁用单个强制执行者规则。

您可以将 enforcer.skip 设置为 true -- 这将禁用 所有 实施规则。

我在类似情况下所做的:

我定义了我自己的强制规则,它继承自“官方”强制规则。此执行器规则包含一个禁用它的开关。

maven mailing list中也有回答。

So something like the following if all configuration is managed via a pluginMangement section;

<build><pluginManagement><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <DependencyConvergence/>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
      <execution>
        <id>bravo</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>alpha</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
</plugins></builds>

query/pom.xml

<build><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
      </execution>
      <execution>
        <id>bravo</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
    </executions>
  </plugin>
</plugins></builds>

you might also be able to do it via a property and in query define the bravo execution instead of the alpha. I've used a similar technique with maven-surefire-plugin, where i define the plugin version using a property and have a default in the root/parent pom, and in one specific child pom i define a different surefire version. so this might work...

<build><pluginManagement><plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>3.0.0-M2</version>
    <executions>
      <execution>
        <id>alpha</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <DependencyConvergence/>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
      <execution>
        <id>bravo</id>
        <phase></phase>
        <goals>
          <goal>enforce</goal>
        </goals>
        <configuration>
          <rules>
            <requireJavaVersion>
              <version>[1.8,)</version>
              <message>*** This project requires
JDK 1.8/J2SE 8 or later. ***</message>
            </requireJavaVersion>
          </rules>
          <fail>true</fail>
        </configuration>
      </execution>
    </executions>
  </plugin>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
      <execution>
        <id>${which-enforcer-id}</id>
        <phase>validate</phase>
      </execution>
    </executions>
  </plugin>
</plugins></builds>
<properties>
  <which-enforcer-id>alpha</which-enforcer-id>
</properties>

query/pom.xml

<properties>
  <which-enforcer-id>bravo</which-enforcer-id>
</properties>

John