Maven学习总结(16)——深入理解maven生命周期和插件
在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最近实在受不了乌龟一样的build速度,于是下定决心再看看《maven实战》吧,
对于我来说,maven最主要的作用有两个方面,一个是对jar包的依赖解决功能,自己管理jar包,另一个功能就是项目的构建,打包部署。现在我觉得最重要的还是maven的生命周期和插件机制,下面就来总结一下吧。
参考url:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
1、三套生命周期
对于maven的生命周期来说,共有三个相互独立的生命周期,分别是clean、default、site。clean生命周期目的是清理项目,default生命周期目的是构建项目,而site生命周期目的是建立项目站点。
每个生命周期分别包含一些阶段,这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段。如clean生命周期包含pre-clean、clean和post-clean三个阶段,如果执行clean阶段,则会先执行pre-clean阶段。
较之于生命周期阶段有前后依赖关系,三套生命周期本身是相互独立的,用户可以仅调用clean生命周期的某个阶段,也可以不执行clean周期,而直接执行default生命周期的某几个阶段。
2、clean生命周期
clean生命周期包含三个阶段,主要负责清理项目,如下:
pre-clean | executes processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | executes processes needed to finalize the project cleaning |
3、default生命周期
default生命周期定义了真正构建时所需要执行的所有步骤,包含的阶段如下:
validate | validate the project is correct and all necessary information is available. |
initialize | initialize build state, e.g. set properties or create directories. |
generate-sources | generate any source code for inclusion in compilation. |
process-sources | process the source code, for example to filter any values. |
generate-resources | generate resources for inclusion in the package. |
process-resources | copy and process the resources into the destination directory, ready for packaging. |
compile | compile the source code of the project. |
process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources | generate any test source code for inclusion in compilation. |
process-test-sources | process the test source code, for example to filter any values. |
generate-test-resources | create resources for testing. |
process-test-resources | copy and process the resources into the test destination directory. |
test-compile | compile the test source code into the test destination directory |
process-test-classes | post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above. |
test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
prepare-package | perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above) |
package | take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify | run any checks to verify the package is valid and meets quality criteria. |
install | install the package into the local repository, for use as a dependency in other projects locally. |
deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
4、site生命周期
siet生命周期的目的是建立和发布项目站点,maven能够基于POM所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息,包含的阶段如下:
pre-site | executes processes needed prior to the actual project site generation |
site | generates the project's site documentation |
post-site | executes processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy | deploys the generated site documentation to the specified web server |
5、命令行与生命周期
从命令行执行maven任务的最主要方式就是调用maven的生命周期阶段。需要注意的是,各个生命周期是相互独立的,而一个生命周期的阶段是有前后依赖关系的。例子如下:
1、$mvn clean :该命令调用clean生命周期的clean阶段。实际执行的阶段为clean生命周期的pre-clean和clean阶段。
2、$mvn test:该命令调用default生命周期的test阶段。实际调用的是default生命周期的validate、initialize等,直到test的所有阶段。
3、$mvn clean install:该命令调换用clean生命周期的clean阶段和default生命周期的instal阶段。
6、插件目标
maven的核心仅仅定义了抽象的生命周期,具体的任务是交由插件完成的,插件以独立的形式存在。
对于插件本身,为了能够复用代码,它往往能够完成多个任务。如maven-dependency-plugin有十多个目标,每个目标对应了一个功能,如 dependency:analyze、 dependency:tree和dependency:list。这是一种通用的写法,冒号前面是插件前缀,后面是该插件的目标。
7、插件绑定
maven的生命周期与插件相互绑定,用以完成实际的构建任务。具体而言,是生命周期的阶段与插件的目标相互绑定,已完成某个具体的构建任务。例如项目编译这一任务,它对应了default生命周期的compile阶段,而maven-compiler-plugin这一插件的compile目标能够完成该任务,因此将他们绑定。
7.1内置绑定
maven在核心为一些主要的生命周期接到绑定了很多插件的目标,如下:
clean和site生命周期相对简单。
clean | clean:clean |
site | site:site |
site-deploy | site:deploy |
default生命周期与插件目标的绑定关系有点复杂一些。这是因为对于任何项目来说,例如jar项目和war项目,他们的项目清理和站点生成任务是一样的,不过构建过程会有区别。例如jar项目需要打成jar包,而war项目需要打成war包。
由于项目的打包类型会影响构建的具体过程,因此,default生命周期的阶段与插件目标的绑定关系有项目打包类型所决定的,打包类型是通过pom中的packaging元素定义的。最常见的打包类型是jar,它也是默认的打包类型。基于该打包类型,default生命周期的内置绑定关系如下:
process-resources | resources:resources |
compile | compiler:compile |
process-test-resources | resources:testResources |
test-compile | compiler:testCompile |
test | surefire:test |
package | ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war |
install | install:install |
deploy | deploy:deploy |
7、2自定义绑定
除了内置绑定以为,用户还能够自己选择奖某个插件目标绑定到生命周期的某个阶段以执行更多更特色的任务。
- <!-- 自动复制资源文件件到根目录 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.6</version>
- <configuration>
- <includeEmptyDirs>true</includeEmptyDirs>
- <encoding>GBK</encoding>
- <nonFilteredFileExtensions>
- <nonFilteredFileExtension>exe</nonFilteredFileExtension>
- <nonFilteredFileExtension>zip</nonFilteredFileExtension>
- <nonFilteredFileExtension>vbs</nonFilteredFileExtension>
- <nonFilteredFileExtension>sh</nonFilteredFileExtension>
- </nonFilteredFileExtensions>
- </configuration>
- <executions>
- <execution>
- <id>copy-resources</id>
- <phase>validate</phase>
- <goals>
- <goal>copy-resources</goal>
- </goals>
- <configuration>
- <includeEmptyDirs>true</includeEmptyDirs>
- <outputDirectory>${project.build.directory}</outputDirectory>
- <excludes>
- <exclude>agentmanager.jsmooth</exclude>
- <exclude>assembly.xml</exclude>
- </excludes>
- <resources>
- <resource>
- <directory>src/main/resources/</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </configuration>
- </execution>
- </executions>
- </plugin>
如上图定义了一个id为copy-resources的任务,绑定到default生命周期的validate阶段,绑定的插件为maven-resources-plugin,插件目标为copy-resources。即用插件的copy-resources功能来实现项目资源文件的拷贝。
- <!-- 自动复制maven依赖包到lib目录 -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-dependency-plugin</artifactId>
- <version>2.1</version>
- <executions>
- <execution>
- <id>copy</id>
- <phase>install</phase>
- <goals>
- <goal>copy-dependencies</goal>
- </goals>
- <configuration>
- <outputDirectory>lib</outputDirectory>
- </configuration>
- </execution>
- </executions>
- </plugin>
同上,定义了一个id为copy的任务,利用插件maven-dependency-plugin的copy-dependencies目标绑定到default生命周期的install阶段,来实现项目依赖的jar包的自动复制。
当插件目标被绑定到不同的生命周期阶段时候,其执行顺序会有生命周期阶段的先后顺序决定的。如果多个目标被绑定到同一个阶段,他们的执行顺序是由插件声明的先后顺序决定目标的执行顺序。
8、插件配置
用户可以配置插件目标的参数,进一步调整插件目标所执行的任务。
8、1命令行插件配置
如 $mvn install -Dmaven.test.skip=true 的意义即跳过测试步骤。
参数-D的java自带的,其功能是通过命令行设置一个java系统属性,maven简单地重用了该参数以实现插件参数的配置。
8、2pom中插件全局配置
如项目编译使用1.6版本的源文件,生成与JVM1.6兼容的字节码文件,如下:
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
9、获取插件描述信息
$mvn help:describe-Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.1 来获取插件的详细信息
可以简化为:
$mvn help:describe-Dplugin=compiler
如果仅仅描述插件目标的信息,可以加上goal参数:
$mvn help:describe-Dplugin=compiler-Dgoal=compile
如果想输出更详细的信息,可以加上detail参数:
$mvn help:describe-Dplugin=compiler-Ddetail
Maven学习总结(16)——深入理解maven生命周期和插件的更多相关文章
- Maven 学习总结 (二) 之 生命周期与插件
五.生命周期与插件 1.Maven有三套独立的生命周期:clean.default和site. clean生命周期的目的是清理项目,default生命周期的目的是构建项目,site生命周期的目的是建立 ...
- 深入理解maven及应用(一):生命周期和插件
在项目里用了快一年的maven了,近期突然发现maven项目在eclipse中build时很慢,由于经经常使用clean install命令来build项目,也没有管那么多,但近期实在受不了乌龟一样的 ...
- 05 Maven 生命周期和插件
Maven 生命周期和插件 除了坐标.依赖以及仓库之外, Maven 另外两个核心概念是生命周期和插件.在有关 Maven 的日常使用中,命令行的输入往往就对应了生命周期,如 mvn package ...
- Maven系列第6篇:生命周期和插件详解,此篇看过之后在maven的理解上可以超越同级别90%的人!
maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第6篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...
- Maven学习(四)-- 生命周期和插件
标签(空格分隔): 学习笔记 Maven生命周期是抽象的,不做任何实际的工作,在Maven的设计中,实际的任务都交由插件来完成. 每个构件步骤都可以绑定一个或者多个插件行为,而且Maven为大多数构建 ...
- maven学习(十)——maven生命周期以及插件
一.生命周期 1.何为生命周期? Maven生命周期就是为了对所有的构建过程进行抽象和统一,包括项目清理,初始化,编译,打包,测试,部署等几乎所有构建步骤 2.Maven三大生命周期 Maven有三套 ...
- MAVEN学习笔记之Maven生命周期和插件简介(3)
MAVEN学习笔记之Maven生命周期和插件简介(3) clean compile site三套生命周期相互独立. clean pre-clean 执行清理前的工作 clean 清理上一次构建生成的所 ...
- Maven学习笔记(六):生命周期与插件
何为生命周期: Maven的生命周期就是为了对全部的构建过程进行抽象和统一.Maven从大量项目和构建工具中学习和反思,然后总结了一套高度完好的.易扩展的生命周期.这个生命周期包括了项目的清 ...
- Maven学习总结(4)——Maven核心概念
Maven学习总结(四)--Maven核心概念 一.Maven坐标 1.1.什么是坐标? 在平面几何中坐标(x,y)可以标识平面中唯一的一点. 1.2.Maven坐标主要组成 groupId:组织标识 ...
随机推荐
- Lua刚開始学习的人(一)--Lua 简单教学
近期因为工作原因.临时木有<Oracle起步学习>续集.领导知道学习下Lua脚本语言.看了一周了.趁热打铁,留下点实用的东西吧. 本系列会主要针对宿主语言为 Delphi,原理都是一样的, ...
- jmeter名词解释之聚合报告
新浪围脖>@o蜗牛快跑o 温馨提示: 1. tps(吞吐量)表征系统性能,系统的好坏能够用这个评估 2. 90%Line是满足需求响应时间的重要指标,假设用户需求说是响应时间不小于5s,那 ...
- SQL Server 为代码减负之存储过程
存储过程能够用来提高数据库的查询效率.由于它事先被编译过.被储存于内存中,每次执行前.不必被从新编译,所以效率非常高. 存储过程是一组sql增删改查的集合,假设程序中的一个功能涉及到对数据库的 ...
- OpenCASCADE中散乱Edge生成Wire
OpenCASCADE中散乱Edge生成Wire eryar@163.com Abstract. In OpenCASCADE a wire can be built from any number ...
- solr 亿万级数据查询性能測试
废话不多说,我电脑配置 i7四核cpu 8G内存 插入数据文档中有5个字段,当中有两个分词.一个int,一个date 批量插入測试一次10万循环10次总共100万用时85秒 批量插入測试一次10万循环 ...
- Icomparer和Icomparable集合排序
c#中实现对象集合的排序可以使用ArrayList中的Sort()方法,而有比较才能谈排序,因为不是基本类型(如string ,int.double......等)所以.NET Framework不可 ...
- Torch 的安装与基本用法
本文安装仅限 ubuntu 系统.官方文档见:Getting started with Torch. 0. 简介 Torch 使用轻量级脚本语言 Lua 及其 C/CUDA 扩展模块实现,底层数值计算 ...
- 71.用express框架,出现 express.Router is not a function
Express版本太久
- ElasticSearch、Kibana Web管理
ElasticSearch的Web管理 http://localhost:9200/ http://localhost:9200/cluster/health?pretty http://localh ...
- 上市公司恋上互联网金融 目前已有14家涌入P2P
时至今日,互联网金融已蔚然成风,诸多上市公司正前赴后继介入到P2P业务中,据记者初步统计,目前至少有14家A股上市公司参与了P2P业务.央行6月份的报告显示,中国当前有600多家P2P公司,交易额达到 ...