简体   繁体   中英

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

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>

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM