Maven的生命周期和插件(五)

前面咱们已经讲过坐标、依赖以及仓库,Maven的另外两个核心概念是生命周期和插件。生命周期和插件两者协同工做,密不可分。 java

1. Maven生命周期基本概念

1) Maven的生命周期就是为了对全部的构建过程进行抽象和统一。Maven总结了一套高度完善的、易扩展的生命周期,包括了项目的清理、初始化、编译、测试、打包、集成测试、验证、部署和站点生成等几乎全部的构建步骤。 apache

2) Maven的生命周期是抽象的,生命周期自己不作任何实际的工做,在Maven的设计中,实际的任务都交给插件来完成。其实这种思想和设计模式中的模版方法(Template Method)很类似。 设计模式

2. 生命周期详解

1)三套生命周期 服务器

Maven有三套相互独立的生命周期,分别为clean、default和site。clean生命周期的目的是清理项目,default生命周期的目的是构建项目,而site生命周期的目的是创建项目站点。 app

2)clean生命周期 框架

clean生命周期的目的是清理项目,它包括以下三个阶段: eclipse

a)pre-clean:执行一些清理前须要完成的工做。 maven

b)clean:清理上一次构建的文件。 post

c)post-clean:执行一些清理后须要完成的工做。 单元测试

3) default生命周期

default生命周期定义了真正构建时所须要执行的全部步骤,它是全部生命周期中最核心的部分。

a)validate

b)initialize

c)generate-sources

d)process-sources

e)generate-resources

f) process-resources

g)compile:编译项目的主源码,即编译src/main/java目录下的Java文件至项目输出的主classpath中。

h)process-classes

i)generate-test-sources

j)process-test-sources

k)generate-test-resources

l)process-test-resources

m)test-compile:编译项目的测试代码,即编译src/test/java目录下的Java文件至项目输出的测试classpath目录中。

n)process-test-classes

o)test:使用单元测试框架运行测试。

p)prepare-package

q)package:接受编译好的代码,打包成可发布的格式。如:.jar

r)pre-integration-test

s)integration-test

t)post-integration-test

u)verify

v)install:将包安装到Maven本地仓库,供本地其余Maven项目使用。

w)deploy:将最终的包复制到远程仓库,供其余开发人员和Maven项目使用。

4) site生命周期

site生命周期的目的是创建和发布项目站点。

a)pre-site:执行一些在生成项目站点以前须要完成的工做。

b)site:生成项目站点文档。

c)post-site:执行一些在生成项目站点以后须要完成的工做。

d)site-deploy:将生成的项目站点发布到服务器上。

5)Window下的命令行执行Maven任务

主要是调用Maven的生命周期的阶段,如下几个经常使用的命令:

mvn clean、mvn test、mvn clean install、mvn clean deploy site-deploy。

3. 插件详解

1)插件目标

咱们已经知道,Maven的核心仅仅定义了抽象的生命周期,具体的任务是交给插件完成的,插件以独立的构件形式存在的。对于插件自己,为了可以复用代码,它每每可以完成多个任务。每一个任务就是一个插件目标。

2) 插件绑定

Maven的生命周期与插件是相互绑定的,用以完成实际的构建任务。具体而言,是生命周期的阶段和插件的目标相互绑定,以完成一项具体的任务。如:项目编译这一任务,它对应了default生命周期中的compile这一阶段,而maven-compiler-plugin这一插件的compile目标可以完成该任务。

3)内置绑定

Maven为一些主要的生命周期阶段绑定了不少插件目标。好比:clean生命周期中的clean阶段,与maven-clean-plugin:clean绑定。

下面我对hello-world项目执行mvn clean install命令,(本人直接使用eclipse执行,命令为clean install),看下面执行了哪些插件目标:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hello-world ---
[INFO] Deleting D:\code\maven\hello-world\target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to D:\code\maven\hello-world\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Surefire report directory: D:\code\maven\hello-world\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.alvinliang.maven.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.067 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] Building jar: D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---
[INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.715s
[INFO] Finished at: Mon Jan 20 22:28:09 CST 2014
[INFO] Final Memory: 16M/167M
[INFO] ------------------------------------------------------------------------

4)自定义绑定

除了内置绑定外,用户能够本身选择将某个插件目标绑定到生命周期的某个阶段上。例如:建立项目的源码,没有对应的内置绑定,这时候就须要用户本身自定义绑定。

4. 插件的配置

1)命令行插件配置

用户能够在Maven命令中使用-D参数,并伴随一个参数键=参数值的形式,来配置插件的参数。

如:mvn install -Dmaven.test.skip = true

2)Eclipse中执行 install -Dmaven.test.skip = true,结果以下:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-world 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-world ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\code\maven\hello-world\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-world ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ hello-world ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello-world ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-world ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ hello-world ---
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ hello-world ---
[INFO] Installing D:\code\maven\hello-world\target\hello-world-0.0.1-SNAPSHOT.jar to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\code\maven\hello-world\pom.xml to D:\library\maven\repository\com\alvinliang\maven\hello-world\0.0.1-SNAPSHOT\hello-world-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.481s
[INFO] Finished at: Mon Jan 20 22:35:59 CST 2014
[INFO] Final Memory: 13M/100M
[INFO] ------------------------------------------------------------------------

5. 使用maven-help-plugin描述插件

在cmd中执行mvn help:describe -Dplugin=compiler,结果以下:

[INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom ---
[INFO] org.apache.maven.plugins:maven-compiler-plugin:3.1

Name: Maven Compiler Plugin
Description: The Compiler Plugin is used to compile the sources of your
  project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-compiler-plugin
Version: 3.1
Goal Prefix: compiler

This plugin has 3 goals:

compiler:compile
  Description: Compiles application sources

compiler:help
  Description: Display help information on maven-compiler-plugin.
    Call mvn compiler:help -Ddetail=true -Dgoal=<goal-name> to display
    parameter details.

compiler:testCompile
  Description: Compiles application test sources.

For more information, run 'mvn help:describe [...] -Ddetail'
相关文章
相关标签/搜索