在說這個插件是什麼前我們先思考這麼一個問題:當我們開發人員進入項目組進行開發前,要準備開發環境,而上司總是會強調工具的統一,編譯環境的統一。比如要求所有開發人員使用JDK1.8進行開發。
開發人員接下來就是去下載下傳指定版本的JDK,然後開始開發。但是如果開發人員的機器配置比較多,有好幾個版本的JDK,而他雖然下載下傳了JDK1.8,但是忘記配置環境變量,很有可能他用了JDK1.6進行的編譯。
問題有了,該如何解決? Maven Enforcer plugin就是來解決這類問題。Enforcer可以在項目validate時,對項目環境進行檢查。
Enforcer配置後預設會在validate後執行enforcer:enforce,然後對項目環境進行檢查。拿上面對JDK的校驗為例,我們在pom.xml中配置插件。
我本地配置沒有JDK1.8,按照我的期望,執行maven指令時應該會失敗。讓我們來執行指令 mvn validate 會打出如下日志告知我們JDK版本過低。
其實執行指令 mvn enforcer:enforce 也可以達到上面的效果。差別就是validate是maven全局指令,enforcer:enforce是隻執行這個插件的指令。
這裡要注意一點,執行enforcer:enforce時,id必須是default-cli(具體原因請參閱:http://stackoverflow.com/questions/24827194/maven-enforcer-plugin-missing-or-invalid-rules),否則會報
現在對這個插件的配置講解一下。
Enforcer的rules除了控制JDK版本,還有很多其他規則,甚至可以通過它的接口自定義。
alwaysFail - Always fail... used to test plugin configuration.
alwaysPass - Always passes... used to test plugin configuration.
banDistributionManagement - enforces that project doesn't have distributionManagement.
bannedDependencies - enforces that excluded dependencies aren't included.
bannedPlugins - enforces that specific plugins aren't included in the build.
bannedRepositories - enforces to not include banned repositories.
banTransitiveDependencies - enforces that project doesn't have transitive dependencies.
dependencyConvergence - ensure all dependencies converge to the same version.
evaluateBeanshell - evaluates a beanshell script.
reactorModuleConvergence - enforces that a multi module build follows best practice.
requireActiveProfile - enforces one or more active profiles.
requireEnvironmentVariable - enforces the existence of an environment variable
requireFilesDontExist - enforces that the list of files does not exist.
requireFilesExist - enforces that the list of files does exist.
requireFilesSize - enforces that the list of files exists and is within a certain size range.
requireJavaVersion - enforces the JDK version.
requireMavenVersion - enforces the Maven version.
requireNoRepositories - enforces to not include repositories.
requireOS - enforces the OS / CPU Architecture.
requirePluginVersions - enforces that all plugins have a specified version.
requirePrerequisite - enforces that prerequisites have been specified.
requireProperty - enforces the existence and values of properties.
requireReleaseDeps - enforces that no snapshots are included as dependencies.
requireReleaseVersion - enforces that the artifact is not a snapshot.
requireSameVersions - enforces that specific dependencies and/or plugins have the same version.
requireUpperBoundDeps - ensures that every (transitive) dependency is resolved to it's specified version or higher.
You may also create and inject your own custom rules by following the maven-enforcer-rule-api instructions.
我們再寫一個例子。比如除了需要限制JDK外,我們還要限定maven版本,項目不得包含TestNG依賴,作業系統必須是mac os x 64位,項目版本号在執行install時必須是正式版本。
直接上代碼:
這裡要說一下requireOS了,不支援表達式,必須準确的寫一個版本号,個人認為這個不好,不過可以把version注釋掉,不校驗作業系統的版本。
如果某些情況下不檢查環境,可以在maven指令上加一個 -Denforcer.skip=true 來跳過enforcer插件執行。
例如:mvn clean validate -Denforcer.skip=true
更多神奇的配置方式還請參閱官方網站 :http://maven.apache.org/enforcer/maven-enforcer-plugin/index.html。
關于作者
作者:辵鵵
出處:http://qyf404.cnblogs.com
歡迎轉載,但未經作者同意請保留此段聲明,并在文章頁面明顯位置給出原文連結。