学习Maven之Maven Enforcer Plugin
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。
学习Maven之Maven Enforcer Plugin的更多相关文章
- 学习Maven之Maven Surefire Plugin(JUnit篇)
1.maven-surefire-plugin是个什么鬼? 如果你执行过mvn test或者执行其他maven命令时跑了测试用例,你就已经用过maven-surefire-plugin了.maven- ...
- 关于maven的规则插件:Maven Enforcer plugin
Maven提供了Maven-Enforcer-Plugin插件,用来校验约定遵守情况(或者说校验开发环境).比如JDK的版本,Maven的版本,开发环境(Linux,Windows等),依赖jar包的 ...
- MAVEN学习笔记之Maven插件的应用(4)
MAVEN学习笔记之Maven插件的应用(4) <build> <pluginManagement> <plugins> <plugin> <gr ...
- MAVEN学习笔记之Maven生命周期和插件简介(3)
MAVEN学习笔记之Maven生命周期和插件简介(3) clean compile site三套生命周期相互独立. clean pre-clean 执行清理前的工作 clean 清理上一次构建生成的所 ...
- maven 学习---部署基于Maven的war文件到Tomcat
在本教程中,我们将学习如何使用Maven的Tomcat插件打包并部署一个WAR文件到Tomcat(Tomcat的6和7. 要用到工具: Maven 3 Tomcat 6.0.37 Tomcat 7.0 ...
- 【mybatis源码学习】利用maven插件自动生成mybatis代码
[一]在要生成代码的项目模块的pom.xml文件中添加maven插件 <!--mybatis代码生成器--> <plugin> <groupId>org.mybat ...
- (转)Maven学习总结(六)——Maven与Eclipse整合
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(六)——Maven与Eclipse整合 一.安装Maven插件 下载下来的maven插件如下图所示:,插件存放的路径是:E:/MavenP ...
- (转)Maven学习总结(四)——Maven核心概念
孤傲苍狼只为成功找方法,不为失败找借口! Maven学习总结(四)——Maven核心概念 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Mav ...
- (转)Maven学习总结(二)——Maven项目构建过程练习
孤傲苍狼 只为成功找方法,不为失败找借口! Maven学习总结(二)——Maven项目构建过程练习 上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项 ...
随机推荐
- OracleDBA之用户管理
再分享一下Oracle中对用户的管理,以下这些东西是我的麦库上存的当时学Oracle的学习笔记今天拿出来和大家分享一下,转载请注明出处,下面用的Oracle的版本是10g,用的时WinServer20 ...
- 移动前端开发-单页应用(spa)模型
一门新的技术诞生总会引来一番争议,单页Web应用程序也不例外,其最大的优势在于用户体验,对于内容的改动不需要加载整个页面:对服务器压力很小,消耗更少的带宽,与面向服务的架构更好地结合.使用HTML+C ...
- C语言 第二章 数据类型、变量和输入函数
一.数据类型简介 在 C 语言中,数据类型指的是用于声明不同类型的变量或函数的一个广泛的系统.变量的类型决定了变量存储占用的空间,以及如何解释存储的位模式. 类型转换: 类型 存储大小 值范围 cha ...
- opendaylight的Beryllium安装
1.首先安装jdk #sudo apt-get install openjdk-7-jdk 2.安装vim编辑工具 #sudo apt-get install vim 3.编辑~/.bashrc ...
- openfire 初始密码
openfire 初始密码 mssql2014 进入数据库,找到 ofUser 表 ,将密码字段对应的密文替换为下面的内容,则密码就是 admin ecbd03623cd819c48718db1b27 ...
- SQL Server:APPLY表运算符
SQL Server 2005(含)以上版本,新增了APPLY表运算,为我们日常查询带来了极大的方便. 新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行.它不像JOIN那样先计算那个表表 ...
- EC笔记,第二部分:8.别让异常逃离析构函数
1.为何析构函数不应该抛出异常? 有两种情况: 1).假设析构函数中有众多语句,而第一条语句抛出异常(或者其他语句),那么抛出异常以后的语句就得不到执行.而通常我们在析构函数中写的是清理资 ...
- 数据结构:单链表结构字符串(python版)
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- SpringMvc面试题
f-sm-1. 讲下SpringMvc和Struts1,Struts2的比较的优势 性能上Struts1>SpringMvc>Struts2 开发速度上SpringMvc和Struts2差 ...
- 【工匠大道】markdown使用技巧
本文地址 提纲: 1. 概述 2. 常见技巧 3. 参考文档 1. 概述 常见的markdown的技巧,这里不再谈了,主要是自己感觉比较少见但有用的技巧. 2. 常见技巧 1)[空格]生成空格的效 ...