尽管很长一段时间以来我一直在使用Maven,但直到最近我才使用过Jetty插件。 为了能够测试REST客户端,我创建了一个Servlet,向我显示了所有传入的参数和带有传入请求的标头。 为了在容器中运行servlet,我决定尝试使用Maven Jetty插件 。 所以首先我使用特定的Maven原型创建一个Web应用程序:

mvn archetype:generate -DgroupId=net.pascalalma -DartifactId=rest-service -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp

这将导致完整的项目和以下日志记录:

[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->true
headers:
|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插件相关推荐

  1. 【转】Maven Jetty 插件的问题(css/js等目录死锁)的解决

    Maven Jetty 插件的问题(css/js等目录死锁,不能自动刷新)的解决: 1. 打开下面的目录:C:\Users\用户名\.m2\repository\org\eclipse\jetty\j ...

  2. eclipse maven jetty插件方式启动项目

    2019独角兽企业重金招聘Python工程师标准>>> 1. 2.点击run即可启动项目 参考:maven命令具体含义请自行百度.例子:maven clean的作用 /  maven ...

  3. maven jetty 插件 允许修改 js

    <!--允许修改js,css--><servlet><servlet-name>default</servlet-name><init-param ...

  4. Jetty在win10上的配置,IDEA中配置Jetty,Maven中配置Jetty插件,Eclipse中配置Jetty插件及其使用,通过java代码内嵌Jetty Server

    1.下载Jetty 下载地址:http://www.eclipse.org/jetty/download.html 2.在windows上运行jetty 一.将下载的jetty解压到D:\instal ...

  5. Maven配置tomcat和jetty插件来运行项目

    针对eclipse中的Run on Server有些情况下并不是那么好操作,比如配置maven下的springmvc插件,如果使用此方法运行会很容易出现组件缺少导致错误出现一大堆的问题. 那么针对这种 ...

  6. maven学习:jetty插件与Tomcat插件

    1.jetty插件 要测试的web的pom里面,加入jetty的插件配置,示例如下: <!-- jetty插件 --> <plugin><groupId>org.m ...

  7. java maven jetty_maven jetty 插件使用

    本机环境 JDK 7 Maven 3.2 Jetty 9.2 Eclipse Luna pom.xml 配置 在你的 pom.xml 文件中添加 jetty 插件的描述信息(查看Jetty更多的版本信 ...

  8. Eclipse运行maven的jetty插件内存溢出解决

    系统运行在MAVEN中的jetty插件下,当在ECLIPSE运 clean jetty:run时系统提示 OutOfMemoryError: PermGen space. 解决办法: 设置run as ...

  9. java jetty eclipse_用Eclipse+Maven+Jetty构建Java Web开发环境(详细笔记)

    (软件环境) 『系统』Windows 10 x64 『JAVA』JDK 1.8.0_91 『Eclipse』 Eclipse-oxygen 『Maven』 apache-maven-3.6.3 『Je ...

最新文章

  1. mac终端python删除键不能用_我把mac 系统下的python目录删掉了怎么办
  2. 牛顿迭代法(Newton's Method)
  3. python 遍历_python中使用iterrows()对dataframe进行遍历的示例
  4. SCU3033 Destroying a Painting(最小费用最大流)
  5. 经struts2中的action后,jsp中css失效的问题
  6. AlexNet 和 VGG-Net的区别
  7. 466.统计重复个数
  8. 利用FreeMarker生成java源代码
  9. MySQL查询指定日期时间语句大全
  10. 12306排队是什么意思_12306抢票显示排队中怎么办
  11. 报错解决:DataLossError: Unable to open table file Data loss: not an sstable (bad magic number):
  12. QQ登录界面测试用例设计:
  13. 【ora-12528】数据库服务器重启之后,出现ora-12528:所有适用例程都无法建立新连接
  14. 专题:手把手学习硬件基础------8、驱动电路
  15. 华为云区块链的跨云联通能力构建
  16. mysql 设置密码出现ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
  17. 电脑如何同时远程控制多台手机
  18. Pedersen承诺
  19. Good Vegetable 4级算法题 分值: [320/3120] 问题: [8/78]
  20. 使用树莓源驱动128*128彩屏(SSD1351)

热门文章

  1. 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis
  2. 离线配置xml的文档类型定义文件(xml语法规则) dtd
  3. 纯干货,Spring-data-jpa详解,全方位介绍。
  4. aws使用技巧_AWS:避免那些“神圣的法案”时刻的一些技巧
  5. 基于Spring Boot配置文件的日志记录示例样本
  6. apache.camel_Apache Camel 2.18 –即将推出的功能的亮点
  7. osgi:install_OSGi服务测试助手:ServiceCollector
  8. js 使用多态替换条件语句_用多态和组成替换多个条件
  9. spring bean依赖_Spring @Configuration并将bean依赖项作为方法参数注入
  10. 数据库班级字段怎么定义名称_班级名称