External Rules

This rule will provide rules configuration from an external resource.

It can be a classpath resource present in a maven-enforcer-plugin dependency or a local file.

Sample Plugin Configuration:

<project>
    [...]
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.5.0</version>
                <dependencies>
                    <!-- Dependency containing the enforcer/rules.xml file -->
                    <dependency>
                        <groupId>org.foo</groupId>
                        <artifactId>foobar-rules</artifactId>
                        <version>1.0.0</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>enforce</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <externalRules>
                                    <!-- enforcer/rules.xml is supposed to be a classpath resource present -->
                                    <!-- in org.foo:foobar-rules dependency of maven-enforcer-plugin defined above -->
                                    <location>classpath:enforcer/rules.xml</location>
                                </externalRules>
                                <externalRules>
                                    <!-- You can add multiple <ExternalRules> elements if you need to enforce -->
                                    <!-- rules from multiple resources. -->
                                    <!-- src/build/rules.xml is a local file path relative to ${project.basedir} -->
                                    <location>src/build/rules.xml</location>
                                </externalRules>
                            </rules>
                            <fail>true</fail>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    [...]
</project>

The External Enforcer Rule Descriptor

Here is a sample rules file. The rules element expects the same content as in the Enforcer Mojo rules configuration:

<enforcer>
    <rules>
        <dependencyConvergence/>
        <bannedDependencies>
            <excludes>
                <exclude>org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec</exclude>
            </excludes>
            <includes>
                <include>jakarta.xml.bind:jakarta.xml.bind-api:*:*:test</include>
            </includes>
        </bannedDependencies>
    </rules>
</enforcer>