深入理解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及应用(一):生命周期和插件的更多相关文章
- maven的仓库、生命周期与插件
一.仓库 统一存储所有Maven项目共享的构建的位置就是仓库. 仓库分为本地仓库和远程仓库.远程仓库又分为中央仓库(中央仓库是Maven核心自带的远程仓库),伺服(另一种特殊的远程仓库,为节省宽带和时 ...
- maven详解之生命周期与插件
Maven是一个优秀的项目管理工具,它能够帮你管理编译.报告.文档等. Maven的生命周期: maven的生命周期是抽象的,它本身并不做任何的工作.实际的工作都交由"插件"来完成 ...
- maven第7章生命周期和插件
maven插件用到哪些思想? 7.7 从命令行调用插件 目标前缀和插件前缀是一个意思. 在本地搭建maven环境,熟悉maven的环境.
- Maven系列第6篇:生命周期和插件详解,此篇看过之后在maven的理解上可以超越同级别90%的人!
maven系列目标:从入门开始开始掌握一个高级开发所需要的maven技能. 这是maven系列第6篇. 整个maven系列的内容前后是有依赖的,如果之前没有接触过maven,建议从第一篇看起,本文尾部 ...
- Maven学习总结(16)——深入理解maven生命周期和插件
在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最近实在受不了乌龟一样的b ...
- (十二)Maven生命周期和插件
除了坐标.依赖以及仓库之外,Maven的另外两个核心概念是生命周期和插件.在有关Maven的日常使用中,命令行的输入往往就对应了生命周期,如mvn package就表示执行默认生命周期阶段packag ...
- 05 Maven 生命周期和插件
Maven 生命周期和插件 除了坐标.依赖以及仓库之外, Maven 另外两个核心概念是生命周期和插件.在有关 Maven 的日常使用中,命令行的输入往往就对应了生命周期,如 mvn package ...
- Maven核心概念之仓库,生命周期与插件
宏观图 一.仓库 统一存储全部Maven项目共享的构建的位置就是仓库. 仓库分为本地仓库和远程仓库.远程仓库又分为中央仓库(中央仓库是Maven核心自带的远程仓库),伺服(还有一种特殊的远程仓库,为节 ...
- Maven 生命周期 和插件
1.3 生命周期1.3.1 什么是生命周期? Maven生命周期就是为了对所有的构建过程进行抽象和统一.包括项目清理.初始化.编译.打包.测试.部署等几乎所有构建步骤. 生命周期可以理解为构建工程的步 ...
随机推荐
- springMVC 定时器配置
1.在springMVC中加入 xmlns:task="http://www.springframework.org/schema/task" http://www.springf ...
- 洛谷P4894 GodFly求解法向量
如果没有学过向量相关知识请出门右转高中数学必修四~~~ 当然如果你和我一样也是小学生我也不反对 首先说结论:\(\vec{z}=(y1z2-y2z1,z1x2-z2x1,x1y2-x2y1)\) 其实 ...
- docker 下修改 mysql sql_mode和配置文件
原文:docker 下修改 mysql sql_mode和配置文件 打开PowerShell 首先创建mysql容器,这里我们指定使用mysql5.7的版本 docker run -d -p 3306 ...
- Dynamics CRM2013 Form利用window.location.reload()进行全局刷新带来的问题及解决的方法
CRM2013以后.表单的保存后变成了局部刷新而非全局刷新,但非常多情况下我们须要刷新整个页面.通过刷新页面来使脚本运行或者业务规则运行来实现某些业务效果,一般我们会使用window.location ...
- Ignatius and the Princess III(杭电1028)(母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Git(三):加入与提交
在这一节.接着使用上一节的代码样例往下讲,http://blog.csdn.net/troy__/article/details/39806245. 加入文件到暂存区 加入新文件和改动版本 ...
- bzoj1003: [ZJOI2006]物流运输(DP+spfa)
1003: [ZJOI2006]物流运输 题目:传送门 题解: 可以用spfa处理出第i天到第j都走这条路的花费,记录为cost f[i]表示前i天的最小花费:f[i]=min(f[i],f[j-1] ...
- bzoj1002: [FJOI2007]轮状病毒(基尔霍夫矩阵)
1002: [FJOI2007]轮状病毒 题目:传送门 题解: 决定开始板刷的第一题... 看到这题的时候想:这不就是求有多少种最小生成树的方式吗? 不会啊!!!%题解... 什么鬼?基尔霍夫矩阵?? ...
- php中的页面跳转和重定向
php中的页面跳转和重定向 ThinkPHP中跳转和重定向的区别 跳转: 浏览器认为: 当前URL请求成功, 重新请求新的URL. 浏览器会 记录当前的URL 和 新的URL 在请求历史记录中. 回退 ...
- Python开源爬虫项目代码:抓取淘宝、京东、QQ、知网数据--转
数据来源:数据挖掘入门与实战 公众号: datadw scrapy_jingdong[9]- 京东爬虫.基于scrapy的京东网站爬虫,保存格式为csv.[9]: https://github.co ...