Maven添加Tomcat插件實(shí)現(xiàn)熱部署代碼實(shí)例
Maven熱部署,顧名思義就是可以不影響項(xiàng)目在服務(wù)器中的運(yùn)行情況,可以實(shí)現(xiàn)項(xiàng)目代碼的更新,減少啟動(dòng),編譯時(shí)間,達(dá)到快速開發(fā)的目的,也不需要手動(dòng)拷貝war包到遠(yuǎn)程項(xiàng)目,可以直接將項(xiàng)目以及war包部署到遠(yuǎn)程服務(wù)器。 實(shí)現(xiàn)Maven熱部署主要需要maven獲得tomcat的管理權(quán)限,首先要進(jìn)行Tomcat的配置,其次在pom.xml中配置tomcat插件即可實(shí)現(xiàn)maven熱部署。
配置Tomcat權(quán)限
在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只讀狀態(tài)頁面(即URL路徑為/manager/status/)
從Tomcat Manager內(nèi)部配置文件中可以得知,manager-gui、manager-script、manager-jmx均具備manager-status的權(quán)限,也就是說,manager-gui、manager-script、manager-jmx三種角色權(quán)限無需再額外添加manager-status權(quán)限,即可直接訪問路徑"/manager/status/*"
maven配置
<server> <id>tomcat7</id> <!--Id名稱可以隨便寫--> <username>tomcat</username> <!--用戶名與tomcat配置中username相同--> <password>tomcat</password> <!--密碼與tomcat配置中password相同--> </server>
pom.xml引入tomcat7插件
maven中關(guān)于tomcat的插件有tomcat6插件和tomcat7插件,普遍使用的是tomcat7插件,在pom.xml中添加以下片段:
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--要部署的遠(yuǎn)程服務(wù)器地址 ip端口,后面/manager/text為tomcat管理項(xiàng)目的路徑不能改變-->
<url>http://localhost:8080/manager/text</url>
<!--maven setting.xml中配置的serverID名稱-->
<server>tomcat7</server>
<!--項(xiàng)目要部署的路徑 /表示根路徑 默認(rèn)Root-->
<path>/</path>
<!--項(xiàng)目是否更新 默認(rèn)true 可不配置-->
<update>true</update>
<!--maven setting.xml以及tomcat tomcat-users.xml 中配置的用戶名密碼-->
<username>tomcat</username>
<password>tomcat</password>
</configuration>
</plugin>
<!--以下配置不是必須,為了保證編譯版本,tomcat7插件默認(rèn)支持的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>
配置完成,接下來運(yùn)行run as maven build,然后在Goals中填上tomcat7:deploy運(yùn)行項(xiàng)目,實(shí)現(xiàn)maven熱部署了,看到以下信息提示,就表示執(zhí)行成功了。
[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] ------------------------------------------------------------------------
注意事項(xiàng):
部署項(xiàng)目前要先啟動(dòng)tomcat服務(wù)器
在執(zhí)行tomcat7:deploy命令時(shí)注意jre版本的配置,JDK版本選擇1.7
首次執(zhí)行選擇第一個(gè)maven build,非首次執(zhí)行選擇第二個(gè)maven build 命令執(zhí)行tomcat7:redeploy
maven集成tomcat常用命令:
tomcat:deploy 部署一個(gè)web war包
tomcat:reload 重新加載web war包
tomcat:start 啟動(dòng)
tomcat tomcat:stop 停止
tomcat tomcat:undeploy 停止一個(gè)war包
tomcat:run 啟動(dòng)嵌入式tomcat ,并運(yùn)行當(dāng)前項(xiàng)目
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springcloud中的@RefreshScope的實(shí)現(xiàn)
@RefreshScope注解是Spring Cloud中的一個(gè)注解,它用來實(shí)現(xiàn)Bean中屬性的動(dòng)態(tài)刷新,本文就來介紹一下@RefreshScope注解的使用,感興趣的可以了解一下2024-06-06
java 在file的尾部添加數(shù)據(jù)的兩種方法總結(jié)
這篇文章主要介紹了java 在file的尾部添加數(shù)據(jù)的兩種方法總結(jié)的相關(guān)資料,這里提供了兩種在file尾部添加數(shù)據(jù)的方法,希望能幫助到大家,需要的朋友可以參考下2017-09-09
springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增
這篇文章主要為大家介紹了springboot?vue項(xiàng)目管理后端實(shí)現(xiàn)接口新增,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
詳解SpringBoot如何實(shí)現(xiàn)多環(huán)境配置
在實(shí)際的軟件開發(fā)過程中,一個(gè)應(yīng)用程序通常會(huì)有多個(gè)環(huán)境,pring?Boot?提供了一個(gè)非常靈活和強(qiáng)大的方式來管理這些環(huán)境配置,下面就跟隨小編一起學(xué)習(xí)一下吧2023-07-07
Java8流式API將實(shí)體類列表轉(zhuǎn)換為視圖對(duì)象列表的示例
這篇文章主要介紹了Java8流式API將實(shí)體類列表轉(zhuǎn)換為視圖對(duì)象列表的示例,文中有相關(guān)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-11-11
Mybatis映射文件之常用標(biāo)簽及特殊字符的處理方法
這篇文章主要介紹了Mybatis映射文件常用標(biāo)簽及特殊字符的處理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05

