Maven使用tomcat7-maven-plugin实现远程部署

14 篇文章 5 订阅
1 篇文章 0 订阅

1简介

参见maven插件 tomcat7-maven-plugin 的使用

The Tomcat Maven Plugin provides goals to manipulate WAR projects within the Tomcat 7.x servlet container

插件文档

注意: 插件与tomcat服务器交互时,需要指定端口处于运行中。

2步骤

2.1Tomcat配置修改

2.1.1 修改apache-tomcat-8.5.38\webapps\manager\META-INF\context.xml

修改为如下的内容,允许所有ip都可以部署(默认只有本机可以部署)

<Context antiResourceLocking="false" privileged="true" >
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="^.*$" />
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

2.1.2 修改apache-tomcat-8.5.38\conf\tomcat-users.xml

修改为如下内容:

<role rolename="manager-jmx"/>
  <role rolename="manager-script"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-status"/>
  
  <user username="admin" password="admin" roles="manager-jmx,manager-script,manager-gui,manager-status"/>

为了访问界面。

2.1.3 界面验证IP:port/manager

键入用户名密码如上面tomcat-users.xml中配置

admin:admin

在这里插入图片描述
表示用户名和密码设置正确

2.2 修改POM

在POM中添加如下插件:

<plugin>
	<groupId>org.apache.tomcat.maven</groupId>
	<artifactId>tomcat7-maven-plugin</artifactId>
	<version>2.2</version>

	<configuration>
		<!--
        <path>/aa</path>
        <uriEncoding>UTF-8</uriEncoding> 闈炲繀閫夐」锛屽彲鐪佺暐 -->

		<url>http://172.20.10.3:8080/manager/text</url>
		<username>admin</username>
		<password>admin</password>
		<path>/</path>
	</configuration>

</plugin>

2.3下载插件到私服中

具体私服配置参见使用Nexus创建私服
在POM所在目录,打开终端,执行如下命令即可:

mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true 

终端可以看到如下输出:


