1.tomcat配置:
修改tomcat/conf/tomcat-users.xml
增加:
<role rolename="manager"/> <role rolename="admin"/> <role rolename="manager-gui"/> <user username = "admin" password = "password" roles = "admin,manager,manager-gui,manager-script,manager-jmx,manager-status" />
2.修改maven的.setting.xml文件:
增加:
<server> <id>tomcat</id> <username>admin</username> <password>admin</password> </server>
3.修改项目下pom.xml文件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.2</version> <configuration> <source>1.7</source> <target>1.7</target> <showWarnings>true</showWarnings> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <webXml>src/main/webapp/WEB-INF/web.xml</webXml> </configuration> </plugin> <!-- tomcat自动部署 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <url>http://192.168.1.253:8080/manager</url> <server>tomcat</server> <username>admin</username> <password>admin</password> <path>/task</path> </configuration> </plugin>
4.右键项目-》Run as-》mvn build(第二个) 然后在goals中填写package tomcat:redeploy点击run
5.注意如果不成功,请按一下方式修改:以下异常处理是网上总结的非常好,借用一下【转自:http://www.51testing.com/html/94/488194-845177.html】
在Maven与Tomcat配合部署过程中,最常见的错误有三种,折腾了半天,终于找到三种错误产生的原因,以及相应的完美解决办法
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>