標籤:arch settings selected lan 注意 tar replace cal 啟動
導讀
首先說說自己為啥要用maven管理項目,一個直接的原因是:我在自己電腦上開發web項目,每次部署到伺服器上時都要經曆如下步驟:
- 首先在Eclipse裡將項目打包成war包
- 將伺服器上原來的專案檔夾刪掉
cd /var/lib/tomcat7/webappssudo rm XXX.warsudo rm -rf XXX
- 將war包傳到伺服器上,比如用pscp命令上傳
pscp -pw "xxx" XXX.war [email protected]:/var/lib/tomcat7/webapps
- 重啟tomcat
sudo service tomcat7 restart
每次都這些步驟,非常煩人,而用maven來管理就不需要這些步驟啦,直接在Eclipse裡配置maven外掛程式,然後使用maven來自動部署項目,關於怎麼自動部署可網上很多教程,具體可參看後面的參考資料,部署成功後只需要用一個命令即可自動將我的web項目部署到tomcat伺服器上,我一般用下面這樣的命令:
mvn tomcat7:deploy -Dmaven.test.skip=true
其中-Dmaven.test.skip=true表示臨時性跳過測試代碼的編譯(也可用-DskipTests表示跳過測試階段),maven.test.skip同時控制maven-compiler-plugin和maven-surefire-plugin兩個外掛程式的行為,即跳過編譯,又跳過測試。
但初次按著教程來總是遇到各種問題,下面記錄我在部署過程中遇到的各種問題及注意事項,以提供參考意義。
maven使用問題小記
-
maven設定檔pom.xml裡的tomcat外掛程式一般像下面這樣配置:
<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裡配置如下:
<server> <id>tomcat</id> <username>admin</username> <password>123456</password></server>
這裡的username和password一般為tomcat server的使用者名稱和密碼。
-
開始運行自動部署命令時,一定要先啟動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錯誤
[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>
參考資料
- 使用Maven自動部署Java Web應用到Tomcat伺服器
- Maven入門樣本(3):自動部署至外部Tomcat
- Maven入門樣本(4):自動部署Tomcat錯誤排除
- Maven使用中的常見問題整理
摘自:http://blog.csdn.net/lanxuezaipiao/article/details/40356991
使用Maven自動部署Java Web項目到Tomcat問題小記