maven项目中部署tomcat插件

时间:2021-10-24 20:00:40

转自 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种可以只写一个

[html] view plaincopy
  1. <</span>pluginManagement>  
  2.     <</span>plugins>  
  3.         <</span>plugin>  
  4.             <</span>groupId>org.apache.tomcat.maven</</span>groupId>  
  5.             <</span>artifactId>tomcat6-maven-plugin</</span>artifactId>  
  6.             <</span>version>2.0</</span>version>       
  7.             <</span>configuration>  
  8.               <</span>url>http://localhost:8080/manager/html</</span>url>   
  9.               <</span>server>tomcat</</span>server>  
  10.             </</span>configuration>         
  11.         </</span>plugin>  
  12.         <</span>plugin>  
  13.             <</span>groupId>org.apache.tomcat.maven</</span>groupId>  
  14.             <</span>artifactId>tomcat7-maven-plugin</</span>artifactId>  
  15.             <</span>version>2.0</</span>version>  
  16.             <</span>configuration>  
  17.                 <</span>url>http://localhost:8080/manager/html</</span>url>   
  18.                 <</span>server>tomcat</</span>server>  
  19.             </</span>configuration>  
  20.         </</span>plugin>  
  21.     </</span>plugins>  
  22. </</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
  1. <</span>repository>  
  2.     <</span>id>people.apache.snapshots</</span>id>  
  3.     <</span>url>  
  4.         http://repository.apache.org/content/groups/snapshots-group/  
  5.     </</span>url>  
  6.     <</span>releases>  
  7.         <</span>enabled>false</</span>enabled>  
  8.     </</span>releases>  
  9.     <</span>snapshots>  
  10.         <</span>enabled>true</</span>enabled>  
  11.     </</span>snapshots>  
  12. </</span>repository>  


[html] view plaincopy
  1. <</span>pluginRepository>  
  2.     <</span>id>apache.snapshots</</span>id>  
  3.     <</span>name>Apache Snapshots</</span>name>  
  4.     <</span>url>  
  5.         http://repository.apache.org/content/groups/snapshots-group/  
  6.     </</span>url>  
  7.     <</span>releases>  
  8.         <</span>enabled>false</</span>enabled>  
  9.     </</span>releases>  
  10.     <</span>snapshots>  
  11.         <</span>enabled>true</</span>enabled>  
  12.     </</span>snapshots>  
  13. </</span>pluginRepository>  


 2.配置setting.xml,%MAVEN_HOME%\conf\setting.xml(前提是在myeclipse preferences中maven启用本地安装版本并设置用户setting.xml为本地conf下的setting.xml,而不是插件,插件应该是“我的文档”\.m2\setting.xml),

在标签中加入

[html] view plaincopy
  1. <</span>server>  
  2.        <</span>id>tomcat</</span>id>  
  3.        <</span>username>admin</</span>username>  
  4.        <</span>password>admin</</span>password>  
  5. </</span>server>  

id与pom.xml文件配置相同,用户名密码与tomcat_user相同。

本部分配置也可写在pox.xml 的中

 

3.给tomcat配置用户,%TOMCAT_HOME%\conf\tomcat_user.xml 增加以下角色和用户,用于tomcat_maven_plugin自动部署工程

[html] view plaincopy
  1. <</span>role rolename="manager-gui"/>    
  2. <</span>role rolename="manager-script"/>    
  3. <</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

http://vivus.iteye.com/blog/1561664