maven集成tomcat进行web应用测试

1、环境介绍
maven:apache-maven-3.0.3
tomcat:apache-tomcat-6.0.32
jdk:jdk1.5.0_11
eclipse:3.7.2

2、配置tomcat-maven-plugin
在pom.xml中加入如下配置
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>tomcat-maven-plugin</artifactId>
				<version>1.1</version>
				<configuration>
					<server>tomcat-6.0.32</server>
					<url>http://127.0.0.1:8080/manager</url>
					<uriEncoding>${encoding}</uriEncoding>
					<warSourceDirectory>WebContent</warSourceDirectory>
				</configuration>
			</plugin>


说明:
  • tomcat-maven-plugin现在已拆分成tomcat7-maven-plugin和tomcat6-maven-plugin了,而groupId也由org.codehaus.mojo改为org.apache.tomcat.maven。
  • 详情可参考: http://tomcat.apache.org/maven-plugin-2.0/


3、集成tomcat测试的方法
方法一(推荐):
运行mvn tomcat:run命令,启动内嵌的tomcat进行测试
1) 可在命令行直接运行mvn tomcat:run命令
2) 如果使用eclipse,可通过maven的eclipse插件m2e新增一个maven build运行tomcat:run命令,如图:




方法二:
运行mvn tomcat:redeploy命令,将web应用发布到外部已启动的tomcat进行测试
1) 在maven的文件里加入如下设置:
				<server>
					<id>tomcat-6.0.32</id>
					<username>tomcat</username>
					<password>tomcat</password>
				</server>

2) 在tomcat/conf的tomcat-users.xml加入如下配置:
	<role rolename="manager-gui"/>

	<role rolename="manager-script"/>

	<user username="tomcat" password="tomcat" roles="manager-gui, manager-script"/>

3) 启动tomcat
4) 类似方法一那样执行tomcat:redeploy命令

方法三(不推荐):
增加eclipse web工程支持,利用传统的eclipse集成tomcat测试的方法进行测试。此方法其实跟maven和tomcat-maven-plugin无关,发布到tomcat下测试的war包不一定跟maven构建的包一致,因此不推荐。
1) 在工程属性里的Project Facets里勾上Dynamic Web Module,如下图:



2) 在Deployment Assembly增加将maven依赖发布到“WEB-INF/lib”下,并将“/src/test/java”和“/src/test/resources”两项删除(因为maven打包时是不会打包这两部分内容的)



3) 这样便可以利用传统的eclipse集成tomcat测试的方法进行测试了,如下图:



补充:
如果是多模块工程,只需在父工程下运行tomcat-maven-plugin的goal即可。如:D:\<父工程目录>>tomcat:run。tomcat-maven-plugin会自动检测和替换相关的依赖。
NOTE If you have a multi module Maven projects and use Maven3, you don't need to install all modules before use the run goal, just use tomcat6/7:run from the root module and the plugin will auto detect build output directory from various modules and replace dependencies with those directories in the webapp classloader.


更多关于tomcat-maven-plugin的使用,请参考:
http://mojo.codehaus.org/tomcat-maven-plugin/index.html
http://tomcat.apache.org/maven-plugin-2/index.html

猜你喜欢

转载自vivus.iteye.com/blog/1561664
今日推荐