配置jetty maven插件

配置Jetty Maven插件

Jetty Maven插件用于快速开发和测试。你可以将它添加到任何Maven Web项目。插件会定期扫描你项目的变化,并在检测到变化后自动重新部署应用。这种通过消除构建和部署步骤的方式使得开发周期更有效率:您使用IDE来编辑项目,运行中的jetty容器会自动编译它们,你可以立即进行测试。

 

快速开始:启动并运行

首先,添加 jetty-maven-pluginpom.xml配置文件

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.1-SNAPSHOT</version>
</plugin

然后在项目根目录下运行命令:

mvn jetty:run

 

就可以启动jetty,你通过通过在http://localhost:8080访问你的web项目。

Jetty继续运行直到你停止它。当它运行时,它会周期性地扫描您的项目文件的更改,如果您保存更改并重编译类文件,Jetty重部署你的应用,你可以立即测试所做的更改。

你可以终止的插件 ctrl-c在终端窗口中运行。

支持的目标

Jetty Maven插件有许多不同的Maven目标。最有用的是run目标,通过它可以快速启动你的web项目。还有其他的目标,可以帮助你完成不同的任务。例如,您可能需要运行你的应用在一个分叉的Jetty实例,而不是正在运行Maven进程;或者你可能需要更细粒度的控制Maven生命周期阶段,按您希望的那样部署你的web应用。有不同的目标来完成这些任务。

查看所Maven插件的所有目标,你可以执行:

mvn jetty:help

 

查看特定目标更详细的参数列表,执行如下命令:

mvn jetty:help -Ddetail=true -Dgoal=goal-name

 

配置Jetty容器

这些配置元素用于设置jetty运行环境。他们被大部分目标共用:

httpConnector

可选的。如果没有指定,jetty将创建一个监听端口8080的ServerConnector实例。你可以使用命令行通过系统属性jetty.http.port改变这种默认端口号。例如, mvn -Djetty.http.port=9999 jetty:run。或者,您可以使用此元素配置ServerConnector信息。以下是有效配置有子元素:

port

连接器监听的端口号。默认情况下它是8080。

host

要监听的特定接口连接器。默认情况下,所有接口。

name

连接器的名称,它只有在一个特殊的连接器上配置上下文时才有用。

idleTimeout

连接的最大空闲时间。

soLinger

Socket延迟时间。

你可以使用一个标准的jetty xml配置文件来代替配置连接器,文件的位置配置在jettyXml参数里。注意,从jetty 9.0开始,不再可以直接在pom.xml配置https连接器,你必须使用jetty配置文件。

jettyXml

可选的。一个逗号分隔的列表的位置 jetty xml文件除了应用于任何插件配置参数。您可以使用它如果你有其他webapps,处理程序,特定类型的连接器等,部署,或者你有其他码头的对象不能从插件配置。

scanIntervalSeconds

之间的停顿在几秒钟内扫描的webapp检查更改,如果检测到任何自动热部署。 在默认情况下这是0,禁用热部署扫描。使其数量大于0。

重新加载

默认值是“自动”,用于与一个非零 scanIntervalSeconds当检测到变化导致自动热部署。设置为“手动”而不是触发扫描输入换行符在控制台运行插件。这可能是有用的,当你在做一系列的变化,你想忽略,直到你做完了。在使用,使用 reload参数。

dumpOnStart

可选的。默认值是错误的。如果这是真的,那么码头将倾倒服务器结构开始。

loginServices

可选的。的列表 org.eclipse.jetty.security.LoginService实现。请注意,没有默认域。如果你用在你的领域 web.xml你可以在这里指定相应的领域。你可以在jetty中配置登录服务的xml文件,并将它的位置添加到 jettyXml参数。

requestLog

可选的。的实现 org.eclipse.jetty.server.RequestLog请求日志界面。一个实现这方面的机子格式是可用的org.eclipse.jetty.server.NCSARequestLog。其他方法来配置RequestLog有三种:

  • 在jetty xml配置文件,如中指定 jettyXml参数。

  • 在一个上下文xml配置文件,如中指定 contextXml参数。

  •  webApp元素。

See Configuring Request Logs for more information.

服务器

自jetty-9.3.1可选。这将配置的一个实例 org.eclipse.jetty.server.Server插件的使用,但是它通常没有必要配置,插件会自动为你配置一个。特别是,如果你使用jettyXml元素,那么你一般不想定义这个元素,正如你可能使用jettyXml文件来配置一个服务器与一种特殊的构造函数参数,如自定义threadpool。如果您定义一个服务器元素,并使用一个jettyXml元素指向一个配置文件,一行像“<配置id =“服务器”class = " org.eclipse.jetty.server。服务器”>“xml配置会覆盖你在pom为服务器配置。

stopPort

可选的。端口监听停止命令。有用的使用结合停止run-forked的目标。

stopKey

可选的。结合stopPort用于阻止jetty。有用的结合使用时停止或run-forked目标。

systemProperties

可选的。允许您配置系统属性执行的插件。有关更多信息,请参见设置系统属性.

systemPropertiesFile

