转自 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种可以只写一个
- <</span>pluginManagement>
- <</span>plugins>
- <</span>plugin>
- <</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段。
- <</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>
- <</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),
在标签中加入
- <</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自动部署工程
- <</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