国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

學習Maven之MavenEnforcerPlugin

2019-11-14 15:35:07
字體:
來源:轉載
供稿:網友

1.Maven Enforcer plugin是什么鬼?

在說這個插件是什么前我們先思考這么一個問題:當我們開發人員進入項目組進行開發前,要準備開發環境,而領導總是會強調工具的統一,編譯環境的統一。比如要求所有開發人員使用JDK1.8進行開發。

開發人員接下來就是去下載指定版本的JDK,然后開始開發。但是如果開發人員的機器配置比較多,有好幾個版本的JDK,而他雖然下載了JDK1.8,但是忘記配置環境變量,很有可能他用了JDK1.6進行的編譯。

問題有了,該如何解決? Maven Enforcer plugin就是來解決這類問題。Enforcer可以在項目validate時,對項目環境進行檢查。

 

2.Maven Enforcer plugin怎么用?

Enforcer配置后默認會在validate后執行enforcer:enforce,然后對項目環境進行檢查。拿上面對JDK的校驗為例,我們在pom.xml中配置插件。

<?xml version="1.0" encoding="UTF-8"?><PRoject>...    <build>        <plugins>            <plugin>                <artifactId>maven-enforcer-plugin</artifactId>                <version>1.4.1</version>                <executions>                    <execution>                        <id>default-cli</id>                        <goals>                            <goal>enforce</goal>                        </goals>                        <phase>validate</phase>                        <configuration>                            <rules>                                <requirejavaVersion>                                    <message>                                        <![CDATA[You are running an older version of Java. This application requires at least JDK ${java.version}.]]>                                    </message>                                    <version>[${java.version}.0,)</version>                                </requireJavaVersion>                            </rules>                        </configuration>                    </execution>                </executions>            </plugin>        </plugins>    </build>    <properties>        <java.version>1.8</java.version>    </properties> </project>

我本地配置沒有JDK1.8,按照我的期望,執行maven命令時應該會失敗。讓我們來執行命令 mvn validate 會打出如下日志告知我們JDK版本過低。

