"Project does not define required minimum version of Maven" 使用 versions-maven-plugin 2.7

"Project does not define required minimum version of Maven" with versions-maven-plugin 2.7

我在执行 mvn versions:display-plugin-updates 时遇到以下错误。基于 this discussion,已在 2.6 中修复,但我使用的是 2.7(尝试使用 2.6 但没有成功)。

[ERROR] Project does not define required minimum version of Maven.
[ERROR] Update the pom.xml to contain maven-enforcer-plugin to
[ERROR] force the Maven version which is needed to build this project.
[ERROR] See https://maven.apache.org/enforcer/enforcer-rules/requireMavenVersion.html
[ERROR] Using the minimum version of Maven: 3.0.5

这是我的 <pluginManager> 插件:

<pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>3.0.0-M2</version>
      <executions>
        <execution>
          <id>enforce-versions</id>
          <goals>
            <goal>enforce</goal>
          </goals>
          <configuration>
            <rules>
              <requireMavenVersion>
                <version>3.5.4</version>
              </requireMavenVersion>
            </rules>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>versions-maven-plugin</artifactId>
      <version>2.7</version>
      <configuration>
        <generateBackupPoms>false</generateBackupPoms>
      </configuration>
    </plugin>
  </plugins>
</pluginManagement>

即使给定插件有 pluginManagement,您仍然需要在 pom 的 build->plugins 部分声明插件,配置才能生效。虽然一些插件,例如maven-compiler-plugin 默认添加到项目中,这显然不是 maven-enforcer-plugin 的情况(如您的实验所示)。

我不确定是否有不需要明确添加的插件列表,但我认为它是最常见的插件,如 maven-surefire-plugin 和 maven-clean-plugin。