Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enforcing maven 3 - when to use maven enforcer plugin? when to use pom prerequisites element?

Tags:

java

maven

The two main approaches for enforcing maven 3 seem to be:

  • maven-enforcer-plugin, and
  • pom.xml <prerequisites> element.

The best approach to uses seems to depend on a few different factors. This question is to help people decide which approach makes the most sense for them.

Question: What types of project structures are best suited to maven-enforcer-plugin and what types of project structure are best suited to pom prerequisites.


Maven Enforcer Plugin Example

<build>
  <plugins>
    <plugin>
      <inherited>true</inherited>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>1.3</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>

Maven POM Prerequisites Example

<project>
   ...
   <prerequisites>
       <maven>3.0.5</maven>
   </prerequisites>
   ...
</project>
like image 951
Chris Snow Avatar asked Aug 05 '13 09:08

Chris Snow


People also ask

What is the purpose of Maven enforcer plugin?

The Enforcer plugin provides goals to control certain environmental constraints such as Maven version, JDK version and OS family along with many more built-in rules and user created rules.

Which plugin goal can be used to cause a build failure if the project uses an old version of Maven or Java?

<artifactId>maven-enforcer-plugin</artifactId> <version>3.1. 0</version>

In which section of POM do you configure Maven plugins?

Build plugins They execute during the build process and should be configured in the <build/> element of pom.

What is dependency convergence error?

This rule requires that dependency version numbers converge. If a project has two dependencies, A and B, both depending on the same artifact, C, this rule will fail the build if A depends on a different version of C than the version of C depended on by B.

What are the goals of the Maven enforcer plugin?

Plugin Configuration and Goals. Maven Enforcer has two goals: enforcer:enforce and enforcer:display-info. The enforce goal runs during a project build to execute rules specified in the configuration, while the display-info goal shows current information about the built-in rules that are present in the project's pom.xml. Let's define...

What are the rules of Maven enforcer?

The Maven Enforcer plugin has many other rules to promote project quality and consistency irrespective of the development environment. Also, the plugin has a command to display info about some currently configured rules: 5. Custom Rules So far, we've been exploring the built-in rules of the plugin.

What does the keyword enforce mean in Maven enforcer?

The keyword enforce gives a subtle suggestion of the existence of rules to abide by. This is how the Maven Enforcer plugin works. We configure it with some rules that are to be enforced during the build phase of the project.

What is the use of requiremavenversion and requirejavaversion rules?

The requireMavenVersion and requireJavaVersion rules enable a project-wide lock-in of required Maven and Java versions, respectively. This will help eliminate the disparity that might arise from using different versions of Maven and JDK in development environments. Let's update the rules section of the plugin configuration:


2 Answers

Prerequisites are deprecated, and you should use the enforcer plugin. MNG-5297 requests a documentation update to clarify this.

The maven-enforcer-plugin FAQ explains the difference:

Why can't I just use the prerequisites tag in the pom?

The prerequisites tag was designed to be used by tools like plugins. It will work for regular projects, but it isn't inherited to their children. If it is set in a parent reactor, then Maven will do the check. However if one of the children are built, the check is not performed. The enforcer plugin is designed to allow centralized control over the build environment from a single "super-pom", and to allow greater flexibility in version specification by supporting ranges.

In addition, note that prerequisites do not work with Maven 3.

like image 58
Joe Avatar answered Sep 19 '22 08:09

Joe


The Maven pom prerequisites approach may be more appropriate for smaller, simpler projects. This approach will be lighter weight as it will not require the maven-enforcer-plugin to be downloaded.

The maven-enforcer-plugin approach may be more suitable if you have children projects that you wish to inherit the maven 3 requirement from the parent. pom prerequisites is not inherited by children projects.

like image 33
Chris Snow Avatar answered Sep 21 '22 08:09

Chris Snow