本文共 12252 字,大约阅读时间需要 40 分钟。
本篇来介绍Maven的生命周期和前面介绍过几个命令的详细介绍过程。先介绍生命周期,生命周期中不同阶段就包括了一些命令,所以理解了生命周期,再来看看每一个命令的详细使用过程。
1.Maven的生命周期分类
Maven生命周期定义了各个构建环节的执行顺序,有了这个清单,Maven就可以自动化执行构建命令了。
Maven有三套相互独立的生命周期,分别是
1)Clean Lifecycle 在进行真正的构建之前进行一些清理工作 2)Default Lifecycle: 构建的核心部分,包含 编译,测试,打包,安装和部署等。 3)Site Lifecycle: 生成项目报告,站点,发布站点。他们是相互独立的,你可以仅仅执行mvn clean来清理工作目录,仅仅调用mvn site来生成站点。也可以这样执行mvn clean install site运行所有这三套生命周期。
2.不同生命周期的不同阶段
先看看Clean Lifecycle生命周期的不同阶段。
Clean生命周期一共包含三个阶段: 1)pre-clean: 执行一些需要在clean之前完成的工作 2)clean:移除所有上一次构建生成的文件 3)post-clean: 执行一些需要在clean之后立刻完成的工作site的生命周期:
1)pre-site:执行一些需要在生成站点文档之前完成的工作。 2)site:生成项目的站点文档 3)post-site:执行一些需要在生成站点文档之后完成的工作,为部署做准备 4)site-deploy:将生成的站点文档部署到特定的服务器上。 工作中可能经常用到的是site阶段和site-deploy阶段,用来生成和发布maven站点,这个功能相当强大,经理比较喜欢这个,文档和统计数据自动生成,很好看。Default生命周期是Maven生命周期中最重要的一个,绝大部分工作都发生在这个生命周期中。
这里只列出一部分重要和常用的阶段。 1)validate 包含generate-sources和process-source两个动作,也就是复制并处理资源文件,至目标目录,准备打包 2)compile 编译项目源码 包含process-class generate-test-sources generate-test-resources process-test-resources 复制并处理资源文件,至目标测试目录。 3)test-compile:编译测试源代码 包含process-test-classes 和prepare-package 4)package 接受编译好的代码,打包成jar或者war 包含pre-integration-test和integration-test和verify 5)install 将包安装到本地仓库,以让其他项目依赖。 6)deploy 将最终的包复制到远程的仓库,以让其他开发人员与项目共享或部署到服务器上运行。
3.具体命令介绍环境准备
我们前面提到了pom.xml,但是并没有解释什么是pom,是英文单词Project Object Model,也就是项目对象模型。我们学习过DOM(文档对象模型),POM(页面对象模型)。pom.xml文件是整个maven项目的核心,几乎我们maven项目的全部配置和依赖都在这个pom.xml文件中进行设置。这个文件的重要性可以和web工程中web.xml文件一样类比。
maven是这样设计的,当输入mvn 构建相关的命令,maven会自动去下载和这个任务相关的插件,maven核心组件并没有提前下载这些插件,所以第一次构建都需要下载这个构建所有任务相关的插件。
在Eclipse中创建一个maven的普通java工程。
主程序下代码
package hello;public class Hello { public String sayHello(String name) { return "Hello "+name; }}
测试程序代码
package cases;import static org.junit.Assert.assertEquals;import org.junit.Test;import hello.Hello;public class HelloTest { Hello hello = new Hello(); @Test public void test1() { String result = hello.sayHello("Anthony"); assertEquals("Hello Anthony", result); }}
pom.xml文件内容
4.0.0 com.anthony.maven HelloDemo 0.0.1-SNAPSHOT junit junit 4.12 test
右键工程名称,maven-Update projects...,选择强制更新,junit依赖包就可以下载到本地仓库,测试代码中导入包语句就没问题。
4.编译命令: mvn compile和mvn test-compile
本来按照顺序,我们先介绍mvn clean清理命令。但是我们是一个干净的项目,前面没有做过编译构建操作,所以不存在清除这个动作,清除放在第二个命令来介绍。
注意一点,当我们执行和构建相关的maven命令的时候,一定要先cd到pom.xml所在的文件夹,也就是该工程的根目录。
先cd到maven工程的根目录,我发现当前没有target,如果执行了编译,就会产生target文件夹,而且文件夹下有classes或者test-classes文件夹。
先来执行mvn compile 也就是编译主程序,看看在target下的classes下有没有这个编译之后的class字节码文件。
上面执行mvn compile,显示构建成功,然后到target下的classes文件下发现有和主程序一样的包名结构下出现了一个Hello.class的字节码文件。
简单来看看cmd这个编译产生的日志,开始是扫描项目,根据上面default Lifecycle的知识,本来是先执行validate阶段,然后执行编译。上面日志,有显示(绿色字体)执行了下面两个阶段
maven-resources-plugin:2.6:resourcesmaven-compiler-plugin:3.1:compile
第一个是资源相关的插件,负责拷贝资源到相关路径。第二个插件是编译插件。这里说下maven-resources-plugin:2.6:resources这个格式,前面maven-resources-plugin:2.6这段叫插件名称和版本,后面resources这个是一个动作,也算一个命令,这个叫目标。怎么理解这个插件的目标,这里就是调用该插件执行resources这个命令,这就是目标。这种格式我们下下面命令会看到不同插件名称和目标。
上面清楚了主程序的编译过程和产生字节码的过程,下面看看测试程序代码的编译过程。
没执行mvn test-compile命令之前,target下是没有test-classes文件夹的。
然后我们来执行mvn test-compile
再来看看test-comiple的编译日志,记录下用了哪些插件,执行了哪些阶段的任务(绿色字体)
maven-resources-plugin:2.6:resourcesmaven-compiler-plugin:3.1:compilemaven-resources-plugin:2.6:testResourcesmaven-compiler-plugin:3.1:testCompile
同样的,前面是插件名称和版本,后面是插件的目标,或者执行动作。翻译过来的意思就是,复制主程序下的资源文件到相关路径(如果有就复制),执行主程序编译。复制测试下的资源文件到相关路径,执行测试程序编译。
5.清理命令:mvn clean
前面我们介绍了主程序的编译和测试程序的编译,这下我们有环境来演示mvn clean的操作。
清除命令之前,target的文件目录是这样。
执行以下mvn clean命令
执行清除命令之后,效果就是把项目根目录下的target目录以及里面的文件全部都删除了。当前这个清除日志就调用了一个插件执行任务。
maven-clean-plugin:2.5:clean
根据上面知识,我们知道执行了mvn clean就走完了一遍Clean Lifecycle这种类型的生命周期。
6.运行测试命令:mvn test
maven自动化构建是因为自己设计了一套命令的执行顺序。在default生命周期内,如果直接执行mvn test,那么maven会直接帮我们执行clean 和 compile和test-compile这些命令。下面我们通过执行mvn test来演示这个结论。
我在执行mvn test过程下载好几个插件,这也体现了不同命令调用不同插件执行任务特定,而且mven发现本地仓库没有这个插件就会去中央仓库下载。
如果下载过程中由于网络不行或者网络中断,那么这次构建就会显示失败,可以再来一次mvn test, 这个test过程所依赖的插件有很多,所以下载很慢。
下面我直接把我cmd中运行mvn test成功的日志拿出来看看。
C:\Users\Anthony\eclipse-workspace\HelloDemo>mvn test[INFO] Scanning for projects...[INFO][INFO] --------------------< com.anthony.maven:HelloDemo >---------------------[INFO] Building HelloDemo 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloDemo ---[INFO] Nothing to compile - all classes are up to date[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloDemo ---[INFO] Nothing to compile - all classes are up to date[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloDemo ---[INFO] Surefire report directory: C:\Users\Anthony\eclipse-workspace\HelloDemo\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running cases.HelloTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.091 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.882 s[INFO] Finished at: 2019-09-08T14:50:19+08:00[INFO] ------------------------------------------------------------------------C:\Users\Anthony\eclipse-workspace\HelloDemo>
上面测试包含测试报告输出,告诉我们执行了一个用例,失败和报错都是0。下面再看看执行了哪些插件的任务。
maven-resources-plugin:2.6:resourcesmaven-compiler-plugin:3.1:compilemaven-resources-plugin:2.6:testResourcesmaven-compiler-plugin:3.1:testCompilemaven-surefire-plugin:2.12.4:test
7.安装命令:mvn install
这里我们先执行下mvn clean,清除前面产生的编译文件。
C:\Users\Anthony\eclipse-workspace\HelloDemo>mvn install[INFO] Scanning for projects...[INFO][INFO] --------------------< com.anthony.maven:HelloDemo >---------------------[INFO] Building HelloDemo 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloDemo ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to C:\Users\Anthony\eclipse-workspace\HelloDemo\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloDemo ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to C:\Users\Anthony\eclipse-workspace\HelloDemo\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloDemo ---[INFO] Surefire report directory: C:\Users\Anthony\eclipse-workspace\HelloDemo\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running cases.HelloTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.095 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloDemo ---[INFO] Building jar: C:\Users\Anthony\eclipse-workspace\HelloDemo\target\HelloDemo-0.0.1-SNAPSHOT.jar[INFO][INFO] --- maven-install-plugin:2.4:install (default-install) @ HelloDemo ---[INFO] Installing C:\Users\Anthony\eclipse-workspace\HelloDemo\target\HelloDemo-0.0.1-SNAPSHOT.jar to C:\Users\Anthony\.m2\repository\com\anthony\maven\HelloDemo\0.0.1-SNAPSHOT\HelloDemo-0.0.1-SNAPSHOT.jar[INFO] Installing C:\Users\Anthony\eclipse-workspace\HelloDemo\pom.xml to C:\Users\Anthony\.m2\repository\com\anthony\maven\HelloDemo\0.0.1-SNAPSHOT\HelloDemo-0.0.1-SNAPSHOT.pom[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.554 s[INFO] Finished at: 2019-09-08T14:53:42+08:00[INFO] ------------------------------------------------------------------------C:\Users\Anthony\eclipse-workspace\HelloDemo>
再来看看mvn install调用了哪些插件,执行了哪些任务。
maven-resources-plugin:2.6:resourcesmaven-compiler-plugin:3.1:compilemaven-resources-plugin:2.6:testResourcesmaven-compiler-plugin:3.1:testCompilemaven-surefire-plugin:2.12.4:testmaven-jar-plugin:2.4:jarmaven-install-plugin:2.4:install
这里多了jar-plugin,是因为我忘记介绍了mvn package这个命令。下面我来执行以下mvn package命令
C:\Users\Anthony\eclipse-workspace\HelloDemo>mvn package[INFO] Scanning for projects...[INFO][INFO] --------------------< com.anthony.maven:HelloDemo >---------------------[INFO] Building HelloDemo 0.0.1-SNAPSHOT[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ HelloDemo ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to C:\Users\Anthony\eclipse-workspace\HelloDemo\target\classes[INFO][INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ HelloDemo ---[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent![INFO] Copying 0 resource[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ HelloDemo ---[INFO] Changes detected - recompiling the module![WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent![INFO] Compiling 1 source file to C:\Users\Anthony\eclipse-workspace\HelloDemo\target\test-classes[INFO][INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ HelloDemo ---[INFO] Surefire report directory: C:\Users\Anthony\eclipse-workspace\HelloDemo\target\surefire-reports------------------------------------------------------- T E S T S-------------------------------------------------------Running cases.HelloTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.075 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO][INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ HelloDemo ---[INFO] Building jar: C:\Users\Anthony\eclipse-workspace\HelloDemo\target\HelloDemo-0.0.1-SNAPSHOT.jar[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 3.103 s[INFO] Finished at: 2019-09-08T15:33:50+08:00[INFO] ------------------------------------------------------------------------C:\Users\Anthony\eclipse-workspace\HelloDemo>
目前我们介绍了以下命令
其中mvn deploy命令我们暂时没有介绍。从上面每个命令我收集的插件调用来看,我们可以发现一个规律。
执行mvn test,会自动帮我们执行mvn compile和mvn test-compile。
执行mvn package,会自动帮我们执行mvn test
执行mvn install,会自动执行mvn package。
越往下走执行的命令,都会包含前面命令的全部阶段。这种设计思想就是maven能够自动化构建的核心。这个设计思想,也可以解释我在前面文章,为什么使用mvn clean install就完成API报告获取和TestNG用例执行。
转载地址:http://cqxws.baihongyu.com/