tomcat:run和tomcat7:run的区别,以及Apache Tomcat Maven Plugin 相关

起因

同事部署的maven项目,之前使用 jetty,现在切换到 tomcat,但是他使用的命令是 tomcat:run ,而不是 tomcat7:run,能启动,但出现问题了。

于是搜索了一番,想了解下二者有何区别,略有所得。

先说结论:

① maven是插件执行的框架,就是说实际上是调用插件执行具体的操作。

② maven可以通过 artifactId 的简写形式来调用插件(相见末尾的 更多3)。

tomcat-maven-plugin 版本的mojos就是②说的简写形式--就这么理解吧,其实不是)tomcat6tomcat7

tomcat-maven-plugin 版本(2.0之前),是不支持tomcat7的。

⑤ tomcat6仅支持Servlet2.5。

 

解析:

tomcat-maven-plugin 这个插件最早是the MojoHaus Project (previously known as Mojo@Codehaus)的一部分,后来 “Moved to the official Maven plugins and is now maintained in The Tomcat Project : tomcat-maven-plugin”。

就是说开始是一帮人自己搞的,后来被收编(或转让)了,由The Tomcat Project维护(开发?)。他们(MojoHaus)已不再负责了,原项目的页面直接 404 了,连基本的文档说明都不再提供。

The Tomcat Project主页上可以看到,该插件在这之后是从 2.0-beta-1 版本开始的,也不提供之前的版本及文档!!

所以,之前的版本应该是被放弃了,不该再被使用

但是,apache maven repo中仍然存在之前的版本,所以仍然可以下载。

 

2.0-beta-1 版本 的介绍页面上有如下介绍(节选):

Apache Tomcat Maven Plugin

This is the new home for the Tomcat Maven Plugin (previously hosted at Codehaus).

The version 2.0-beta-1 have the following new features:
    Apache Tomcat7 support
    Build an Executable War/Jar


groupId and Mojo name change Since version 2.0-beta-1 tomcat mojos has been renamed to tomcat6 and tomcat7 with the same goals.

 

根据这个我们可以知道,该插件之前肯定不支持tomcat7;而且现在的 mojos (应该是artifactId的一部分,见本文末尾的 更多3)也变成了tomcat6 和 tomcat7。

所以使用的话应该是这样的:

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat6-maven-plugin</artifactId>
          <version>2.0-beta-1</version>
        </plugin>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.0-beta-1</version>
        </plugin>
      </plugins>
    </pluginManagement>

注意:上面仅是示意。实际工作中建议使用新版本

推进:

本文到目前为止,只是说明应该使用什么,但仍未说明为什么执行 tomcat:runtomcat7:run 是不同的。 ~~

继续搜索maven的命令,又有如下所得。

① maven本质上是一个执行插件的框架(a plugin execution framework),所有工作都由插件完成!

② maven插件分为两类:buildreporting。相应的,应该分别在POM的<build></build><reporting></reporting>标签中配置。

③ maven本身是没有tomcat相关的命令的,这些命令均来自maven的tomcat插件。

④ 从本文末尾的 快速启动maven项目 链接中可以看到,使用 mvn tomcat6/7:run,可以快速将项目部署到插件内置的tomcat中,并启动。

⑤ maven可以通过 artifactId 的简写形式来调用插件(相见末尾的 更多3)。

 

至此,差不多已经明白了为什么需要运行tomcat7:run。可惜的是,由于找不到之前的版本,所以始终无法得知tomcat:run 的具体操作。

--但我们只需要知道该【该插件之前肯定不支持tomcat7;而且现在的 mojos 也变成了tomcat6 和 tomcat7】即可。

 

更多1:

通过本文末尾的 maven plugin页面 还可以有一些所得(关于插件--也是命令):

  failsafe:Run the JUnit integration tests in an isolated classloader.

  install:Install the built artifact into the local repository.

  resources:Copy the resources to the output directory for including in the JAR.

  surefire:Run the JUnit unit tests in an isolated classloader.

更多2:

通过本文末尾的 maven plugin开发 可以得知,一个插件的执行有两种格式:完整格式和缩略格式。

完整格式:mvn groupId:artifactId:version:goal

缩略格式这里只说两种情况:

  省略version,则执行本地已安装的最新的版本。

    如果插件的artifactId 符合格式:${prefix}-maven-plugin (or maven-${prefix}-plugin),则可执行 mvn ${prefix}

所以,tomcat6-maven-plugin 或者 tomcat7-maven-plugin,应该使用 tomcat6:run 或者 tomcat7:run

 

更多3:

还是通过本文末尾的 maven plugin开发 可以得知,

  groupId 是这个插件所在组的Id--应该符合命名规范。

  artifactId 是这个插件的名字。(直译:工艺品Id)

  What is a Mojo? A mojo is a Maven plain Old Java Object. Each mojo is an executable goal in Maven, and a plugin is a distribution of one or more related mojos.

另外,这里的groupId 命名规范 是,<yourplugin>-maven-plugin 。因为 maven-<yourplugin>-plugin 是Apache保留的,会侵权~

 

结束

可以逆推原因了:因为使用tomcat:run,所以用的是之前的版本,肯定不支持tomcat7,应该是tomcat6。

但是tomcat6仅支持Servlet2.5,而我们用的是servlet3,所以肯定会出问题。

 

几个地址,方便以后查看:

  tomcat maven plugin主页

  maven插件配置向导

  tomcat6-maven-plugin插件使用

  快速启动maven项目(v2.2)

  部署相关命令(v2.2)

  容器(tomcat)相关信息(v2.2)

  maven plugin页面

    maven plugin开发

  tomcat6 文档

  tomcat7 文档

posted on 2016-07-16 20:19  LarryZeal  阅读(10180)  评论(0编辑  收藏  举报

导航