简体   繁体   English

执行maven 3 - 何时使用maven enforcer插件?什么时候使用pom先决条件元素?

[英]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 3的两种主要方法似乎是:

  • maven-enforcer-plugin, and maven-enforcer-plugin,和
  • pom.xml <prerequisites> element. pom.xml <prerequisites>元素。

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,哪种类型的项目结构最适合于先决条件。


Maven Enforcer Plugin Example Maven Enforcer插件示例

<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 Maven POM先决条件示例

<project>
   ...
   <prerequisites>
       <maven>3.0.5</maven>
   </prerequisites>
   ...
</project>

Prerequisites are deprecated, and you should use the enforcer plugin. 不推荐使用先决条件,您应该使用enforcer插件。 MNG-5297 requests a documentation update to clarify this. MNG-5297要求更新文档以澄清这一点。

The maven-enforcer-plugin FAQ explains the difference : maven-enforcer-plugin FAQ 解释了不同之处

Why can't I just use the prerequisites tag in the pom? 为什么我不能只使用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. 如果它在父反应堆中设置,那么Maven将进行检查。 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. enforcer插件旨在允许从单个“super-pom”集中控制构建环境,并通过支持范围允许更大的版本规范灵活性。

In addition, note that prerequisites do not work with Maven 3 . 此外,请注意, 先决条件不适用于Maven 3

The Maven pom prerequisites approach may be more appropriate for smaller, simpler projects. Maven pom prerequisites方法可能更适合更小,更简单的项目。 This approach will be lighter weight as it will not require the maven-enforcer-plugin to be downloaded. 这种方法的重量较轻,因为它不需要下载maven-enforcer-plugin

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. 如果您希望从父级继承maven 3要求的子项目,则maven-enforcer-plugin方法可能更合适。 pom prerequisites is not inherited by children projects. 子项目不会继承pom prerequisites

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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