首先說說本身爲啥要用maven管理項目,一個直接的緣由是:我在本身電腦上開發web項目,每次部署到服務器上時都要經歷以下步驟:html
cd /var/lib/tomcat7/webapps sudo rm XXX.war sudo rm -rf XXX
pscp -pw "xxx" XXX.war username@ip:/var/lib/tomcat7/webapps
sudo service tomcat7 restart
每次都這些步驟,很是煩人,而用maven來管理就不須要這些步驟啦,直接在Eclipse裏配置maven插件,而後使用maven來自動部署項目,關於怎麼自動部署可網上不少教程,具體可參看後面的參考資料,部署成功後只須要用一個命令便可自動將個人web項目部署到tomcat服務器上,我通常用下面這樣的命令:nginx
mvn tomcat7:deploy -Dmaven.test.skip=true
其中-Dmaven.test.skip=true表示臨時性跳過測試代碼的編譯(也可用-DskipTests表示跳過測試階段),maven.test.skip同時控制maven-compiler-plugin和maven-surefire-plugin兩個插件的行爲,即跳過編譯,又跳過測試。web
但初次按着教程來老是遇到各類問題, 下面記錄我在部署過程當中遇到的各類問題及注意事項,以提供參考意義。sql
maven配置文件pom.xml裏的tomcat插件通常像下面這樣配置:shell
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <path>/test</path> <url>http://localhost:8080/manager/</url> <server>tomcat</server> </configuration> </plugin>
裏面的server須要在maven的配置文件settings.xml裏配置以下:apache
<server> <id>tomcat</id> <username>admin</username> <password>123456</password> </server>
這裏的username和password通常爲tomcat server的用戶名和密碼。vim
開始運行自動部署命令時,必定要先啓動tomcat。不然會報下列錯誤:tomcat
[INFO]
[INFO] --- tomcat-maven-plugin:1.0:redeploy (default-cli) @ SSHMJ-FRANK --- [INFO] Deploying war to http://localhost:8080/SSHMJ-FRANK [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 9.630s [INFO] Finished at: Tue Aug 31 16:35:52 CST 2010 [INFO] Final Memory: 6M/15M [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.0:redeploy (default-cli) on project SSHMJ-FRANK: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
HTTP 403錯誤ruby
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat-maven-plugin: 1.1: deploy (default-cli) on project XXX: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/html/deploy?path=XXX -> [Help 1]
網上有人說產生該問題有可能由於兩個緣由:服務器
1)若是使用的是Tomcat 7,須要修改pom.xml中部署的url地址,將
<url> http://localhost:8080/manager </url>
改成
<url> http://localhost:8080/manager/text </url>
2)tomcat用戶權限分配問題,須要同時具有manager-gui和manager-script權限,好比忘了分配manager-script權限。
正確的conf/tomcat-users.xml配置應爲:
<tomcat-users> <role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin」 password="admin" roles="manager-gui, manager-script"/> </tomcat-users>
不過個人問題都不是上面兩個,個人問題是自動部署命令寫錯了,應該是mvn tomcat7:deploy命令,而我以前用的是mvn tomcat:deploy命令
「Application already exists at path」問題 使用tomcat7-maven-plugin插件部署到tomcat服務器時,當服務器上已經有相同名字的項目就會致使
FAIL - Application already exists at path ...
解決方法是在pom.xml文件中配置tomcat7-maven-plugin插件時加入參數update
<plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <url>http://XXX:8080/manager/html</url> <server>tomcat</server> <username>admin</username> <password>12345</password> <path>/${finalName}</path> <update>true</update> </configuration> </plugin>
「web.xml which will be ignored 」問題 在使用Maven 編譯項目的時候會出現:
[WARNING] Warning: selected war files include a WEB-INF/web.xml which will be ignored (webxml attribute is missing from war task, or ignoreWebxml attribute is specified as 'true')
解決方法是添加下面這樣一個plugin便可:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.1.1</version> <configuration> <packagingExcludes>WEB-INF/web.xml</packagingExcludes> </configuration> </plugin>
去tomcat官網http://tomcat.apache.org/,左側欄Apache Tomcat下的Maven Plugin,點進去選擇最新版本Version 2.2
經過介紹可知,使用tomcat的maven插件有兩種配置方式:
第一種:在pom.xml文件的<build></build>中加入以下配置:
這種配置是針對某一個項目的,只對一個項目生效。
第二種:在maven的setting.xml文件中加入以下配置:
這種在maven插件上的配置會對全部的項目起做用。
配置好以後,就能夠啓動項目看效果了。
使用Maven Build啓動項目,Goals那一欄填:
tomcat6:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080
或者填:
tomcat7:run -Dmaven.tomcat.uriEncoding=UTF-8 -Dmaven.tomcat.path=/ -Dmaven.tomcat.port=8080
其中,
-Dmaven.tomcat.uriEncoding=UTF-8 這個配置最好始終加上
-Dmaven.tomcat.path=/ 這個配置能夠不加,默認使用/${artifactId},此處的artifactId
即建pom.xml文件時寫的那個artifactId
,通常爲項目名。若是配置爲/的話,屆時訪問的路徑就是hostname:port/,若是配置爲/test的話,則訪問路徑是hostname:port/test,至關於namesapce的做用。
-Dmaven.tomcat.port=8080 這個配置能夠設置,默認是8080
以上兩種啓動方式的區別僅在於使用的tomcat的版本不同。若是使用tomcat7的話,則若是配置方式是在pom.xml文件中配置的話,則必須配置tomcat7-maven-plugin,不然會BUILD FAILURE;若是配置方式是配置maven的setting.xml文件的話,則無所謂,<pluginGroup>org.apache.tomcat.maven</pluginGroup>這一行的做用是把全部版本的maven的tomcat插件及相關插件都下載下來了。使用tomcat6的話也同理。