屢次驗證,得把<</span>url>http://localhost:8080/manager/html</</span>url>調整爲http://localhost:8080/manager/text。html
多方搜索,終於使maven項目能夠自動發佈到tomcat下了。web
tomcat7 須要使用 tomcat-maven-plugin 的新版本,版本支持tomcat6和tomcat7,groupId也由org.codehaus.mojo改成org.apache.tomcat.maven。 能夠參考看看:http://tomcat.apache.org/maven-plugin.htmlapache
主菜來了。tomcat
1.修改項目的pom.xml eclipse
a.在project節點下 添加tomcat-maven-plugin插件信息,以下寫法添加了tomcat6和tomcat7的插件,如只用1種能夠只寫一個maven
[html] view plaincopyui
<</span>pluginManagement> url
<</span>plugins> spa
<</span>plugin> .net
<</span>groupId>org.apache.tomcat.maven</</span>groupId>
<</span>artifactId>tomcat6-maven-plugin</</span>artifactId>
<</span>version>2.0</</span>version>
<</span>configuration>
<</span>url>http://localhost:8080/manager/html</</span>url>
<</span>server>tomcat</</span>server>
</</span>configuration>
</</span>plugin>
<</span>plugin>
<</span>groupId>org.apache.tomcat.maven</</span>groupId>
<</span>artifactId>tomcat7-maven-plugin</</span>artifactId>
<</span>version>2.0</</span>version>
<</span>configuration>
<</span>url>http://localhost:8080/manager/html</</span>url>
<</span>server>tomcat</</span>server>
</</span>configuration>
</</span>plugin>
</</span>plugins>
</</span>pluginManagement>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>tomcat</server>
<username>admin</username>
<password>1234</password>
</configuration>
</plugin>
--html能夠替換成text,此處必須替換爲text,不然報403。
b.在project節點下,添加倉庫信息,保證maven能夠從倉庫中下載到tomcat-maven-plugin插件,少添加了這段信息,沒有下載到插件,致使報錯,浪費了很多時間。
網上的帖子說要添加以下兩段信息,我的懷疑只須要添加1段。
[html] view plaincopy
<</span>repository>
<</span>id>people.apache.snapshots</</span>id>
<</span>url>
http://repository.apache.org/content/groups/snapshots-group/
</</span>url>
<</span>releases>
<</span>enabled>false</</span>enabled>
</</span>releases>
<</span>snapshots>
<</span>enabled>true</</span>enabled>
</</span>snapshots>
</</span>repository>
[html] view plaincopy
<</span>pluginRepository>
<</span>id>apache.snapshots</</span>id>
<</span>name>Apache Snapshots</</span>name>
<</span>url>
http://repository.apache.org/content/groups/snapshots-group/
</</span>url>
<</span>releases>
<</span>enabled>false</</span>enabled>
</</span>releases>
<</span>snapshots>
<</span>enabled>true</</span>enabled>
</</span>snapshots>
</</span>pluginRepository>
2.配置setting.xml,%MAVEN_HOME%\conf\setting.xml(前提是在myeclipse preferences中maven啓用本地安裝版本並設置用戶setting.xml爲本地conf下的setting.xml,而不是插件,插件應該是「個人文檔」\.m2\setting.xml),
在標籤中加入
[html] view plaincopy
<</span>server>
<</span>id>tomcat</</span>id>
<</span>username>admin</</span>username>
<</span>password>admin</</span>password>
</</span>server>
id與pom.xml文件配置相同,用戶名密碼與tomcat_user相同。
本部分配置也可寫在pox.xml 的中
3.給tomcat配置用戶,%TOMCAT_HOME%\conf\tomcat_user.xml 增長如下角色和用戶,用於tomcat_maven_plugin自動部署工程
[html] view plaincopy
<</span>role rolename="manager-gui"/>
<</span>role rolename="manager-script"/>
<</span>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