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 ,並運行當前項目