我在其自己的项目中有一个父pom,以实现可重用性,该父pom将maven-enforcer-plugin
配置为运行:
<project ...>
<!-- ... -->
<groupId>org.example.common</groupId>
<artifactId>common-parent</artifactId>
<version>1</version>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-default-settings</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requirePluginVersions><message>Best Practice is to always define plugin versions!</message></requirePluginVersions>
<dependencyConvergence />
<reactorModuleConvergence />
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<!-- loads of version pinning of default maven plugins like the one below -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
</plugin>
<!-- ... -->
</plugins>
<!-- ... -->
</build>
<!-- ... -->
</project>
在我的实际pom中,我已经
<project ...>
<!-- ... -->
<parent>
<groupId>org.example.common</groupId>
<artifactId>common-parent</artifactId>
<version>1</version>
</parent>
<groupId>org.example</groupId>
<artifactId>project-x</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- ... -->
</project>
当我在子pom上运行mvn clean package
时,我会得到
[INFO] --- maven-enforcer-plugin:3.0.0-M2:enforce (enforce-default-settings) @ project-x ---
[WARNING] Rule 2: org.apache.maven.plugins.enforcer.RequirePluginVersions failed with message:
Some plugins are missing valid versions:(LATEST RELEASE SNAPSHOT are not allowed )
ome plugins are missing valid versions:(LATEST RELEASE SNAPSHOT are not allowed )
org.apache.maven.plugins:maven-compiler-plugin. The version currently in use is 3.8.1
org.apache.maven.plugins:maven-surefire-plugin. The version currently in use is 2.22.2
org.apache.maven.plugins:maven-jar-plugin. The version currently in use is 3.1.2
org.apache.maven.plugins:maven-clean-plugin. The version currently in use is 3.1.0
org.apache.maven.plugins:maven-dependency-plugin. The version currently in use is 3.1.1
org.apache.maven.plugins:maven-install-plugin. The version currently in use is 2.5.2
org.apache.maven.plugins:maven-source-plugin. The version currently in use is 3.1.0
org.apache.maven.plugins:maven-site-plugin. The version currently in use is 3.8.2
org.apache.maven.plugins:maven-resources-plugin. The version currently in use is 3.1.0
org.apache.maven.plugins:maven-deploy-plugin. The version currently in use is 2.8.2
org.apache.maven.plugins:maven-enforcer-plugin. The version currently in use is 3.0.0-M2
Best Practice is to always define plugin versions!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.665 s
[INFO] Finished at: 2019-10-09T12:37:00+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.0.0-M2:enforce (enforce-default-settings) on project project-x: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1]
在父pom上运行mvn clean package
可以正常工作。
尽管当我在儿童pom上运行mvn versions:display-plugin-updates
时,我得到了:
[INFO] --- versions-maven-plugin:2.7:display-plugin-updates (default-cli) @ project-x ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[INFO] All plugins have a version specified.
[INFO]
[INFO] Project inherits minimum Maven version as: 3.5.0
[INFO] Plugins require minimum Maven version of: null
[INFO]
[INFO] No plugins require a newer version of Maven than specified by the pom.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
当我删除从父pom固定版本时。构建父pom时,我不收到警告。另外,子pom上的版本插件会报告:
[INFO] --- versions-maven-plugin:2.7:display-plugin-updates (default-cli) @ project-x ---
[INFO]
[INFO] All plugins with a version specified are using the latest versions.
[INFO]
[WARNING] The following plugins do not have their version specified:
[WARNING] maven-jar-plugin ........................ (from super-pom) 3.1.2
所以我对此感到困惑。我以为可以通过将版本固定在父pom的dependencyManagement中来做正确的事?
我也不明白为什么强制执行器似乎对父pom不起作用。
答案 0 :(得分:1)
尽管通常可以安全地将“配置”设置放置在插件管理树中的插件引用中,但执行起来却不是。
按照以下步骤更新pom并与继承的工件共享执行器执行:
<project ...>
<!-- ... -->
<build>
<pluginManagement>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
</plugin>
<!-- ... -->
</plugins>
</pluginManagement>
<plugins>
<!-- ... -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-default-settings</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requirePluginVersions><message>Best Practice is to always define plugin versions!</message></requirePluginVersions>
<dependencyConvergence />
<reactorModuleConvergence />
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<!-- ... -->
</plugins>
<!-- ... -->
</build>
<!-- ... -->
</project>
注意:您当然可以在插件管理树中移动<configuration>
部分,但是在当前情况下没有这样做的附加值。
答案 1 :(得分:1)
执行程序插件版本3.0.0-M2可能有问题-请参见https://issues.apache.org/jira/browse/MENFORCER-306。已在3.0.0-M3中修复