javamavencommand-line-interfacemaven-enforcer-plugin

Add parameters to Maven Enforcer Rules called from the command line


Following https://stackoverflow.com/a/59389998/927493, one can now call the Maven enforcer plugin from the command line.

This works well, but unfortunately I haven't understood yet whether I can set rule parameters through the command line. An example would be version in the RequireMavenVersion rule.


Solution

  • No you can't. I'm actually not that happy with the current implementation because of these expectations. The first usecase was a simple, parameterless rule. Of course we could predict the next questions.

    Let's first explain how "complex" plugin configuration works. Take a look at the following example:

    <rules>
      <requireMavenVersion>
        <version/>
      </requireMavenVersion>
    </rules>
    

    Here requireMavenVersion is the lowercase classname in the same package as the EnforceMojo(or enforce goal), version is a setter on this class. What you see is nothing more than a Pojo. The only requirement here is that RequireMavenVersion implements the EnforcerRule interface, so Maven can call its execute method see:

     @Parameter
     private EnforcerRule[] rules;
    

    If you want to call a rule from commandline, the plugin simple tries to rename the rule to a full qualified classname make a new instance of it. Next its execute method will be called.

    The request for this feature was to enforce rules without touching the project. This should be solved as a Maven Extension, which fits much better to that usecase.

    But in conclusion: no you can't.

    UPDATE As of Maven Enforcer 3.0.0 there's a Maven Enforcer Extension which gives you an extra way to define enforcer rules.