Maven添加Tomcat插件实现热部署

Maven热部署,顾名思义就是可以不影响项目在服务器中的运行情况,可以实现项目代码的更新,减少启动,编译时间,达到快速开发的目的,也不需要手动拷贝war包到远程项目,可以直接将项目以及war包部署到远程服务器。 实现Maven热部署主要需要maven获得tomcat的管理权限,首先要进行Tomcat的配置,其次在pom.xml中配置tomcat插件即可实现maven热部署。

配置Tomcat权限

在tomcat文件目录下找到apache-tomcat-7.0.68/conf/tomcat-users.xml文件,在文中位置加入以下角色(非注释部分)配置:

<tomcat-users>
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary.
-->
<!--
  NOTE:  The sample user and role entries below are wrapped in a comment
  and thus are ignored when reading this file. Do not forget to remove
  <!.. ..> that surrounds them.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
-->
<role rolename="manager-gui" />
<role rolename="manager-script" />
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script" />
</tomcat-users>

配置说明

  • manager-gui:允许访问html接口(即URL路径为/manager/html/)
  • manager-script:允许访问纯文本接口(即URL路径为/manager/text/)
  • manager-jmx:允许访问JMX代理接口(即URL路径为/manager/jmxproxy/)
  • manager-status:允许访问Tomcat只读状态页面(即URL路径为/manager/status/)

从Tomcat Manager内部配置文件中可以得知,manager-gui、manager-script、manager-jmx均具备manager-status的权限,也就是说,manager-gui、manager-script、manager-jmx三种角色权限无需再额外添加manager-status权限,即可直接访问路径"/manager/status/*"

maven配置

<server>
    <id>tomcat7</id>					<!--Id名称可以随便写-->
    <username>tomcat</username>				<!--用户名与tomcat配置中username相同-->
    <password>tomcat</password>				<!--密码与tomcat配置中password相同-->
</server>

pom.xml引入tomcat7插件

maven中关于tomcat的插件有tomcat6插件和tomcat7插件,普遍使用的是tomcat7插件,在pom.xml中添加以下片段:

<plugins>
	<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
        <!--要部署的远程服务器地址 ip端口,后面/manager/text为tomcat管理项目的路径不能改变-->
        <url>http://localhost:8080/manager/text</url>
        <!--maven setting.xml中配置的serverID名称-->
        <server>tomcat7</server>
        <!--项目要部署的路径 /表示根路径 默认Root-->
        <path>/</path>
        <!--项目是否更新 默认true 可不配置-->
        <update>true</update>
        <!--maven setting.xml以及tomcat tomcat-users.xml 中配置的用户名密码-->
        <username>tomcat</username>
        <password>tomcat</password>
        </configuration>
	</plugin>       
	<!--以下配置不是必须,为了保证编译版本,tomcat7插件默认支持的JDK1.7-->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <encoding>UTF-8</encoding>
        </configuration>
    </plugin>
</plugins>

配置完成,接下来运行run as maven build,然后在Goals中填上tomcat7:deploy运行项目,实现maven热部署了,看到以下信息提示,就表示执行成功了。

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.560 s
[INFO] Finished at: 2019-04-18T20:37:57+08:00
[INFO] Final Memory: 21M/222M
[INFO] ------------------------------------------------------------------------

注意事项:

  • 部署项目前要先启动tomcat服务器
  • 在执行tomcat7:deploy命令时注意jre版本的配置,JDK版本选择1.7
  • 首次执行选择第一个maven build,非首次执行选择第二个maven build 命令执行tomcat7:redeploy

maven集成tomcat常用命令:

tomcat:deploy 部署一个web war包     
tomcat:reload 重新加载web war包  
tomcat:start 启动    
tomcat  tomcat:stop 停止   
tomcat  tomcat:undeploy 停止一个war包    
tomcat:run 启动嵌入式tomcat ,并运行当前项目

本文参考:https://www.cnblogs.com/luchangyou/p/7573481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星光Starsray

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值