如果你使用的是Idea编辑器社区版,你可选择配置Tomcat的Maven插件的方式来使用Tomcat。
<!-- Tomcat --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path>/${project.artifactId}</path> </configuration> </plugin>
配置tomcat的插件,更新maven后可以在插件里看到有tomcat,并且可以使用它的run命令:
你可以直接双击执行它,也可以右键debug执行,tomcat启动后log如下:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building chapter1 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ chapter1 >>>
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ chapter1 ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.3:compile (default-compile) @ chapter1 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\IdeaProjects\Smart4jFramework\Chapter1\target\classes
[INFO]
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ chapter1 <<<
[INFO]
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ chapter1 ---
[INFO] Running war on http://localhost:8080/chapter1
[INFO] Using existing Tomcat server configuration at D:\IdeaProjects\Smart4jFramework\Chapter1\target\tomcat
[INFO] create webapp with contextPath: /chapter1
十一月 01, 2016 10:43:24 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
十一月 01, 2016 10:43:24 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Tomcat
十一月 01, 2016 10:43:24 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.47
十一月 01, 2016 10:43:26 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
注意一下:
tomcat的maven插件版本好像只到tomcat7,虽然在maven官方可以看到有tomcat8的插件,但导入后并不显示第二张图上的插件,并且有错误提示。而且官方的例子也是到tomcat7为止。
关于tomcat的Maven官方插件库:https://mvnrepository.com/artifact/org.apache.tomcat.maven
Tomcat的官方例子:https://tomcat.apache.org/maven-plugin-trunk/
对于tomcat maven plugin的一些设置项,请参看官方文档:
https://tomcat.apache.org/maven-plugin-trunk/run-mojo-features.html