qyfmac$ mvn validate[INFO] Scanning for projects...[INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order:[INFO] [INFO] AppFuse Modular Application[INFO] AppFuse Modular Application - Core[INFO] AppFuse Modular Application - Web (Spring MVC)[INFO]                                                                         [INFO] ------------------------------------------------------------------------[INFO] Building AppFuse Modular Application 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO] [INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-versions) @ stock ---[WARNING] Rule 1: org.apache.maven.plugins.enforcer.RequireJavaVersion failed with message:You are running an older version of Java. This application requires at least JDK 1.8.[INFO] ------------------------------------------------------------------------[INFO] Reactor Summary:[INFO] [INFO] AppFuse Modular Application ........................ FAILURE [  0.987 s][INFO] AppFuse Modular Application - Core ................. SKipPED[INFO] AppFuse Modular Application - Web (Spring MVC) ..... SKIPPED[INFO] ------------------------------------------------------------------------[INFO] BUILD FAILURE[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.886 s[INFO] Finished at: 2015-09-22T15:37:04+08:00[INFO] Final Memory: 16M/156M[INFO] ------------------------------------------------------------------------[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-versions) on project stock: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [Help 1][ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.[ERROR] Re-run Maven using the -X switch to enable full debug logging.[ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

其實執行命令 mvn enforcer:enforce 也可以達到上面的效果。區別就是validate是maven全局命令,enforcer:enforce是只執行這個插件的命令。

這里要注意一點,執行enforcer:enforce時,id必須是default-cli(具體原因請參閱:http://stackoverflow.com/questions/24827194/maven-enforcer-plugin-missing-or-invalid-rules),否則會報

The parameters 'rules' for goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce are missing or invalid

 

3.Maven Enforcer plugin標簽詳解

現在對這個插件的配置講解一下。

<plugin><artifactId>maven-enforcer-plugin</artifactId><version>1.4.1</version><executions>  <execution>    <id>default-cli</id>         --一個執行實例的id      <goals>        <goal>enforce</goal>     --執行的命令      </goals>      <phase>validate</phase>    --執行的階段      <configuration>        <rules>                  --規則          <requireJavaVersion>   --JDK的版本            <message>            --失敗后提示消息              <![CDATA[You are running an older version of Java. This application requires at least JDK ${java.version}.]]>             </message>             <version>[${java.version}.0,)</version>   --JDK版本規則           </requireJavaVersion>         </rules>       </configuration>    </execution>  </executions></plugin>        

 

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時必須是正式版本。

直接上代碼:

<?xml version="1.0" encoding="UTF-8"?><project>...    <build>        <plugins>            <plugin>                <artifactId>maven-enforcer-plugin</artifactId>                <version>1.4.1</version>                <executions>                    <execution>                        <id>default-cli</id>                        <goals>                            <goal>display-info</goal>                            <goal>enforce</goal>                        </goals>                        <phase>validate</phase>                        <configuration>                            <rules>                                <requireMavenVersion>                                    <message>                                        <![CDATA[You are running an older version of Maven. This application requires at least Maven ${maven.version}.]]>                                    </message>                                    <version>[${maven.version},)</version>                                </requireMavenVersion>                                <requireJavaVersion>                                    <message>                                        <![CDATA[You are running an older version of Java. This application requires at least JDK ${java.version}.]]>                                    </message>                                    <version>[${java.version}.0,)</version>                                </requireJavaVersion>                                <bannedDependencies>                                    <!--是否檢查傳遞性依賴(間接依賴)-->                                    <searchTransitive>true</searchTransitive>                                    <excludes>                                        <!--groupId[:artifactId][:version][:type][:scope][:classifier]--> 
<exclude>org.testng:testng</exclude> </excludes> <message>don't use TestNG,must use JUnit</message> </bannedDependencies> <requireOS> <name>mac os x</name> <family>mac</family> <arch>x86_64</arch> <version>10.10.3</version> </requireOS> <requireProperty> <property>project.version</property> <message>"Project version must be specified."</message> <regex>.*(/d|-SNAPSHOT)$</regex> <regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage> </requireProperty> </rules> </configuration> </execution> <execution> <id>enforce-install</id> <goals> <goal>enforce</goal> </goals> <phase>install</phase> <configuration> <rules> <requireProperty> <property>project.version</property> <message>"Project version must be specified."</message> <regex>.*(/d)$</regex> <regexMessage>"Project version must end in a number."</regexMessage> </requireProperty> </rules> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> ... <!--測試放開--> <!--<dependency>--> <!--<groupId>org.testng</groupId>--> <!--<artifactId>testng</artifactId>--> <!--<version>6.1.1</version>--> <!--<scope>test</scope>--> <!--</dependency>--> </dependencies> <properties> <java.version>1.7</java.version> <maven.version>3.3.3</maven.version> </properties></project>

這里要說一下requireOS了,不支持表達式,必須準確的寫一個版本號,個人認為這個不好,不過可以把version注釋掉,不校驗操作系統的版本。

如果某些情況下不檢查環境,可以在maven命令上加一個 -Denforcer.skip=true 來跳過enforcer插件執行。
例如:mvn clean validate -Denforcer.skip=true

 

更多神奇的配置方式還請參閱官方網站 :http://maven.apache.org/enforcer/maven-enforcer-plugin/index.html。 

 

關于作者


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 溧阳市| 台南市| 和龙市| 商丘市| 枣强县| 康平县| 玉树县| 固始县| 台前县| 安塞县| 牙克石市| 滦平县| 时尚| 镇远县| 柳河县| 永平县| 新乐市| 灵山县| 广河县| 荃湾区| 宜阳县| 奉贤区| 日喀则市| 丰县| 高雄市| 化德县| 建水县| 保亭| 安仁县| 紫云| 黄陵县| 岚皋县| 德州市| 彭阳县| 长宁县| 武陟县| 游戏| 彝良县| 深水埗区| 滦南县| 平谷区|