Maven已经是Java的项目管理标配,如何在JavaEE开发使用Maven调用Web应用,是很多同学关心的问题。本文将介绍,Maven如何介绍Tomcat插件。

Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同。其使用也只能针对当前应用有效。

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

tomcat7-maven-plugin 插件官网:​​http://tomcat.apache.org/maven-plugin.html​​。

 

tomcat7-maven-plugin 使用(常用)

配置

在pom.xm 加入以下xml。

<build>
<!-- 配置了很多插件 -->
<plugins>

<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/SSM</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>


 

 

 

注意上面有底色部分,简要说明一下:

path  是访问应用的路径,例如我上面写的是/SSM,则访问路径是     http://localhost/SSM

port 是tomcat 的端口号  

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

Server 指定tomcat名称。

 IDEA插件运行(重点)

(1)如果在pom.xml中配置了Tomcat插件,在右边的Maven Project中会出现对应的插件,例如:

pom.xml:

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/SSM</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>


 

【IDEA】IDEA集成Tomcat7插件运行项目_apache

 

(2)此时运行的时候只需要右击右边的命令,然后可以以run模式或者debug模式启动项目,debug模式可以打断点进行调试

 

【IDEA】IDEA集成Tomcat7插件运行项目_tomcat_02

 

或者:

点击Run-》Edit Configurations后搜索maven

【IDEA】IDEA集成Tomcat7插件运行项目_tomcat_03

 

 

 

 

 

启动之后访问项目即可

 

 

IDEA使用小结:

  1.运行之后可以点击上面的箭头然后点击Save.... 将运行方式保存起来,下次选择相应的命令然后点击右边的运行按钮(三角)或者调试按钮(虫子)即可以对应的模式运行命令。

    (1)save之前:

        

【IDEA】IDEA集成Tomcat7插件运行项目_tomcat_04

 

     (2)save之后选择对应运行的命令然后以对应模式启动即可:

        

【IDEA】IDEA集成Tomcat7插件运行项目_tomcat_05

 

     (3)通过三角或虫子上的小绿点可以判断是以哪种模式启动的:

        

【IDEA】IDEA集成Tomcat7插件运行项目_apache_06

 

   

  2.如果项目中加上tomcat-maven-plugin插件发现点击右边的maven projects也会多出此插件:

例如下面配置集成了两个tomcat插件:

pom.xml

<build>
<!-- 配置了很多插件 -->
<plugins>

<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<!-- tomcat6插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<path>/SSM</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat6</server>
</configuration>
</plugin>
<!-- tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>80</port>
<path>/SSM</path>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat7</server>
</configuration>
</plugin>
</plugins>
</build>


 

 

 发现右边会两个插件同时出现,也就是右边的插件随pom.xml变动,此时选择对应的插件运行即可

 

【IDEA】IDEA集成Tomcat7插件运行项目_maven_07

 

 

 

 

 

下面介绍几个常用的Goal

命令 描述 tomcat7:deploy 部署一个web war包 tomcat7:reload 重新加载web war包 tomcat7:start 启动tomcat tomcat7:stop 停止tomcat tomcat7:undeploy 停止一个war包 tomcat7:run 启动嵌入式tomcat ,并运行当前项目


 

 

 

 

tomcat-maven-plugin  插件使用

配置

在pom.xm 加入以下xml。

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<path>/wp</path>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
<server>tomcat6</server>
</configuration>
</plugin>


具体配置一样。

插件使用 

在这里要注意一下,该插件命名方式有些不同,比如启动tomcat ,对应的目标命令是: tomcat:run ,同样,其它命令也是这样,需要更改为:tomcat:<插件执行点>