maven实现tomcat的热部署。


第一步:安装tomcat


    先在CentOS中安装jdk,然后解压tomcat。


第二步:在tomcat中配置用户权限


    我们需要实现热部署,自然就需要通过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"/>

第三步:修改pom文件

    在project中添加插件,以及maven中配置的server,

现在maven已经拥有操作tomcat的权限了,但是这两者之间想要通信的话还需要一个桥梁,那就是在maven中配置tomcat插件.

    修改项目的pom.xml文件,在<build> 节点下面增加如下配置:

<build>
		<plugins>
			<!-- 配置Tomcat插件 -->
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8081</port>
					<path>/</path>
					<url>http://192.168.25.136:8080/manager/text</url>
					<username>tomcat</username>
					<password>tomcat</password>
				</configuration>		
			</plugin>
		</plugins>
	</build>


    一般使用搜是在eclipse中,可以右键点击需要部署的项目,Run as -> Run configurations -> maven build -> 右键 new,这样配置一个新的maven命令具体配置命令方法:

第四步:设置部署命令

初次部署可以使用 "tomcat7:deploy" 命令

如果已经部署过使用 "tomcat7:redeploy" 命令


注:相关错误提示及解决办法

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=adminpassword=adminroles=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-guimanager-script权限,我在遇到该问题时,就是忘了分配manager-script权限。

正确的conf/tomcat-users.xml配置应为:

<tomcat-users>

<role rolename="manager-gui"/>

<role rolename="manager-script"/>

<user username="adminpassword="admin" roles="manager-gui, manager-script"/>

</tomcat-users>




猜你喜欢

转载自blog.csdn.net/woainimax/article/details/78408673