Maven tomcat7热部署

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

  1. 增量发布,把修改过得那些文件手动上传至Tomcat,*.class *.xml 等等,这样的缺点非常大,需要断开Tomcat,记住那些你修改过得文件,很繁琐
  2. Tomcat控制台GUI热部署,就是每次打完war包,手动上传到Tomcat,这样不需要断开,也就是手工热部署,但是如果你是分布式开发,很多工程一个个手工打包上传部署,也是够了…
  3. Tomcat脚本方式热部署(自动热部署),这个比较简便,实用maven编译后直接部署到远程服务器~

Tomcat管理页面:http://192.168.0.106:8080/manager

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配置文件,配置用户、密码和权限。

[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.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>

实际项目pom例子:

<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<package.env>dev</package.env>
				<server.url>http://169.128.255.90:9080</server.url>
				<log4j.level>DEBUG</log4j.level>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>test</id>
			<properties>
				<package.env>test</package.env>
				<server.url>http://169.128.255.90:9080</server.url>
				<log4j.level>DEBUG</log4j.level>
			</properties>
		</profile>
		<profile>
			<id>product</id>
			<properties>
				<package.env>product</package.env>
				<server.url></server.url>
				<log4j.level>INFO</log4j.level>
			</properties>
		</profile>
</profiles>
	
<build>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*</include>
				</includes>
				<excludes>
					<exclude>**/dev/*</exclude>
					<exclude>**/product/*</exclude>
					<exclude>**/test/*</exclude>
					<exclude>*.properties</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>*.properties</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources/${package.env}</directory>
				<filtering>false</filtering>
				<includes>
					<include>**/*</include>
				</includes>
			</resource>

		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>2.5</version>
				<configuration>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.3.2</version>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
					<debug>true</debug>
					<optimize>false</optimize>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<url>${server.url}/manager/text</url>
					<username>script</username>
					<password>tomcatdmpad</password>
					<path>/dmpproduct</path>
				</configuration>
			</plugin>

		</plugins>
	</build>

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

部署到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存在,则使用此命令 clean tomcat7:redeploy -DskipTests

强大的IDEA,提供了插件,点击一下轻松搞定。(这种方式对修改配置文件好像不会生效)
在这里插入图片描述

编译上传

[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
发布了69 篇原创文章 · 获赞 123 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/u011870547/article/details/83992295
今日推荐