23

I am currently doing some cleanup of Java projects which use Maven, and use NetBeans IDE to 'debug' problems in the POM. I have set Maven 3.0.4 in the IDE as the Maven version, but other developers or our Continuous Intgeration system might have different settings.

Is it possible to 'enforce' a specific Maven version directly in the POM (for example by using a Maven 3 specific element)?

3 Answers 3

27

Yes you can and you should. Some Maven plugins requires Maven 3 or newer.

Use the maven-enforcer-plugin by adding the following to your pom.xml:

<build>
  <plugins>
    <plugin>
      <inherited>true</inherited>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>1.3.1</version>
        <executions>
          <execution>
            <id>enforce-maven-3</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireMavenVersion>
                  <version>3.0.5</version>
                </requireMavenVersion>                
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
       </executions>
     </plugin>
  </plugins>
</build>
11

Another option is to use the prerequisites element in the pom, for example:

<project>
   ...
   <prerequisites>
       <maven>3.0.0</maven>
   </prerequisites>
   ...
</project>

As noted Michal Kalinowski's answer - this simple approach does not work so well for children projects.

For a summary of which approach will work best for you, see here: enforcing maven 3 - when to use maven enforcer plugin? when to use pom prerequisites element?

0
7

The best thing you can do is to use Enforcer plugin. Here's an example:
http://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html

There is no out-of-the-box method in Maven itself for doing this. There is actually prerequisites tag but it doesn't work well, because children don't inherit it.

1
  • 1
    Be mindful of including links as they can die... And note there was already another answer that includes the relevant snippet.
    – Barett
    Dec 11, 2017 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.