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.

推荐阅读更多精彩内容