maven 插件系列1 :tomcat插件 集成

Maven已经是Java的项目管理常用方式,本文将介绍,Maven如何使用Tomcat7插件
tomcat7-maven-plugin 插件官网:http://tomcat.apache.org/maven-plugin.html
下图为tomcat7插件的命令详细:

Paste_Image.png
命令

| 命令| 描述|
| :------------- :|:-------------:|
| tomcat7:run| 运行当前项目 |
| tomcat7:deploy| 部署当前项目 |
| tomcat7:redeploy|重新部署项目 |
|tomcat7:exec-war| 创建一个可执行的jar文件,允许使用java -jar mywebapp.jar 运行web项目 |
|tomcat7:undeploy| 取消部署一个war |
|tomcat7:help| 在tomcat7-maven-plugin显示帮助信息 |

1.本地运行(tomcat7:run)
pom.xml文件配置
<!-- 本地环境使用 -->
<plugin>   
  <groupId>org.apache.tomcat.maven</groupId>   
  <artifactId>tomcat7-maven-plugin</artifactId>   
  <version>2.1</version>   
  <configuration>      
    <hostName>localhost</hostName>    <!--   Default: localhost -->  
    <port>8080</port>    <!-- 启动端口 Default:8080 --> 
    <path>/api</path>    <!-- 访问应用路径  Default: /${project.artifactId}-->  
    <uriEncoding>UTF-8</uriEncoding>      <!-- uri编码 Default: ISO-8859-1 -->
  </configuration>
</plugin>
2.远程部署(tomcat7:deploy)
2.1 pom.xml配置
<!-- 本地环境使用 -->
<plugin>    
  <groupId>org.apache.tomcat.maven</groupId>    
  <artifactId>tomcat7-maven-plugin</artifactId>    
  <configuration>        
    <url>http://192.168.1.110:8080/manager/text</url>        
    <server>tomcat</server>        
    <username>xxxx</username>        
    <password>xxxxx</password>        
    <path>/api</path>   
 </configuration>
</plugin>
2.2 其他配置

2.2.1 .在tomcat中配置用户权限(用户名密码自行修改:本例是 tomcat,tomcat)
我们需要实现热部署,自然就需要通过maven操作tomcat,所以就需要maven取得操作tomcat的权限。
在tomcat的安装目录下,修改conf / tomcat-user.xml文件,在<tomcat-users> 节点下面增加如下配置:

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

2.2.2 在maven中添加server,配置tomcat的管理员帐号密码
maven要操作tomcat,那么maven就要拿到tomcat的管理员帐号和密码才能够使用.
设置maven配置的setting.xml文件.在<server> 节点下面增加如下配置:

<server> 
  <id>admin</id> 
  <username>tomcat</username> 
  <password>tomcat</password> 
</server>
2.3 注意事项
  • 部署之前应先启动tomcat服务器,否则报Connection refused错误
    错误信息:[ERROR]Failed to execute goal org.apache.tomcat.maven: tomcat7-maven-plugin: 2.0- SNAPSHOT: deploy (default-cli) on project helloworld: Cannot invoke Tomcat manager: Connection refused: connect -> [Help 1]
  • 第一次部署使用tomcat7:deploy 以后使用tomcat7:redeploy
3.显示帮助信息(tomcat7:help)
This plugin has 14 goals:

tomcat7:deploy
  Deploy a WAR to Tomcat.

tomcat7:deploy-only
  Deploy a WAR to Tomcat without forking the package lifecycle.

tomcat7:exec-war
  Create a self executable jar file containing all necessary Apache Tomcat
  classes. This allows for using just java -jar mywebapp.jar to run your webapp
  without needing to install a Tomcat instance. More details here.

tomcat7:exec-war-only
  Same as exec-war goal without forking the package lifecycle.

tomcat7:help
  Display help information on tomcat7-maven-plugin.
  Call mvn tomcat7:help -Ddetail=true -Dgoal=<goal-name> to display parameter
  details.

tomcat7:redeploy
  Redeploy a WAR in Tomcat. (Alias for the deploy goal with its update parameter
  set to true.)

tomcat7:redeploy-only
  Redeploy a WAR in Tomcat without forking the package lifecycle. (Alias for the
  deploy-only goal with its update parameter set to true.)

tomcat7:run
  Runs the current project as a dynamic web application using an embedded Tomcat
  server.

tomcat7:run-war
  Runs the current project as a packaged web application using an embedded
  Tomcat server.

tomcat7:run-war-only
  Same as run-war goal without forking the package cycle.

tomcat7:shutdown
  Shuts down all possibly started embedded Tomcat servers. This will be
  automatically done through a shutdown hook or you may call this Mojo to shut
  them down explictly.
  
  By default the shutdown goal is not bound to any phase. For integration tests
  you might want to bind it to post-integration-test.

tomcat7:standalone-war
  This Mojo will create an executable war file with embedded Tomcat that is also
  capable of being deployed elsewhere.

tomcat7:standalone-war-only
  This Mojo will create an executable war file with embedded Tomcat that is also
  capable of being deployed elsewhere.

tomcat7:undeploy
  Undeploy a WAR from Tomcat.
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容