Search code examples
javamavenjava-8maven-3maven-enforcer-plugin

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


I am using a maven-enforcer plugin in a multi-module maven project. Let's say my project structure is like below

main
  - query
  - storage

My enforcer plugin in main pom looks like below

<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>

In a child module (query) if I need to disable one of the enforcer rules (let's say DependencyConvergence) can someone let me know how can this be done?

Maven Version - 3.6.1


Solution

  • This is also answered in the 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