Search code examples
mavencommand-linemaven-3maven-pluginmaven-enforcer-plugin

Maven Enforcer Plugin: Specify rules via command line


I want to execute the Maven Enforcer plugin via the command line.

I've tried:

mvn enforcer:enforce -Drules=[requireReleaseDeps]
mvn enforcer:enforce -Drules=requireReleaseDeps

I am always getting this error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (default-cli) on project lkww-util-app-wurm-admin-rs-api: The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce are missing or invalid -> [He
lp 1]

How do I have to specify the rules parameter?


Solution

  • The enforcer plugin does not allow rules to be chosen/engaged via command line parameters.

    There is an open issue against the plugin for this so you could vote for that.

    In the meantime, if your choice of rules can be categorised into a small number of choices then you could perhaps create profiles and associate rules with profiles thereby allowing a build to be run for a selected subset of rules by specifying a profile. In the example below there are two profiles, each of which has a different enforcer rule:

    <profiles>
        <profile>
            <id>EnforceBannedPlugins</id>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <id>enforce-banned-plugins</id>
                            <goals>
                                <goal>enforce</goal>
                            </goals>
                            <configuration>
                                <rules>
                                    <bannedPlugins>
                                        ...
                                    </bannedPlugins>
                                </rules>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </profile>
        <profile>
            <id>EnforceMavenVersion</id>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-enforcer-plugin</artifactId>
                    <version>3.0.0</version>
                    <executions>
                        <execution>
                            <id>enforce-maven-version</id>
                            <goals>
                                <goal>enforce</goal>
                            </goals>
                            <configuration>
                                <rules>
                                    <requireMavenVersion>
                                        ...
                                    </requireMavenVersion>
                                </rules>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </profile>
    </profiles>
    

    Of course, this is only a runner if your requirement to specify enforcer rules at runtime can be satisfied by a few canned configurations. If, however, the requirement is to support any possible enforcer rule then you're out of luck because the plugin does not support that.