Rapid Development 快速开发

为了快速开发,首先是需要一款趁手的 IDE。 IDE 的选取以个人喜好为主,只要是自己熟悉的就好。本书的实例是使用 Eclipse 作为 IDE。

安装 M2Eclipse 插件

一般 Eclipse 都集成 Maven 的插件,如果没有,要想在 Eclipse 中使用 Maven,需要 安装 M2Eclipse 插件。有关 M2Eclipse 的安装可以参考http://www.eclipse.org/m2e/。安装完后就能导入 Maven 项目,使用 Maven 的命令行。

4. Rapid Development 快速开发 - 图1

4. Rapid Development 快速开发 - 图2

内嵌 Servlet 容器

放了方便运行项目,可以将 Servlet 容器嵌入进项目中。下面谈下几种 常见的 Servlet 容器的嵌入。

嵌入 Tomcat

设置插件

  1. <plugin>
  2. <groupId>org.apache.tomcat.maven</groupId>
  3. <artifactId>tomcat7-maven-plugin</artifactId>
  4. <version>${tomcat7.version}</version>
  5. </plugin>

执行

  1. mvn tomcat7:run

4. Rapid Development 快速开发 - 图3

项目启动成功,可以看到输出:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
  4. [INFO]
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Building servlet-container 1.0-SNAPSHOT
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO]
  9. [INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container >>>
  10. [INFO]
  11. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ servlet-container ---
  12. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  13. [INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\resources
  14. [INFO]
  15. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ servlet-container ---
  16. [INFO] Compiling 1 source file to D:\workspaceGithub\rest-in-action\samples\servlet-container\target\classes
  17. [INFO]
  18. [INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container <<<
  19. [INFO]
  20. [INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container ---
  21. [INFO] Running war on http://localhost:8080/servlet-container
  22. [INFO] Creating Tomcat server configuration at D:\workspaceGithub\rest-in-action\samples\servlet-container\target\tomcat
  23. [INFO] create webapp with contextPath: /servlet-container
  24. 三月 02, 2015 2:25:06 下午 org.apache.coyote.AbstractProtocol init
  25. INFO: Initializing ProtocolHandler ["http-bio-8080"]
  26. 三月 02, 2015 2:25:07 下午 org.apache.catalina.core.StandardService startInternal
  27. INFO: Starting service Tomcat
  28. 三月 02, 2015 2:25:07 下午 org.apache.catalina.core.StandardEngine startInternal
  29. INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
  30. 三月 02, 2015 2:25:11 下午 org.apache.coyote.AbstractProtocol start
  31. INFO: Starting ProtocolHandler ["http-bio-8080"]

在浏览器里访问 http://localhost:8080/servlet-container/ 就能看到主页面了。

4. Rapid Development 快速开发 - 图4

嵌入 Jetty

设置插件

  1. <plugin>
  2. <groupId>org.eclipse.jetty</groupId>
  3. <artifactId>jetty-maven-plugin</artifactId>
  4. <version>${jetty.version}</version>
  5. </plugin>

注意:使用该插件需要 Maven 3 和 Java 1.7 及以上版本

执行

  1. mvn jetty:run

4. Rapid Development 快速开发 - 图5

项目启动成功,可以看到输出:

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
  4. [INFO]
  5. [INFO] ------------------------------------------------------------------------
  6. [INFO] Building servlet-container 1.0-SNAPSHOT
  7. [INFO] ------------------------------------------------------------------------
  8. [INFO]
  9. [INFO] >>> jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container >>>
  10. [INFO]
  11. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ servlet-container ---
  12. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  13. [INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\resources
  14. [INFO]
  15. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ servlet-container ---
  16. [INFO] Nothing to compile - all classes are up to date
  17. [INFO]
  18. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ servlet-container ---
  19. [INFO] Using 'UTF-8' encoding to copy filtered resources.
  20. [INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\test\resources
  21. [INFO]
  22. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ servlet-container ---
  23. [INFO] No sources to compile
  24. [INFO]
  25. [INFO] <<< jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container <<<
  26. [INFO]
  27. [INFO] --- jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container ---
  28. 2015-03-02 15:06:54.654:INFO::main: Logging initialized @1764ms
  29. [INFO] Configuring Jetty for project: servlet-container
  30. [INFO] webAppSourceDirectory not set. Trying src\main\webapp
  31. [INFO] Reload Mechanic: automatic
  32. [INFO] Classes = D:\workspaceGithub\rest-in-action\samples\servlet-container\target\classes
  33. [INFO] Context path = /
  34. [INFO] Tmp directory = D:\workspaceGithub\rest-in-action\samples\servlet-container\target\tmp
  35. [INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
  36. [INFO] Web overrides = none
  37. [INFO] web.xml file = file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/WEB-INF/web.xml
  38. [INFO] Webapp directory = D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\webapp
  39. 2015-03-02 15:06:54.713:INFO:oejs.Server:main: jetty-9.2.9.v20150224
  40. 2015-03-02 15:06:55.885:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@2863c{/,file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/,AVAILABLE}{file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/}
  41. 2015-03-02 15:06:55.886:WARN:oejsh.RequestLogHandler:main: !RequestLog
  42. [INFO] Started Jetty Server
  43. 2015-03-02 15:06:55.911:INFO:oejs.ServerConnector:main: Started ServerConnector@1dde93{HTTP/1.1}{0.0.0.0:8080}
  44. 2015-03-02 15:06:55.912:INFO:oejs.Server:main: Started @3022ms

在浏览器里访问 http://localhost:8080就能看到主页面了。这里 Jetty 启动项目是默认是不显示项目名称的。

4. Rapid Development 快速开发 - 图6

Source code 源码

servlet-container

参考: