首页 > 代码库 > Tomcat热部署

Tomcat热部署

tomcat7部署,项目发布有很多种方式

1. 增量发布,把修改过得那些文件手动上传至tomcat,*.class *.xml 等等,这样的缺点非常大,需要断开tomcat,记住那些你修改过得文件,很繁琐,我曾经经历过,值得吐槽

2. tomcat控制台GUI热部署,就是每次打完war包,手动上传到tomcat,这样不需要断开,但是如果你是分布式开发,有30个war,那你也每次手动上传吗?

3. tomcat脚本方式热部署,这个比较简便,实用maven编译后直接部署到远程服务器

修改这个文件夹下的这个文件

[root@localhost conf]# pwd
/usr/local/software/apache-tomcat-7.0.77/conf
[root@localhost 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中增加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>
                <path>/</path>
                <url>http://192.168.0.106:8080/manager/text</url>
                <username>admin</username>
                <password>admin</password>
            </configuration>
        </plugin>
    </plugins>
</build>

部署到ROOT下

[root@localhost 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存在,则使用此命令覆盖即可

技术分享

编译上传

[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

 

Tomcat热部署