Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 570
Harold L. Brown Avatar asked Oct 06 '17 11:10

Harold L. Brown


2 Answers

Rather than using profiles, as recommended in another answer, you can also pre-configure your <executions> in the main section of your POM and then use the <execution>’s <id> to invoke them from the command line (see the Guide to Configuring Plug-ins for more info on this syntax):

mvn enforcer:enforcer@my-execution-id

As any <execution> of the enforce goal by defaults binds the goal to the validate phase, however, the my-execution-id execution also runs on a normal mvn clean install. If that is not desired, configure the execution with <skip>true</true> and override this on the command-line:

mvn enforcer:enforcer@my-execution-id -Denforcer.skip=false

Whether this is clearer than spreading the maven-enforcer-plugin configuration across the POM’s main section and <profiles> is a matter of personal preference.

like image 113
Andreas Sewe Avatar answered Oct 02 '22 09:10

Andreas Sewe


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.

like image 27
glytching Avatar answered Oct 02 '22 08:10

glytching