tuk
tuk

Reputation: 6872

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

Upvotes: 5

Views: 3770

Answers (3)

niklas
niklas

Reputation: 919

You can disable a single rule using the property enforcer.skipRules. Either as a cmdline property or a property in a child module.

You can for example put the following property in a child pom to disable the rule banTransitiveDependencies, assuming it was configured in a parent pom

<properties>
    <enforcer.skipRules>banTransitiveDependencies</enforcer.skipRules>
</properties>

See https://maven.apache.org/enforcer/maven-enforcer-plugin/enforce-mojo.html#rulestoskip

Upvotes: 1

tuk
tuk

Reputation: 6872

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

Upvotes: 1

J Fabian Meier
J Fabian Meier

Reputation: 35853

AFAIK you cannot disable a single enforcer rule.

You can set enforcer.skip to true -- this disables all enforcer rules.

What I have done in a similar situation:

I have defined my own enforcer rule which inherited from an "official" enforcer rule. This enforcer rule contained a switch to disable it.

Upvotes: 1

Related Questions