D:\600-Git\profile\profile>mvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:profile:war:1.3-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring
-boot-starter-web:jar -> duplicate declaration of version (?) @ line 54, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------< com.example:profile >-------------------------
[INFO] Building profile 1.3-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.0.2:sources (default-cli) @ profile ---
[INFO]
[INFO] The following files have been resolved:
[INFO]    org.springframework.boot:spring-boot-starter:jar:sources:2.0.1.RELEASE
[INFO]    org.springframework.boot:spring-boot:jar:sources:2.0.1.RELEASE
[INFO]    org.springframework:spring-context:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework.boot:spring-boot-autoconfigure:jar:sources:2.0.1.RELEASE
[INFO]    org.springframework.boot:spring-boot-starter-logging:jar:sources:2.0.1.RELEASE
[INFO]    ch.qos.logback:logback-classic:jar:sources:1.2.3
[INFO]    ch.qos.logback:logback-core:jar:sources:1.2.3
[INFO]    org.slf4j:slf4j-api:jar:sources:1.7.25
[INFO]    org.apache.logging.log4j:log4j-to-slf4j:jar:sources:2.10.0
[INFO]    org.apache.logging.log4j:log4j-api:jar:sources:2.10.0
[INFO]    org.slf4j:jul-to-slf4j:jar:sources:1.7.25
[INFO]    javax.annotation:javax.annotation-api:jar:sources:1.3.2
[INFO]    org.springframework:spring-core:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework:spring-jcl:jar:sources:5.0.5.RELEASE
[INFO]    org.yaml:snakeyaml:jar:sources:1.19
[INFO]    org.springframework.boot:spring-boot-configuration-processor:jar:sources:2.0.1.RELEASE
[INFO]    org.springframework.boot:spring-boot-starter-web:jar:sources:2.0.1.RELEASE
[INFO]    org.springframework.boot:spring-boot-starter-json:jar:sources:2.0.1.RELEASE
[INFO]    com.fasterxml.jackson.core:jackson-databind:jar:sources:2.9.5
[INFO]    com.fasterxml.jackson.core:jackson-annotations:jar:sources:2.9.0
[INFO]    com.fasterxml.jackson.core:jackson-core:jar:sources:2.9.5
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:sources:2.9.5
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:sources:2.9.5
[INFO]    com.fasterxml.jackson.module:jackson-module-parameter-names:jar:sources:2.9.5
[INFO]    org.hibernate.validator:hibernate-validator:jar:sources:6.0.9.Final
[INFO]    javax.validation:validation-api:jar:sources:2.0.1.Final
[INFO]    org.jboss.logging:jboss-logging:jar:sources:3.3.2.Final
[INFO]    com.fasterxml:classmate:jar:sources:1.3.4
[INFO]    org.springframework:spring-web:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework:spring-beans:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework:spring-webmvc:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework:spring-aop:jar:sources:5.0.5.RELEASE
[INFO]    org.springframework:spring-expression:jar:sources:5.0.5.RELEASE
[INFO]    org.apache.maven:maven-plugin-api:jar:sources:3.5.0
[INFO]    org.apache.maven:maven-model:jar:sources:3.5.0
[INFO]    org.codehaus.plexus:plexus-utils:jar:sources:3.0.24
[INFO]    org.apache.commons:commons-lang3:jar:sources:3.7
[INFO]    org.apache.maven:maven-artifact:jar:sources:3.5.0
[INFO]    org.eclipse.sisu:org.eclipse.sisu.plexus:jar:sources:0.3.3
[INFO]    javax.enterprise:cdi-api:jar:sources:1.0
[INFO]    javax.annotation:jsr250-api:jar:sources:1.0
[INFO]    javax.inject:javax.inject:jar:sources:1
[INFO]    org.eclipse.sisu:org.eclipse.sisu.inject:jar:sources:0.3.3
[INFO]    org.codehaus.plexus:plexus-component-annotations:jar:sources:1.5.5
[INFO]    org.codehaus.plexus:plexus-classworlds:jar:sources:2.5.2
[INFO]    org.codehaus.cargo:cargo-maven2-plugin:jar:sources:1.7.6
[INFO]    org.apache.maven:maven-archiver:jar:sources:2.4.1
[INFO]    org.codehaus.plexus:plexus-archiver:jar:sources:1.0
[INFO]    org.codehaus.plexus:plexus-container-default:jar:sources:1.0-alpha-9-stable-1
[INFO]    junit:junit:jar:sources:4.12
[INFO]    org.hamcrest:hamcrest-core:jar:sources:1.3
[INFO]    org.codehaus.plexus:plexus-io:jar:sources:1.0
[INFO]    org.codehaus.plexus:plexus-interpolation:jar:sources:1.13
[INFO]    org.codehaus.cargo:cargo-core-api-generic:jar:sources:1.7.6
[INFO]    commons-discovery:commons-discovery:jar:sources:0.5
[INFO]    commons-logging:commons-logging:jar:sources:1.1.1
[INFO]    org.codehaus.cargo:cargo-core-api-container:jar:sources:1.7.6
[INFO]    org.codehaus.cargo:cargo-core-api-module:jar:sources:1.7.6
[INFO]    jaxen:jaxen:jar:sources:1.1.6
[INFO]    org.jdom:jdom:jar:sources:1.1.3
[INFO]    org.apache.geronimo.specs:geronimo-j2ee-deployment_1.1_spec:jar:sources:1.1
[INFO]    org.codehaus.cargo:cargo-documentation:jar:sources:1.7.6
[INFO]    org.codehaus.cargo:cargo-daemon-client:jar:sources:1.7.6
[INFO]    org.codehaus.cargo:cargo-core-api-util:jar:sources:1.7.6
[INFO]    org.apache.ant:ant:jar:sources:1.7.0
[INFO]    javax.servlet:javax.servlet-api:jar:sources:3.1.0
[INFO]    org.springframework.boot:spring-boot-starter-tomcat:jar:sources:2.0.1.RELEASE
[INFO]    org.apache.tomcat.embed:tomcat-embed-core:jar:sources:8.5.29
[INFO]    org.apache.tomcat.embed:tomcat-embed-el:jar:sources:8.5.29
[INFO]    org.apache.tomcat.embed:tomcat-embed-websocket:jar:sources:8.5.29
[INFO]
[INFO] The following files have NOT been resolved:
[INFO]    org.springframework.boot:spring-boot-starter:jar:2.0.1.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot:jar:2.0.1.RELEASE:compile
[INFO]    org.springframework:spring-context:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot-autoconfigure:jar:2.0.1.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot-starter-logging:jar:2.0.1.RELEASE:compile
[INFO]    ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO]    ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO]    org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO]    org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
[INFO]    org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO]    org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO]    javax.annotation:javax.annotation-api:jar:1.3.2:compile
[INFO]    org.springframework:spring-core:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework:spring-jcl:jar:5.0.5.RELEASE:compile
[INFO]    org.yaml:snakeyaml:jar:1.19:runtime
[INFO]    org.springframework.boot:spring-boot-configuration-processor:jar:2.0.1.RELEASE:compile (optional)
[INFO]    org.springframework.boot:spring-boot-starter-web:jar:2.0.1.RELEASE:compile
[INFO]    org.springframework.boot:spring-boot-starter-json:jar:2.0.1.RELEASE:compile
[INFO]    com.fasterxml.jackson.core:jackson-databind:jar:2.9.5:compile
[INFO]    com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO]    com.fasterxml.jackson.core:jackson-core:jar:2.9.5:compile
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.5:compile
[INFO]    com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.5:compile
[INFO]    com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.5:compile
[INFO]    org.hibernate.validator:hibernate-validator:jar:6.0.9.Final:compile
[INFO]    javax.validation:validation-api:jar:2.0.1.Final:compile
[INFO]    org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO]    com.fasterxml:classmate:jar:1.3.4:compile
[INFO]    org.springframework:spring-web:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework:spring-beans:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework:spring-webmvc:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework:spring-aop:jar:5.0.5.RELEASE:compile
[INFO]    org.springframework:spring-expression:jar:5.0.5.RELEASE:compile
[INFO]    org.apache.maven:maven-plugin-api:jar:3.5.0:compile
[INFO]    org.apache.maven:maven-model:jar:3.5.0:compile
[INFO]    org.codehaus.plexus:plexus-utils:jar:3.0.24:compile
[INFO]    org.apache.commons:commons-lang3:jar:3.7:compile
[INFO]    org.apache.maven:maven-artifact:jar:3.5.0:compile
[INFO]    org.eclipse.sisu:org.eclipse.sisu.plexus:jar:0.3.3:compile
[INFO]    javax.enterprise:cdi-api:jar:1.0:compile
[INFO]    javax.annotation:jsr250-api:jar:1.0:compile
[INFO]    javax.inject:javax.inject:jar:1:compile
[INFO]    org.eclipse.sisu:org.eclipse.sisu.inject:jar:0.3.3:compile
[INFO]    org.codehaus.plexus:plexus-component-annotations:jar:1.5.5:compile
[INFO]    org.codehaus.plexus:plexus-classworlds:jar:2.5.2:compile
[INFO]    org.codehaus.cargo:cargo-maven2-plugin:jar:1.7.6:compile
[INFO]    org.apache.maven:maven-archiver:jar:2.4.1:compile
[INFO]    org.codehaus.plexus:plexus-archiver:jar:1.0:compile
[INFO]    org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:compile
[INFO]    junit:junit:jar:4.12:compile
[INFO]    org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO]    classworlds:classworlds:jar:1.1-alpha-2:compile
[INFO]    org.codehaus.plexus:plexus-io:jar:1.0:compile
[INFO]    org.codehaus.plexus:plexus-interpolation:jar:1.13:compile
[INFO]    org.codehaus.cargo:cargo-core-api-generic:jar:1.7.6:compile
[INFO]    commons-discovery:commons-discovery:jar:0.5:compile
[INFO]    commons-logging:commons-logging:jar:1.1.1:compile
[INFO]    org.codehaus.cargo:cargo-core-api-container:jar:1.7.6:compile
[INFO]    org.codehaus.cargo:cargo-core-api-module:jar:1.7.6:compile
[INFO]    jaxen:jaxen:jar:1.1.6:compile
[INFO]    org.jdom:jdom:jar:1.1.3:compile
[INFO]    org.apache.geronimo.specs:geronimo-j2ee-deployment_1.1_spec:jar:1.1:compile
[INFO]    org.codehaus.cargo:cargo-documentation:jar:1.7.6:compile
[INFO]    org.codehaus.cargo:cargo-daemon-client:jar:1.7.6:compile
[INFO]    org.codehaus.cargo:cargo-core-api-util:jar:1.7.6:compile
[INFO]    org.apache.ant:ant:jar:1.7.0:compile
[INFO]    org.apache.ant:ant-launcher:jar:1.7.0:compile
[INFO]    javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO]    org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.1.RELEASE:provided
[INFO]    org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.29:provided
[INFO]    org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.29:provided
[INFO]    org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.29:provided
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:01 min
[INFO] Finished at: 2019-08-24T18:04:12+08:00
[INFO] ------------------------------------------------------------------------

