27

I am trying to setup the enforcer plugin for maven to enforce a minimum Java version. However, whenever I try to run mvn enforcer:enforce, I get:

The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.3.1:enforce are missing or invalid

Here is the relevant portion of my pom file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.3.1</version>
    <executions>
        <execution>
            <id>enforce-java</id>
            <phase>validate</phase>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <requireJavaVersion>
                        <version>(1.7.0-20,)</version>
                    </requireJavaVersion>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

I also tried replacing the <requireJavaVersion> block with <alwaysPass/>, in case something was invalid, but it still failed with the same error.

hoijui
  • 3,615
  • 2
  • 33
  • 41
X3no
  • 412
  • 1
  • 5
  • 10
  • Possible duplicate of [usage of maven enforcer plugin](https://stackoverflow.com/questions/6754974/usage-of-maven-enforcer-plugin) – acm Oct 29 '18 at 11:38
  • @acm in my case the solution was the default-cli parameter, part of the accepted answer here. Less obvious in https://stackoverflow.com/questions/6754974/usage-of-maven-enforcer-plugin – louisgab Jan 02 '19 at 20:01
  • doesnt work, I always get error Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M3:enforce (default-cli) on project : No rules are configured. Use the skip flag if you want to disable execution. – Govind Kalyankar Mar 24 '20 at 23:14
  • [This](https://maven.apache.org/enforcer/enforcer-rules/dependencyConvergence.html) this is really helpful. – Diablo Jul 25 '21 at 16:56

3 Answers3

39

It may be that you are using invalid rule names. Check out the rules page. The rule names are case sensitive. Though this is not the case here.

---- Edit ----

Note that the POM configuration has an execution ID of enforce-java and that execution is bound to the validate phase of the lifecycle. The command mvn enforcer:enforce is running a goal, not a phase in the lifecycle. The configuration you provided in the POM doesn't apply to the enforcer:enforce goal.

There are two ways to make this work. Which one you choose depends on what you need.

  1. If you are just trying to test the enforcer plugin configuration without running the whole build, run mvn validate.
  2. If the requirement is that mvn enforcer:enforce works, then change the execution ID to default-cli.
hoijui
  • 3,615
  • 2
  • 33
  • 41
user944849
  • 14,524
  • 2
  • 61
  • 83
  • I was originally using and but when I copied it here I did it incorrectly. even with the valid rule names I get the same error. I edited the post to correct this. – X3no Jul 18 '14 at 14:41
  • 4
    Changing the id to `default-cli` makes `mvn enforcer:enforce` work correctly. But `mvn validate` always succeeds even if the enforced requirements aren't met. – X3no Jul 18 '14 at 20:28
  • You should check your version range for Java you have given. I think you would like to do something like this: ´[1.7.0,)`. – khmarbaise Jul 19 '14 at 13:37
  • If you change the id to `default-cli`, then the config will ONLY work if you run `enforcer:enforce` from the command line. The way you originally had it, the config works when run as part of the lifecycle. If you want both to work, then you need two executions, one with ID `default-cli` and one with phase `validate`. Consider reviewing the [Maven documentation](http://maven.apache.org/guides/mini/guide-default-execution-ids.html) for an explanation. – user944849 Jul 21 '14 at 13:33
  • I changed the id back to enforce-java but it still doesn't get run during the validate phase. – X3no Jul 21 '14 at 13:49
  • Fixed it. The issue was that my plugins were nested in a `` tag. It works as expected now. Thanks. – X3no Jul 21 '14 at 14:55
  • 8
    `default-cli` fixed it for me, thanks. I like how Maven requires hard-to-find arcane incantations to make the simplest things work. – mkm13 Apr 24 '15 at 11:04
  • 6
    To be fair, its the `execution` that complicates this configuration. `enforcer:enforce` would also work if you added the configuration to the plugin instead of a specific execution. Something like this: ``` org.apache.maven.plugins maven-enforcer-plugin 3.0.0-M2 ``` – lmsurprenant Aug 22 '18 at 14:39
0

I encountered this precise same error while trying to build vertx workshop project. As it turns out, the error is primarily an enforcer plugin version related issue. This following configuration solved it for me.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-enforcer-plugin</artifactId>
  <version>3.0.0-M3</version><!--$NO-MVN-MAN-VER$-->
  <executions>
    <execution>
      <id>enforce-java</id>
      <phase>enforce</phase>
      <goals>
        <goal>enforce</goal>
      </goals>
      <configuration>
        <rules>
          <requireJavaVersion>
            <version>(1.8.0,)</version>
          </requireJavaVersion>
        </rules>
      </configuration>
    </execution>
  </executions>
</plugin>
flyingdutchman
  • 1,197
  • 11
  • 17
Gautam
  • 1,030
  • 13
  • 37
0

In my case the problem was that I was putting the enforcer configuration inside the build part of a Maven profile which was not being built when I ran mvn validate. A simple mvn validate -P correctProfile fixed it for me. In the end I put it into the parent project of a multi-module application, set the transitive search of bytecode enforcer rule to true: <searchTransitive>true</searchTransitive> and verified that an incorrect dependency in a child project indeed causes a build failure.

JohnEye
  • 6,436
  • 4
  • 41
  • 67