可选的。包含系统属性设置的文件执行的插件。默认情况下,设置在这里 不覆盖任何命令行上已经设置系统属性,由JVM,或者在POM通过systemProperties。读设置系统属性如何强制覆盖。

跳过

默认是假的。如果这是真的,这个插件的执行退出。设置SystemProperty一样 -Djetty.skip在命令行上。这是最有用的,当配置Jetty执行集成测试期间和你想跳过测试

useProvidedScope

默认值是 false。如果这是真的,依赖项 <scope>provided</scope>被放置在 容器类路径。请注意,这不是应用类路径,作为“提供”表明,这些依赖关系通常会将容器提供的。你应该很少需要使用这个。相反,你应该复制依赖提供显式的依赖关系 plugin代替。

excludedGoals

可选的。jetty插件的目标名单,将导致插件打印一条消息并退出。有用的,如果你想防止用户执行,你知道不能处理您的项目的目标。

配置一个Https连接器

为了配置https连接器,您需要使用jetty xml配置文件。这个示例使用文件直接从jetty分布等/目录复制,尽管你当然可以弥补自己的xml文件或文件。我们将使用以下文件:

jetty.xml

设置不同的特点 org.eclipse.jetty.server.Server实例的插件使用。重要的是,它的设置 org.eclipse.jetty.server.HttpConfiguration元素,我们可以参考在随后的xml文件配置连接器。相关部门:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< New id = "httpConfig" class = "org.eclipse.jetty.server.HttpConfiguration" >
   < Set name = "secureScheme" >https</ Set >
   < Set name = "securePort" >< Property name = "jetty.secure.port" default = "8443" /></ Set >
   < Set name = "outputBufferSize" >32768</ Set >
   < Set name = "requestHeaderSize" >8192</ Set >
   < Set name = "responseHeaderSize" >8192</ Set >
   < Set name = "sendServerVersion" >true</ Set >
   < Set name = "sendDateHeader" >false</ Set >
   < Set name = "headerCacheSize" >512</ Set >
 
   <!-- Uncomment to enable handling of X-Forwarded- style headers
   <Call name="addCustomizer">
     <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg>
   </Call>
   -->
</ New >
jetty-ssl.xml

建立ssl将使用https连接器。这是 jetty-ssl.xml文件从jetty-distribution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<? xml version = "1.0" ?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
 
<!-- ============================================================= -->
<!-- Base SSL configuration                                        -->
<!-- This configuration needs to be used together with 1 or more   -->
<!-- of jetty-https.xml or jetty-http2.xml                         -->
<!-- ============================================================= -->
< Configure id = "Server" class = "org.eclipse.jetty.server.Server" >
 
   <!-- =========================================================== -->
   <!-- Add a SSL Connector with no protocol factories              -->
   <!-- =========================================================== -->
   < Call  name = "addConnector" >
     < Arg >
       < New id = "sslConnector" class = "org.eclipse.jetty.server.ServerConnector" >
         < Arg name = "server" >< Ref refid = "Server" /></ Arg >
         < Arg name = "acceptors" type = "int" >< Property name = "jetty.ssl.acceptors" deprecated = "ssl.acceptors" default = "-1" /></ Arg >
         < Arg name = "selectors" type = "int" >< Property name = "jetty.ssl.selectors" deprecated = "ssl.selectors" default = "-1" /></ Arg >
         < Arg name = "factories" >
           < Array type = "org.eclipse.jetty.server.ConnectionFactory" >
             <!-- uncomment to support proxy protocol
             <Item>
               <New class="org.eclipse.jetty.server.ProxyConnectionFactory"/>
             </Item>-->
           </ Array >
         </ Arg >
 
         < Set name = "host" >< Property name = "jetty.ssl.host" deprecated = "jetty.host" /></ Set >
         < Set name = "port" >< Property name = "jetty.ssl.port" deprecated = "ssl.port" default = "8443" /></ Set >
         < Set name = "idleTimeout" >< Property name = "jetty.ssl.idleTimeout" deprecated = "ssl.timeout" default = "30000" /></ Set >
         < Set name = "soLingerTime" >< Property name = "jetty.ssl.soLingerTime" deprecated = "ssl.soLingerTime" default = "-1" /></ Set >
         < Set name = "acceptorPriorityDelta" >< Property name = "jetty.ssl.acceptorPriorityDelta" deprecated = "ssl.acceptorPriorityDelta" default = "0" /></ Set >
         < Set name = "acceptQueueSize" >< Property name = "jetty.ssl.acceptQueueSize" deprecated = "ssl.acceptQueueSize" default = "0" /></ Set >
       </ New >
     </ Arg >
   </ Call >
 
   <!-- =========================================================== -->
   <!-- Create a TLS specific HttpConfiguration based on the        -->
   <!-- common HttpConfiguration defined in jetty.xml               -->
   <!-- Add a SecureRequestCustomizer to extract certificate and    -->
   <!-- session information                                         -->
   <!-- =========================================================== -->
   < New id = "sslHttpConfig" class = "org.eclipse.jetty.server.HttpConfiguration" >
     < Arg >< Ref refid = "httpConfig" /></ Arg >
     < Call name = "addCustomizer" >
       < Arg >
         < New class = "org.eclipse.jetty.server.SecureRequestCustomizer" >
           < Arg type = "boolean" >< Property name = "jetty.ssl.sniHostCheck" default = "true" /></ Arg >
         </ New >
       </ Arg >
     </ Call >
   </ New >
 