D:\600-Git\profile\profile>
D:\600-Git\profile\profile>^Amvn dependency:sources -DdownloadSources=true -DdownloadJavadocs=true
'mvn' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\600-Git\profile\profile>

2.3.4 远程部署至tomcat

2.3.4.1 命令行部署

由于上面的配置文件中采用了 / 的路径,程序会部署至webapps/ROOT目录下。

mvn tomcat7:redeploy

可以在终端看到如下的输出:

"C:\Program Files\Java\jdk1.8.0_131\bin\java" -Dmaven.multiModuleProjectDirectory=D:\600-Git\profile\profile "-Dmaven.home=C:\Program Files\apache-maven-3.5.4" "-Dclassworlds.conf=C:\Program Files\apache-maven-3.5.4\bin\m2.conf" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\lib\idea_rt.jar=64025:C:\Program Files\JetBrains\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\apache-maven-3.5.4\boot\plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2018.1 -s C:\Users\lenovo\.m2\settings.xml org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:redeploy -f pom.xml -P war
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.example:profile:war:1.3-SNAPSHOT
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version (?) @ line 46, column 15
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO] 
[INFO] ------------------------< com.example:profile >-------------------------
[INFO] Building profile 1.3-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:redeploy (default-cli) > package @ profile >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ profile ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\600-Git\profile\profile\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ profile ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ profile ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ profile ---
[INFO] Packaging webapp
[INFO] Assembling webapp [profile] in [D:\600-Git\profile\profile\target\ROOT]
[INFO] Processing war project
[INFO] Webapp assembled in [260 msecs]
[INFO] Building war: D:\600-Git\profile\profile\target\ROOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.0.1.RELEASE:repackage (default) @ profile ---
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:redeploy (default-cli) < package @ profile <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ profile ---
[INFO] Deploying war to http://172.20.10.3:8080/  
Uploading: http://172.20.10.3:8080/manager/text/deploy?path=%2F&update=true
Uploaded: http://172.20.10.3:8080/manager/text/deploy?path=%2F&update=true (17581 KB at 19361.8 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:
[INFO] OK - Deployed application at context path [/]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:38 min
[INFO] Finished at: 2019-08-24T18:16:49+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

