起因:
同事部署的maven項目,之前使用 jetty,現在切換到 tomcat,但是他使用的命令是 tomcat:run ,而不是 tomcat7:run,能啟動,但出現問題了。
於是搜索了一番,想了解下二者有何區別,略有所得。
先說結論:
① maven是插件執行的框架,就是說實際上是調用插件執行具體的操作。
② maven可以通過 artifactId 的簡寫形式來調用插件(相見末尾的 更多3)。
③ tomcat-maven-plugin 新版本的mojos(就是②說的簡寫形式--就這么理解吧,其實不是)是tomcat6 和 tomcat7 。
④ 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:run 和 tomcat7:run 是不同的。 ~~
繼續搜索maven的命令,又有如下所得。
① maven本質上是一個執行插件的框架(a plugin execution framework),所有工作都由插件完成!
② maven插件分為兩類:build 和 reporting。相應的,應該分別在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,所以肯定會出問題。
幾個地址,方便以后查看:
