使用Maven Jetty插件
盡管很長一段時間以來我一直在使用Maven,但直到最近我才使用過Jetty插件。 為了能夠測試REST客戶端,我創建了一個Servlet,向我顯示了所有傳入的參數和帶有傳入請求的標頭。 為了在容器中運行servlet,我決定嘗試使用Maven Jetty插件 。 所以首先我使用特定的Maven原型創建一個Web應用程序:
這將導致完整的項目和以下日志記錄:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<< [INFO] [INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Interactive mode Downloading: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar Downloaded: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 5.2 KB/sec) Downloading: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom Downloaded: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 1.1 KB/sec) [INFO] Using property: groupId = net.pascalalma [INFO] Using property: artifactId = rest-service [INFO] Using property: version = 1.0.0-SNAPSHOT [INFO] Using property: package = net.pascalalma Confirm properties configuration: groupId: net.pascalalma artifactId: rest-service version: 1.0.0-SNAPSHOT package: net.pascalalmaY: : Y [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: net.pascalalma [INFO] Parameter: packageName, Value: net.pascalalma [INFO] Parameter: package, Value: net.pascalalma [INFO] Parameter: artifactId, Value: rest-service [INFO] Parameter: basedir, Value: /Users/pascal/projects [INFO] Parameter: version, Value: 1.0.0-SNAPSHOT [INFO] project created from Old (1.x) Archetype in dir: /Users/pascal/projects/rest-service [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 13.057s [INFO] Finished at: Sun Feb 03 17:13:33 CET 2013 [INFO] Final Memory: 7M/81M [INFO] ------------------------------------------------------------------------ MacBook-Air-van-Pascal:projects pascal$接下來,我將servlet代碼添加到項目中:
package net.pascalalma.servlets;import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/**** @author pascal*/ public class TestRestServlet extends HttpServlet {public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {PrintWriter out = response.getWriter();out.println('GET method called');out.println('parameters:\n ' + parameters(request));out.println('headers:\n ' + headers(request));}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {PrintWriter out = response.getWriter();out.println('POST method called');out.println('parameters: ' + parameters(request));out.println('headers: ' + headers(request));}public void doDelete(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {PrintWriter out = response.getWriter();out.println('Delete method called');}private String parameters(HttpServletRequest request) {StringBuilder builder = new StringBuilder();for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {String name = (String) e.nextElement();builder.append('|' + name + '->' + request.getParameter(name)+'\n');}return builder.toString();}private String headers(HttpServletRequest request) {StringBuilder builder = new StringBuilder();for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) {String name = (String) e.nextElement();builder.append('|' + name + '->' + request.getHeader(name)+'\n');}return builder.toString();} }并在“ web.xml”中配置servlet。 順便說一下,生成的“ web.xml”無法顯示在我的Netbeans版本(v7.2.1)中。 我收到消息:
Web應用程序版本不受支持。 將web.xml升級到2.4或更高版本,或使用以前的NetBeans版本
為了解決這個問題,我修改了web.xml,使其從以下命名空間聲明開始:
<web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns='http://java.sun.com/xml/ns/javaee' xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd' xsi:schemaLocation='http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd' id='WebApp_ID' version='2.5'>接下來,將servlet添加到修改后的“ web.xml”中:
<?xml version='1.0' encoding='UTF-8'?> ...<display-name>Archetype Created Web Application</display-name><servlet><servlet-name>TestRestServlet</servlet-name><servlet-class>net.pascalalma.servlets.TestRestServlet</servlet-class></servlet><servlet-mapping><servlet-name>TestRestServlet</servlet-name><url-pattern>/TestRestServlet</url-pattern></servlet-mapping> ...現在一切準備就緒,可以測試servlet。 正如我之前所說的,我將為此使用Jetty插件。 要將插件添加到項目中,只需將以下內容放入“ pom.xml”中:
<plugins><plugin><groupId>org.mortbay.jetty</groupId><artifactId>jetty-maven-plugin</artifactId><configuration><scanIntervalSeconds>10</scanIntervalSeconds><contextPath>/</contextPath><scanIntervalSeconds>10</scanIntervalSeconds><stopKey>STOP</stopKey><stopPort>8005</stopPort><port>8080</port></configuration></plugin> </plugins>現在,我可以在終端中運行命令“ mvn jetty:run”,以使容器運行servlet。 日志應該以以下內容結尾:
.... 2013-02-19 09:54:53.044:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080 [INFO] Started Jetty Server [INFO] Starting scanner at interval of 10 seconds.</code>現在,如果您打開瀏覽器并轉到該URL'http:// localhost:8080 / TestRestServlet?bla = true&#8217 ; 您將看到servlet的運行并輸出到瀏覽器:
GET method called parameters: |bla->trueheaders: |DNT->1 |Host->localhost:8080 |Accept->text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 |Accept-Charset->ISO-8859-1,utf-8;q=0.7,*;q=0.3 |Accept-Language->en-US,en;q=0.8 |User-Agent->Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17 |Connection->keep-alive |Cache-Control->max-age=0 |Accept-Encoding->gzip,deflate,sdch 注意事項:如您所見,在插件配置中,為方便起見,我添加了一些額外的參數。 因此,容器將每隔10秒檢查一次servlet中的更改,因此我不必在每次更改servlet之后重新啟動Jetty容器。 要停止容器,您現在可以在另一個終端會話中輸入命令'mvn jetty:stop -DstopPort = 8005 -DstopKey = STOP'。 順便說一句,請確保將插件命名為“ jetty-maven-plugin”,而不是“ maven-jetty-plugin”,因為那樣您將使用該插件的舊版本,而該插件不會使用配置參數(是的,非常令我感到困惑和沮喪)。
參考:在The Pragmatic Integrator博客上使用我們JCG合作伙伴 Pascal Alma的Maven Jetty插件 。
翻譯自: https://www.javacodegeeks.com/2013/03/using-maven-jetty-plugin.html
總結
以上是生活随笔為你收集整理的使用Maven Jetty插件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海蓝量化是什么公司?
- 下一篇: 如果微信理财通里的钱没有了怎么办?