轉自 http://blog.csdn.net/lanhaimylover/article/details/7794476
多次驗證,得把<</span>url>http://localhost:8080/manager/html</</span>url>調整為http://localhost:8080/manager/text。
多方搜尋,終於使maven項目可以自動發布到tomcat下了。
tomcat7 需要使用 tomcat-maven-plugin 的新版本,版本支援tomcat6和tomcat7,groupId也由org.codehaus.mojo改為org.apache.tomcat.maven。 可以參考看看:http://tomcat.apache.org/maven-plugin.html
主菜來了。
1.修改項目的pom.xml
a.在project節點下添加tomcat-maven-plugin外掛程式資訊,如下寫法添加了tomcat6和tomcat7的外掛程式,如只用1種可以唯寫一個
<pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.0</version> <configuration> <url>http://localhost:8080/manager/html</url> <server>tomcat</server> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0</version> <configuration> <url>http://localhost:8080/manager/html</url> <server>tomcat</server> <username>admin</username> <password>admin</password> </configuration> </plugin> </plugins> </pluginManagement>
--html可以替換成text,此處必須替換為text,否則報403。
b.在project節點下,添加倉庫資訊,保證maven可以從倉庫中下載到tomcat-maven-plugin外掛程式,少添加了這段資訊,沒有下載到外掛程式,導致報錯,浪費了不少時間。
網上的文章說要添加如下兩段資訊,個人懷疑只需要添加1段。
<repository> <id>people.apache.snapshots</id> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository>
<pluginRepository> <id>apache.snapshots</id> <name>Apache Snapshots</name> <url> http://repository.apache.org/content/groups/snapshots-group/ </url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository>
2.配置setting.xml,%MAVEN_HOME%\conf\setting.xml(前提是在myeclipse preferences中maven啟用本地安裝版本並設定使用者setting.xml為本地conf下的setting.xml,而不是外掛程式,外掛程式應該是“我的文件”\.m2\setting.xml),
在標籤中加入
<server> <id>tomcat</id> <username>admin</username> <password>admin</password> </server>
id與pom.xml檔案配置相同,使用者名稱密碼與tomcat_user相同。
本部分配置也可寫在pox.xml 的中
3.給tomcat配置使用者,%TOMCAT_HOME%\conf\tomcat_user.xml 增加以下角色和使用者,用於tomcat_maven_plugin自動部署工程
<role rolename="manager-gui"/> <role rolename="manager-script"/> <user username="admin" password="admin" roles="manager-gui, manager-script"/>
4. 啟動tomcat,在工程或pom.xml上右鍵,maven build的goals中輸入命令tomcat7:deploy即可發布,或在Run Configurations->Maven build建立一個命令,base directory裡選擇你的web project,在Goals欄可填寫你所需要的命令。
goals中使用的命令可以參考http://tomcat.apache.org/maven-plugin-2.0-SNAPSHOT/tomcat7-maven-plugin/plugin-info.html
參考連結:
http://hi.baidu.com/xikan/blog/item/66547fedb13201f5b21cb120.html
http://vivus.iteye.com/blog/1561664