前言:html
此处的方法适用于tomcat6 和 tomcat7,对于最新的tomcat8尚未进行过测试,有兴趣的同窗能够本身测一下.linux
总共分为五步:apache
1.在tomcat中配置用户权限,即添加管理员账号tomcat
2.在maven中添加server,配置tomcat的管理员账号密码服务器
3.在project中添加插件,以及maven中配置的server,eclipse
4.设置部署命令maven
5.进行部署测试
附相关错误及解决办法ui
下面进行分步骤讲解:url
咱们须要实现热部署,天然就须要经过maven操做tomcat,因此就须要maven取得操做tomcat的权限,如今这一步就是配置tomcat的可操做权限.
在tomcat的安装目录下,修改conf / tomcat-user.xml文件,在<tomcat-users> 节点下面增长以下配置:
<role rolename="manager-gui" /> <role rolename="manager-script" /> <user username="tomcat" password="tomcat" roles="manager-gui, manager-script" />
二.在maven中添加server,配置tomcat的管理员账号密码
如今tomcat开启了权限,maven既然要操做tomcat,那么maven就要拿到tomcat的管理员账号和密码才可以使用.
在maven的安装目录下,修改conf / setting.xml文件.在<server> 节点下面增长以下配置:
<server> <id>admin</id> <username>tomcat</username> <password>tomcat</password> </server>
三.在project中添加插件,以及maven中配置的server,
如今maven已经拥有操做tomcat的权限了,可是这二者之间想要通讯的话还须要一个桥梁,那就是在maven中配置tomcat插件.
修改项目的pom.xml文件,在<build> 节点下面增长以下配置:
<plugins> <!-- 第一种方式: apache官方tomcat插件,支持deploy --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>admin</server> </configuration> </plugin> <!-- 第二种方式: 第三方tomcat插件,支持redeploy --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <url>http://localhost:8080/manager/text</url> <server>admin</server> <ignorePackaging>true</ignorePackaging> </configuration> </plugin> </plugins>
对于以上插件,网上有两种,通过测试都是能够用的,可是注意,以上的配置只能用于tomcat7,若是要用tomcat6的话,须要配置成以下方式:
<plugins> <!-- apache官方tomcat插件,支持deploy --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.0-SNAPSHOT</version> <configuration> <url>http://localhost:8080/manager/html</url> <server>admin</server> </configuration> </plugin> <!-- 第三方tomcat插件,支持redeploy --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <url>http://localhost:8080/manager</url> <server>admin</server> <ignorePackaging>true</ignorePackaging> </configuration> </plugin> </plugins>
不管是tomcat7仍是tomcat6,只要用到其中一种插件就好了.可是这两种插件仍是有区别的:
第一种是apache官方的插件,支持deploy命令,若是已经部署到tomcat容器,第二次部署的时候就会报错,提示该项目已经部署.
第二种是第三方的插件,能够使用redeploy命令,能够重复部署
同时,请在<project>节点下增长仓库配置,否则可能插件找不到,致使报错:
<repositories> <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> </repositories> <pluginRepositories> <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> </pluginRepositories>
通常使用搜是在eclipse中,能够右键点击须要部署的项目,Run as -> Run configurations -> maven build -> 右键 new,这样配置一个新的maven命令
具体配置命令方法:
1.在base directory中选择本身的项目
2.Goals的配置
若是使用apache的官方插件,那么就用 " tomcat7:deploy " 命令
若是使用第三方插件,那么就用 " tomcat:redeploy "命令
五.进行部署
右键本身的项目,Run as -> Maven build便可.
注:
有些同窗不习惯使用IDE,那么在命令行中使用maven实现部署也是同样的.
经过命令行进入到本身的项目文件夹下,使用mvn命令,例如: mvn tomcat7:deploy
1.Connection refused错误
报错信息以下:
[ERROR]Failed to execute goal org.apache.tomcat.maven: tomcat7-maven-plugin: 2.0- SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]
缘由:未启动Tomcat服务器
解决办法:先启动Tomcat服务器再选择Run
2. 401错误
报错信息以下:
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin: 2.0-SNAPSHOT:deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 401 for URL: http://localhost:8080/manager/text/deploy?path=%2Fhelloworld -> [Help 1]
缘由:权限问题
解决办法在$CATALINA_BASE/conf/tomcat-users.xml,
如D:\apache-tomcat-7.0.34\conf\tomcat-users.xml文件中添加权限
<role rolename=”manager”/>
<user username=”admin” password=”admin” roles=”manager”/>
修改pom.xml文件,在<configuration> </configuration>中添加
<username>admin</username> <password>admin</password>
3.403错误
报错信息以下:
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin: 2.0-SNAPSHOT:deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Server returned HTTP response code: 403 for URL: http://localhost:8080/manager/html/deploy?path=%2Fhelloworld -> [Help 1]
缘由:产生该问题有可能由于两个缘由,具体参看法决办法
解决办法:
1)若是使用的是Tomcat 7,须要修改pom.xml中部署的url地址,将<url>http://localhost:8080/manager< /url>改<url>http://localhost:8080/manager/text</url>
2)给tomcat用户权限分配上,须要同时具有manager-gui和manager-script权限,我在遇到该问题时,就是忘了分配manager-script权限。正确的conf/tomcat-users.xml配置应为:<tomcat-users><role rolename="manager-gui"/><role rolename="manager-script"/><user username="admin” password="admin" roles="manager-gui, manager-script"/></tomcat-users>