其实用maven已经一年多了,我们的自动化测试代码一年多以前就开始用maven管理了,之所以现在才写这篇笔记,是觉得有些东西虽然常常在用,还是得不时的温故温故,以免忘了。
当初选择maven,也没想那么多,主要是它管理jar比较方便,不像之前用ant的时候(后来才知道用ivy和ant配合,也可轻松管理依赖jar包:你在一个默认命名为ivy.xml的文件中为你的项目指定了依赖项。Ivy将会把所有列在ivy.xml文件中的依赖项下载到一个本地缓存目录中),组内每个人从服务器下载自动化代码后,可能都要更新一下本地的jar包,以免由于jar包未升级对所依赖的jar包不兼容等等原因,造成自动化代码报错。
maven的出现,解决了这一难题。它可以对项目依赖的jar包进行管理,可以让你的项目保持基本的依赖,排除冗余jar包,并且可以让你非常轻松的对依赖的jar包进行版本升级。而这些仅仅是maven最基本的功能,它可以在这基础上对项目进行清理、编译、测试、打包、发布等等构建项目的工 作。。
不过其实我们自动化测试所用到的maven的功能并不多,但是这并不妨碍它是个很强大的项目管理工具的事实。
下面开始一一介绍maven的功能(具体怎么安装maven插件,怎么创建maven项目就不说了)
一。POM(Project Object Model)
每次新建一个maven项目,都会生成一个pom.xml文件。这个文件里面主要放置该项目的信息,以及所依赖包的信息。而每个依赖包都会有相应的坐标,maven就是通过这个坐标来找到这些依赖包,并对他们进行管理的。
上面的是这个项目的信息:
groupId:表明其所属组织或公司及其所属项目。
artifactId:项目的模块名
version:当前项目的版本号
packaging:定义项目的打包方式。
dependencies里面存放的是这个项目所依赖的依赖包。同样的,每个依赖包里面都会存放相应的groupId,artifactId,version,scope(指定依赖的范围)
二:maven库
上面说过pom文件里面的坐标是为了方便maven找到依赖的包,下面就具体分析下:
1.将groupId转化成路径,把里面的包名分隔符(.)转化成路径分隔符(/):如上面的com.qiuwy.mavenDemo就转换成com/qiuwy/mavenDemo
2.将artifactId转换成路径:在groupId转化的路径上拼接上artifactId。故:com/qiuwy/mavenDemo/myTest
3.将version转换成路径:在artifactId转化的路径上拼接上version。即:com/qiuwy/mavenDemo/myTest/0.0.1-SNAPSHHOT
4.将artifactId和version拼接生成依赖包文件名。如这里就是
5.packeging就是生成的依赖包文件名的后缀。如上面的就是
于是按照这些坐标就可以找到这个依赖包在仓库里的具体位置了com/qiuwy/mavenDemo/myTest/0.0.1-SNAPSHHOT/ myTest-0.0.1-SNAPSHHOT.jar
而maven的仓库又分为本地仓库和远程仓库。
当maven根据这些坐标去寻找依赖包时,他会先去本地仓库寻找,如果本地仓库找不到,它会自动去远程仓库寻找,找到后,再下载到本地仓库。最后在classpath文件里会自动生成这个依赖包的路径。
下面说说本地仓库和远程仓库。
本地仓库:就是我们设置的maven在我们本机的目录。默认是在目录:C:Users用户.m2 epository。比如我们刚才说的项目生成的依赖包所在的位置就是
远程仓库包括中央仓库、私服、其他公共仓库。
中央仓库地址是:http://repo.maven.apache.org/maven2
三。生命周期
maven很强大的还有一个原因就是它自己有一个很完整的生命周期,生命周期里预定义了很多的阶段,有很多插件依附于这些阶段。如果你想进入maven生命周期的某个阶段,你只需要调用相应的命令,maven就会自动去调用这些插件去一步步执行,达到你想要的生命周期阶段。而不需要像ant一样,需要我们写清楚每一步的步骤是什么。
比如说:
mvn clean
就是清除项目,把target里面的文件都删除
测试结果如下:
maven test
会执行src/test/java 下的Junit 测试代码,并把测试结果生成报表放在targetsurefire-reports目录下。测试代码不会被打包部署
当然在执行测试之前会自动执行编译命令,运行结果如下图:
测试结果如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrcmain esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrc est esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myTest ---
[INFO] Surefire report directory: C:Usersqiuwyworkspace2myTest argetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.qiuwy.mavenDemo.myTest.TryTest
--setup---
--testAdd---
--teardown---
--setup---
--testMinus---
--teardown---
--setup---
--eee---
--teardown---
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.056 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.517s
[INFO] Finished at: Thu Nov 20 16:49:50 CST 2014
[INFO] Final Memory: 7M/120M
[INFO] ------------------------------------------------------------------------
mvn package:
会将项目打成jar包,并放在target目录中
执行此命令之前会先执行编译和测试命令
测试结果如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrcmain esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrc est esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myTest ---
[INFO] Surefire report directory: C:Usersqiuwyworkspace2myTest argetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.qiuwy.mavenDemo.myTest.TryTest
--setup---
--testAdd---
--teardown---
--setup---
--testMinus---
--teardown---
--setup---
--eee---
--teardown---
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myTest ---
[INFO] Building jar: C:Usersqiuwyworkspace2myTest argetmyTest-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.858s
[INFO] Finished at: Thu Nov 20 16:53:46 CST 2014
[INFO] Final Memory: 8M/120M
[INFO] ------------------------------------------------------------------------
mvn install:
把项目jar包安装到本地仓库中,以便其他项目使用
执行此命令之前会先执行编译,测试,打包命令
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building myTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrcmain esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest argetclasses
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myTest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:Usersqiuwyworkspace2myTestsrc est esources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ myTest ---
[INFO] Compiling 1 source file to C:Usersqiuwyworkspace2myTest arget est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myTest ---
[INFO] Surefire report directory: C:Usersqiuwyworkspace2myTest argetsurefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.qiuwy.mavenDemo.myTest.TryTest
--setup---
--testAdd---
--teardown---
--setup---
--testMinus---
--teardown---
--setup---
--eee---
--teardown---
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myTest ---
[INFO] Building jar: C:Usersqiuwyworkspace2myTest argetmyTest-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ myTest ---
[INFO] Installing C:Usersqiuwyworkspace2myTest argetmyTest-0.0.1-SNAPSHOT.jar to C:Usersqiuwy.m2 epositorycomqiuwymavenDemomyTest .0.1-SNAPSHOTmyTest-0.0.1-SNAPSHOT.jar
[INFO] Installing C:Usersqiuwyworkspace2myTestpom.xml to C:Usersqiuwy.m2 epositorycomqiuwymavenDemomyTest .0.1-SNAPSHOTmyTest-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.919s
[INFO] Finished at: Thu Nov 20 16:52:33 CST 2014
[INFO] Final Memory: 8M/120M
[INFO] ------------------------------------------------------------------------