1、環(huán)境介紹
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>
說明:
3、集成tomcat測(cè)試的方法
方法一(推薦):
運(yùn)行mvn tomcat:run命令,啟動(dòng)內(nèi)嵌的tomcat進(jìn)行測(cè)試
1) 可在命令行直接運(yùn)行mvn tomcat:run命令
2) 如果使用eclipse,可通過maven的eclipse插件m2e新增一個(gè)maven build運(yùn)行tomcat:run命令,如圖:
方法二:
運(yùn)行mvn tomcat:redeploy命令,將web應(yīng)用發(fā)布到外部已啟動(dòng)的tomcat進(jìn)行測(cè)試
1) 在maven的文件里加入如下設(shè)置:
- <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) 啟動(dòng)tomcat
4) 類似方法一那樣執(zhí)行tomcat:redeploy命令
方法三(不推薦):
增加eclipse web工程支持,利用傳統(tǒng)的eclipse集成tomcat測(cè)試的方法進(jìn)行測(cè)試。此方法其實(shí)跟maven和tomcat-maven-plugin無關(guān),發(fā)布到tomcat下測(cè)試的war包不一定跟maven構(gòu)建的包一致,因此不推薦。
1) 在工程屬性里的Project Facets里勾上Dynamic Web Module,如下圖:
2) 在Deployment Assembly增加將maven依賴發(fā)布到“WEB-INF/lib”下,并將“/src/test/java”和“/src/test/resources”兩項(xiàng)刪除(因?yàn)閙aven打包時(shí)是不會(huì)打包這兩部分內(nèi)容的)
3) 這樣便可以利用傳統(tǒng)的eclipse集成tomcat測(cè)試的方法進(jìn)行測(cè)試了,如下圖:
補(bǔ)充:
如果是多模塊工程,只需在父工程下運(yùn)行tomcat-maven-plugin的goal即可。如:D:\<父工程目錄>>tomcat:run。tomcat-maven-plugin會(huì)自動(dòng)檢測(cè)和替換相關(guān)的依賴。
- 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.
更多關(guān)于tomcat-maven-plugin的使用,請(qǐng)參考:
http://mojo.codehaus.org/tomcat-maven-plugin/index.htmlhttp://tomcat.apache.org/maven-plugin-2/index.html