1. 程式人生 > >動態釋出maven專案到tomcat

動態釋出maven專案到tomcat

我們知道tomcat是一款非常小巧且易於使用的伺服器。它不僅僅支援本地部署web專案,而且支援遠端線上部署web專案。以下是tomcat7 “manager App”的介紹性文件:
<!--In many production environments, it is very useful to have the capability to deploy a new web application, or undeploy an existing one, without having to shut down and restart the entire container. In addition, you can request an existing application to reload itself, even if you have not declared it to be reloadable in the Tomcat server configuration file.
To support these capabilities, Tomcat includes a web application (installed by default on context path /manager) that supports the following functions:
Deploy a new web application from the uploaded contents of a WAR file.
Deploy a new web application, on a specified context path, from the server file system.
List the currently deployed web applications, as well as the sessions that are currently active for those web apps.
Reload an existing web application, to reflect changes in the contents of /WEB-INF/classes or /WEB-INF/lib.
List the OS and JVM property values.
List the available global JNDI resources, for use in deployment tools that are preparing <ResourceLink>elements nested in a <Context> deployment description.
Start a stopped application (thus making it available again).
Stop an existing application (so that it becomes unavailable), but do not undeploy it.
Undeploy a deployed web application and delete its document base directory (unless it was deployed from file system).-->
Maven正是利用了tomcat的這個特性,通過簡單的配置就可以方便快捷的部署web專案到tomcat上,以tomcat7為例:

1.為tomcat “manager App”(app管理模組配置)建立使用者,併為其分配許可權。開啟伺服器所在目錄/config/tomcat-users.xml

<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->

<!--修改配置檔案為 <!--配置許可權選擇,"manager App"有四種許可權供選擇
   The available roles are:
	1.manager-gui — Access to the HTML interface.
	2.manager-status — Access to the "Server Status" page only.
	3.manager-script — Access to the tools-friendly plain text interface that is described in this document, 
	  and to the "Server Status" page.
	4.manager-jmx — Access to JMX proxy interface and to the "Server Status" page.
     詳細說明請參閱tomcat說明文件,我們要通過配置檔案的方式部署web專案,所以需要的使用者角色是manger-script
   -->
  <role rolename="manager-script"/>
  <!--建立使用者和密碼且賦予使用者許可權為"manager-script"-->
  <user username="tomcat" password="tomcat" roles="manager-script"/>
  <!--<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>-->

2.對maven的pom進行配置。maven要實現動態釋出web專案到tomcat,必須先安裝tomcat外掛。
專案maven的pom.xml   
<plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <!-- tomcat使用<artifactId>tomcat6-maven-plugin<artifactId> -->
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
          	<!--tomcat7動態部署web專案地址,tomcat6不需要加text的  -->
          	<url>http://localhost:8080/manager/text</url>
          	<!-- 在上問tomcat-users配置檔案中建立的使用者名稱和密碼 -->
          	<username>tomcat</username>
          	<password>tomcat</password>
          	<!-- 專案名稱 -->
          	<path>demo3</path>
          </configuration>
       </plugin>
3.maven專案釋出過程

  

  執行結果圖:釋出成功

   

The goals required to redeploy a WAR project depend upon how it was deployed:

  • To redeploy a WAR project deployed by tomcat:deploy you can type:
    mvn package tomcat6/7:redeploy
  • To redeploy a WAR project deployed by tomcat:exploded you can type:
    mvn war:exploded tomcat6/7:redeploy
  • To redeploy a WAR project deployed by tomcat:inplace you can type:
    mvn war:inplace tomcat6/7:redeploy
  • To redeploy a context.xml file deployed by tomcat:deploy you can type:
    mvn tomcat6/7:redeploy

    Note: Depending on the docBase specified in the context.xml you may also need to call war:exploded or war:inplace as above.

參考文件:https://tomcat.apache.org/tomcat-7.0-doc/manager-howto.html

  https://tomcat.apache.org/maven-plugin-2.2/