</ Configure >
jetty-https.xml

设置使用HttpConfiguration从https连接器 jetty.xml和ssl配置 jetty-ssl.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<? xml version = "1.0" ?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
 
<!-- ============================================================= -->
<!-- Configure a HTTPS connector.                                  -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and jetty-ssl.xml.                                            -->
<!-- ============================================================= -->
< Configure id = "sslConnector" class = "org.eclipse.jetty.server.ServerConnector" >
 
   < Call name = "addIfAbsentConnectionFactory" >
     < Arg >
       < New class = "org.eclipse.jetty.server.SslConnectionFactory" >
         < Arg name = "next" >http/1.1</ Arg >
         < Arg name = "sslContextFactory" >< Ref refid = "sslContextFactory" /></ Arg >
       </ New >
     </ Arg >
   </ Call >
 
   < Call name = "addConnectionFactory" >
     < Arg >
       < New class = "org.eclipse.jetty.server.HttpConnectionFactory" >
         < Arg name = "config" >< Ref refid = "sslHttpConfig" /></ Arg >
       </ New >
     </ Arg >
   </ Call >
   
</ Configure >

现在你需要告诉插件应用上面的文件:

1
2
3
4
5
6
7
8
< plugin >
   < groupId >org.eclipse.jetty</ groupId >
   < artifactId >jetty-maven-plugin</ artifactId >
   < version >9.3.1-SNAPSHOT</ version >
   < configuration >
     < jettyXml >jetty.xml,jetty-ssl.xml,jetty-https.xml</ jettyXml >
   </ configuration >
</ plugin >

谨慎

就像Jetty的安装分布,xml文件的顺序是重要的。

您还可以使用jetty xml文件配置http连接器插件使用。我们使用相同的 jetty-http.xml文件从码头地理分布:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<? xml version = "1.0" ?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
 
<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
< Configure id = "Server" class = "org.eclipse.jetty.server.Server" >
 
   <!-- =========================================================== -->
   <!-- Add a HTTP Connector.                                       -->
   <!-- Configure an o.e.j.server.ServerConnector with a single     -->
   <!-- HttpConnectionFactory instance using the common httpConfig  -->
   <!-- instance defined in jetty.xml                               -->
   <!--                                                             -->
   <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
   <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
   <!-- that may be set here.                                       -->
   <!-- =========================================================== -->
   < Call name = "addConnector" >
     < Arg >
       < New id = "httpConnector" class = "org.eclipse.jetty.server.ServerConnector" >
         < Arg name = "server" >< Ref refid = "Server" /></ Arg >
         < Arg name = "acceptors" type = "int" >< Property name = "jetty.http.acceptors" deprecated = "http.acceptors" default = "-1" /></ Arg >
         < Arg name = "selectors" type = "int" >< Property name = "jetty.http.selectors" deprecated = "http.selectors" default = "-1" /></ Arg >
         < Arg name = "factories" >
           < Array type = "org.eclipse.jetty.server.ConnectionFactory" >
             < Item >
               < New class = "org.eclipse.jetty.server.HttpConnectionFactory" >
                 < Arg name = "config" >< Ref refid = "httpConfig" /></ Arg >
               </ New >
             </ Item >
           </ Array >
         </ Arg >
         < Set name = "host" >< Property name = "jetty.http.host" deprecated = "jetty.host" /></ Set >
         < Set name = "port" >< Property name = "jetty.http.port" deprecated = "jetty.port" default = "8080" /></ Set >
         < Set name = "idleTimeout" >< Property name = "jetty.http.idleTimeout" deprecated = "http.timeout" default = "30000" /></ Set >
         < Set name = "soLingerTime" >< Property name = "jetty.http.soLingerTime" deprecated = "http.soLingerTime" default = "-1" /></ Set >
         < Set name = "acceptorPriorityDelta" >< Property name = "jetty.http.acceptorPriorityDelta" deprecated = "http.acceptorPriorityDelta" default = "0" /></ Set >
         < Set name = "acceptQueueSize" >< Property name = "jetty.http.acceptQueueSize" deprecated = "http.acceptQueueSize" default = "0" /></ Set >
       </ New >
     </ Arg >
   </ Call >
 
</ Configure >

现在我们将其添加到插件的配置列表应用:

1
2
3
4
5
6
7
8
< plugin >
   < groupId >org.eclipse.jetty</ groupId >
   < artifactId >jetty-maven-plugin</ artifactId >
   < version >9.3.1-SNAPSHOT</ version >
   < configuration >
     < jettyXml >jetty.xml,jetty-http.xml,jetty-ssl.xml,jetty-https.xml</ jettyXml >
   </ configuration >
</ plugin >

或者,您可以使用 httpConnector配置元素在pom而不是如上所述。

猜你喜欢

转载自tiro-li.iteye.com/blog/2253945