温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Maven添加Tomcat插件实现热部署代码实例

发布时间:2020-09-26 18:51:34 来源:脚本之家 阅读:268 作者:Chsoul''S Blog 栏目:编程语言

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 ,并运行当前项目

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI