maven-tomcat-plugin使用

Maven已经是Java的项目管理标配,如何在开发时热部署和生产时快速部署maven项目是很多人关心的问题。

Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同,现在介绍第一种。

tomcat-maven-plugin 插件官网:http://mojo.codehaus.org/tomcat-maven-plugin/plugin-info.html。


tomcat-maven-plugin  插件使用

首先在tomcat下激活管理员

打开tomcat-user.xml,加入如下代码

  <role rolename="manager-gui"/>  
  <role rolename="manager-script"/>  
  <role rolename="manager-jmx"/>  
  <role rolename="manager-status"/>  
  <user username="admin" password="admin" roles="manager-gui"/> 
接着在maven的settings.xml加入如下代码

<server>    
       <id>admin</id>    
       <username>admin</username>    
       <password>admin</password>    
</server> 

这里的id标签和下一步的server标签中的要一样

然后在项目的pom.xm 加入以下xml。

    <build>     
       <plugins>          
           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <path>/test</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/manager/html</url>
                    <server>admin</server>
                </configuration>
            </plugin>
        </plugins>
    </build>

简要说明一下:

path  是访问应用的路径

port 是tomcat 的端口号

uriEncoding  URL按UTF-8进行编码,这样就解决了中文参数乱码。

Server 指定tomcat名称

url:部署到外部tomcat时,要指定的tomcat管理界面的地址,上面的是tomcat6的,tomcat7的是http://localhost:8080/manager/text

配置就这么简单,基本搞掂,下面看看如何使用。

插件运行

插件的前缀是tomcat,常用命令可以使用命令:mvn help:describe -Dplugin=org.codehaus.mojo:tomcat-maven-plugin:1.1获取帮助,在开发时一般使用run

下面介绍几个常用的Goal

tomcat:deploy 部署一个web war包
tomcat:reload 重新加载web war包

tomcat:start

启动tomcat

tomcat:stop

停止tomcat

tomcat:undeploy

停止一个war包
tomcat:run 启动嵌入式tomcat ,并运行当前项目

你可能感兴趣的:(maven-tomcat-plugin使用)