1. 程式人生 > >Maven tomcat7 遠端熱部署配置

Maven tomcat7 遠端熱部署配置

Tomcat部署,專案釋出有很多種方式

  1. 增量釋出,把修改過得那些檔案手動上傳至Tomcat,*.class *.xml 等等,這樣的缺點非常大,需要斷開Tomcat,記住那些你修改過得檔案,很繁瑣
  2. Tomcat控制檯GUI熱部署,就是每次打完war包,手動上傳到Tomcat,這樣不需要斷開,也就是手工熱部署,但是如果你是分散式開發,很多工程一個個手工打包上傳部署,也是夠了…
  3. Tomcat指令碼方式熱部署(自動熱部署),這個比較簡便,實用maven編譯後直接部署到遠端伺服器~

img

You are not authorized to view this page. If you have not changed any configuration files, please examine the file conf/tomcat-users.xml in your installation. That file must contain the credentials to let you use this webapp.

For example, to add the manager-gui role to a user named tomcat with a password of s3cret, add the following to the config file listed above.

<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager role to the following four roles. You will need to assign the role(s) required for the functionality you wish to access.

manager-gui - allows access to the HTML GUI and the status pages
manager-script - allows access to the text interface and the status pages
manager-jmx - allows access to the JMX proxy and the status pages
manager-status - allows access to the status pages only
The HTML interface is protected against CSRF but the text and JMX interfaces are not. To maintain the CSRF protection:

Users with the manager-gui role should not be granted either the manager-script or manager-jmx roles.
If the text or jmx interfaces are accessed through a browser (e.g. for testing since these interfaces are intended for tools not humans) then the browser must be closed afterwards to terminate the session.
For more information - please see the Manager App HOW-TO.

修改tomcat-users.xml配置檔案,配置使用者、密碼和許可權。

[[email protected] conf]# pwd
/usr/local/software/apache-tomcat-7.0.77/conf
[[email protected] conf]# ll
total 208
drwxr-xr-x. 3 root root   4096 Mar 31 13:49 Catalina
-rw-------. 1 root root  12257 Mar 28 12:07 catalina.policy
-rw-------. 1 root root   6496 Mar 28 12:07 catalina.properties
-rw-------. 1 root root   1394 Mar 28 12:07 context.xml
-rw-------. 1 root root   3288 Mar 28 12:07 logging.properties
-rw-------. 1 root root   6613 Mar 28 12:07 server.xml
-rw-------. 1 root root   2098 Mar 31 13:58 tomcat-users.xml
-rw-------. 1 root root 167655 Mar 28 12:07 web.xml

增加這3行配置,表示GUI和指令碼部署

<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. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <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"/>
-->
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="admin" password="admin" roles="manager-gui,manager-script"/>
</tomcat-users>

在pom.xml中增加tomcat7外掛

<build>
    <finalName>winner-test</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <port>8080</port>
         <!--/會部署到ROOT下-->
                <path>/</path>
         <!--系統熱部署配置-->
                <url>http://192.168.0.106:8080/manager/text</url>
                <username>admin</username>
                <password>admin</password>
            </configuration>
        </plugin>
    </plugins>
</build>

Tomcat7+路徑是/manager/text,Tomcat6是/manager~

部署到ROOT下:/

[[email protected] webapps]# ll
total 1824
drwxr-xr-x. 14 root root    4096 Mar 31 13:48 docs
drwxr-xr-x.  7 root root    4096 Mar 31 13:48 examples
drwxr-xr-x.  5 root root    4096 Mar 31 13:48 host-manager
drwxr-xr-x.  5 root root    4096 Mar 31 13:48 manager
drwxr-xr-x.  4 root root    4096 Mar 31 14:00 ROOT
-rw-r--r--.  1 root root 1847171 Mar 31 14:00 ROOT.war

初次部署可以使用 “tomcat7:deploy” 命令,如果ROOT不存在,則使用此命令

如果已經部署過使用 “tomcat7:redeploy” 命令,如果ROOT存在,則使用此命令覆蓋即可

img

修正命令 clean tomcat7:redeploy -DskipTests

編譯上傳

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building winner-test Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ winner-test ---
[INFO] Deleting D:\winner-test\target
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:redeploy (default-cli) > package @ winner-test >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ winner-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ winner-test ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ winner-test ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\winner-test\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ winner-test ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ winner-test ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ winner-test ---
[INFO] Packaging webapp
[INFO] Assembling webapp [winner-test] in [D:\winner-test\target\winner-test]
[INFO] Processing war project
[INFO] Copying webapp resources [D:\winner-test\src\main\webapp]
[INFO] Webapp assembled in [183 msecs]
[INFO] Building war: D:\winner-test\target\winner-test.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:redeploy (default-cli) < package @ winner-test <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ winner-test ---
[INFO] Deploying war to http://192.168.0.106:8080/  
Uploading: http://192.168.0.106:8080/manager/text/deploy?path=%2F&update=true
Uploaded: http://192.168.0.106:8080/manager/text/deploy?path=%2F&update=true (1804 KB at 3031.7 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:OK
[INFO] OK - Deployed application at context path /
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.434 s
[INFO] Finished at: 2017-04-16T19:02:36+08:00
[INFO] Final Memory: 16M/204M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0