Maven Enforcer Plugin 定义一些必须遵守的配置
你如果想再maven中定义一些配置,这些配置需要整个团队遵守,比如定义maven版本,java版本,os配置,文件系统的配置,或者你想扩展的任何配置,那么就可以使用Maven Enforcer Plugin这个maven插件。
pom中引入Maven Enforcer Plugin插件。
<project> ... <build> <!-- To define the plugin version in your parent POM --> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0.1</version> </plugin> ... </plugins> </pluginManagement> <!-- To use the plugin goals in your POM or parent POM --> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0.1</version> </plugin> ... </plugins> </build> ... </project>
在执行mvn clean package时会默认执行这个插件,如果只想运行这个插件可以直接mvn enforcer:display-info,mvn enforcer:enforce,mvn ,可以参考http://maven.apache.org/plugins/maven-enforcer-plugin/plugin-info.html
比如我们如果想指定maven和java的版本,则配置如下:
<project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.0.1</version> <executions> <execution> <id>enforce-versions</id> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <requireMavenVersion> <version>2.0.6</version> </requireMavenVersion> <requireJavaVersion> <version>1.5</version> </requireJavaVersion> <requireOS> <family>unix</family> </requireOS> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build> [...]
如果为了提高执行速度,不想运行这个插件,则可以通过-Denforcer.skip=true或者简单的-Dskip=true就可以跳过这个这个插件的运行。如果mvn package过程中出现有关这个插件的异常,则可以简单通过这个参数跳过这个验证。
这个插件默认提供的规则有:
alwaysPass - Always passes... used to test plugin configuration. alwaysFail - Always fail... used to test plugin configuration. bannedDependencies - enforces that excluded dependencies aren't included. bannedPlugins - enforces that excluded plugins aren't included. dependencyConvergence - ensure all dependencies converge to the same version. evaluateBeanshell - evaluates a beanshell script. requireReleaseDeps - enforces that no snapshots are included as dependencies. requireReleaseVersion - enforces that the artifact is not a snapshot. requireMavenVersion - enforces the Maven version. requireJavaVersion - enforces the JDK version. requireOS - enforces the OS / CPU Archictecture. requirePluginVersions - enforces that all plugins have a specified version. requireProperty - enforces the existence and values of properties. requireFilesDontExist - enforces that the list of files do not exist. requireFilesExist - enforces that the list of files do exist. requireFilesSize - enforces that the list of files exist and are within a certain size range.
可以参考:http://maven.apache.org/enforcer/enforcer-rules/index.html
当然你可以扩展自定义的规则:http://maven.apache.org/enforcer/enforcer-api/writing-a-custom-rule.html
还有一篇文章可以参考:http://www.juvenxu.com/2010/07/30/you-have-to-follow-convention-maven-enforcer-plugin/