maven 自动部署到 tomcat7

来源:互联网 发布:解压缩软件 安卓 编辑:程序博客网 时间:2024/04/30 19:25
多方搜索,终于使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. <pluginManagement>  
  2.     <plugins>  
  3.         <plugin>  
  4.             <groupId>org.apache.tomcat.maven</groupId>  
  5.             <artifactId>tomcat6-maven-plugin</artifactId>  
  6.             <version>2.0-SNAPSHOT</version>       
  7.             <configuration>  
  8.               <url>http://localhost:8080/manager/html</url>   
  9.               <server>tomcat</server>  
  10.             </configuration>         
  11.         </plugin>  
  12.         <plugin>  
  13.             <groupId>org.apache.tomcat.maven</groupId>  
  14.             <artifactId>tomcat7-maven-plugin</artifactId>  
  15.             <version>2.0-SNAPSHOT</version>  
  16.             <configuration>  
  17.                 <url>http://localhost:8080/manager/html</url>   
  18.                 <server>tomcat</server>  
  19.             </configuration>  
  20.         </plugin>  
  21.     </plugins>  
  22. </pluginManagement>  

--2.0-SNAPSHOT是最新版本还未realse,最新realse的版本是2.0-beta-1

--html可以替换成text

 

b.在project节点下,添加仓库信息,保证maven可以从仓库中下载到tomcat-maven-plugin插件,少添加了这段信息,没有下载到插件,导致报错,浪费了不少时间。

网上的帖子说要添加如下两段信息,个人怀疑只需要添加1段。

 

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


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


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

在<servers>标签中加入

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

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

本部分配置也可写在pox.xml <plugin>的<configuration>中

 

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

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