可以在终端中看到如下的输出:

[INFO] <<< tomcat7-maven-plugin:2.2:redeploy (default-cli) < package @ profile <<<
[INFO] 
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:redeploy (default-cli) @ profile ---
[INFO] Deploying war to http://172.20.10.3:8080/  
Uploading: http://172.20.10.3:8080/manager/text/deploy?path=%2F&update=true
Uploaded: http://172.20.10.3:8080/manager/text/deploy?path=%2F&update=true (17581 KB at 19361.8 KB/sec)

[INFO] tomcatManager status code:200, ReasonPhrase:
[INFO] OK - Deployed application at context path [/]

表示部署成功。
tomcat7 插件常用命令如下:
在这里插入图片描述
在这里插入图片描述

2.3.4.2 IDEA部署

使用IDEA部署就更加方便了,因为远程部署的相关信息已经在POM中了,只要使用插件即可。如下图所示:
在这里插入图片描述

2.3.5 Postman验证

在这里插入图片描述
然后在application.properties中修改环境为dev,重新远程部署
在这里插入图片描述

3总结

阿基米德说:“给我一个支点,我将把地球撬起。”这句话就是典型的工具思维,与其说他疯了,不如说他在感受到一种工具的伟大力量之后的狂喜。工具可以确保令错误,

工具思维就是你能意识到永远会有更好的工具能做得更快,处理的更多,失误更少,收益良多。在这种意识的前提下,你愿意主动去寻找甚至创建这样的工具,从而使你从繁重的工作中解脱出来。

与其忍受重复和繁重的劳动,不如痛下决心,花时间去提高自己编写测试工具的能力。这种能力是不会被时代淘汰的。

基于工具思维,我在这个周六研究了Maven的用法,同样是基于工具思维,我选择用SVN管理文档,Git管理代码,Markdown撰写日报,Nexus私服管理依赖,所有这些实践,不过是为了解放自己,减少自己重复的操作,使得开发更加轻便和优雅。

 本文主要介绍了tomcat7-maven-plugin的实践。希望能节省开发者的时间。

4代码下载

GitHub地址 : https://github.com/yanchenmochen/profile.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值