配置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而不是如上所述。

配置您的应用

这些配置参数应用到你的应用。他们是常见的几乎所有的目标。

webApp

这是一个实例org.eclipse.jetty.maven.plugin.JettyWebAppContext,这是一个扩展类 org.eclipse.jetty.webapp.WebAppContext。您可以使用任何物体的setter方法来配置您的应用。这里有一些最有用的:

contextPath

你的应用的上下文路径。默认情况下,这个设置 /.

描述符

的路径 web.xml为您的应用文件。

defaultsDescriptor

的路径 webdefault.xml之前文件将被应用到你的应用 web.xml。如果你不供应,烤到Jetty使用一个默认的文件 jetty-webapp.jar.

overrideDescriptor

的路径 web.xmlJetty应用在阅读你的文件 web.xml。您可以使用它来替换或添加配置。

tempDirectory

的路径dir码头可以用来扩大或复制jar和jsp编译当你的应用正在运行。默认值是 ${project.build.outputDirectory}/tmp.

baseResource

Jetty是静态资��的路径。默认为 src/main/webapp.

resourceBases

使用,而不是 baseResource如果您有多个dirs你想提供静态内容。这是一个数组dir的名字。

baseAppFirst

默认为“true”。控制是否覆盖战争之前或之后添加原基础资源的应用。上看到的部分覆盖的战争为更多的信息。

containerIncludeJarPattern

默认为“。* / javax.servlet -[^ /]* \ . jar |美元。* / servlet api -[^ /]* \ . jar |美元。* javax.servlet.jsp.jstl -[^ /]* \ . jar |。* taglibs-standard-impl -。* \ . jar”。这个模式应用于jar文件的名称在容器的类路径(ie插件的类路径中,而不是应用),应扫描片段,这些注释等等。这类似于上下文属性org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern这是记录在这里。您可以定义额外的jar模式应包括在扫描。

webInfIncludeJarPattern

默认为匹配 所有依赖性jar的应用(即相当于web - inf / lib)。你可以使这种模式更多的限制性使用setter只匹配某些jar。这类似于上下文属性org.eclipse.jetty.server.webapp.WebInfIncludeJarPattern这是记录在这里.

contextXml

通往一个上下文xml文件后应用到你的应用 webApp元素。

jetty:跑

 run目标运行在网络应用,不需要内置一场战争。相反,Jetty部署应用其来源。看起来的组成部分应用在Maven项目默认位置,尽管你可以覆盖这些插件配置。例如,默认情况下它查找:

  • 资源 ${project.basedir}/src/main/webapp

  •  ${project.build.outputDirectory}

  • web.xml ${project.basedir}/src/main/webapp/WEB-INF/

插件之前自动重建,保证了类最新的部署。如果你改变一个类的来源和IDE在后台自动编译它,插件拿起改变类。

你不需要组装应用成战争,节省时间在开发周期。一旦调用,您可以配置连续运行的插件,扫描项目的变化,必要时自动执行热部署。你做任何更改会立即反映在码头的运行实例,让你迅速从编码到测试,而不是经历的循环:代码,编译、重新组装、部署、测试。

这是一个小例子,打开扫描变化每十秒钟,并设置应用上下文路径 /test:

1
2
3
4
5
6
7
8
9
10
11
12
< plugin >
   < groupId >org.eclipse.jetty</ groupId >
   < artifactId >jetty-maven-plugin</ artifactId >
   < version >9.3.1-SNAPSHOT</ version >
   < configuration >
     < scanIntervalSeconds >10</ scanIntervalSeconds >
     < webApp >
       < contextPath >/test</ contextPath >
     </ webApp >
   </ configuration >
</ plugin >
      
配置

除了 webApp共同特征是大多数目标, jetty:run目标支持:

classesDirectory

webapp的已编译类的位置。你应该很少需要设置该参数。相反,你应该设定 build outputDirectory在你的 pom.xml.

testClassesDirectory

webapp编译测试类的位置。在默认情况下这是 ${project.build.testOutputDirectory}.

useTestScope

如果这是真的,这些类 testClassesDirectory和依赖关系的范围“测试”是第一个放在类路径中。在默认情况下这是错误的。

webAppSourceDirectory

默认情况下,这个设置 ${project.basedir}/src/main/webapp。如果你的静态资源在不同的位置,设置相应的参数。

jettyEnvXml

可选的。的位置 jetty-env.xml文件,你可以让JNDI绑定满足 env-entry, resource-env-ref, resource-ref联系的 web.xml范围只到webapp,不能共享与其他webapps,你可能会部署在同一时间(例如,通过使用一个 jettyConfig文件)。

scanTargets

可选的。定期扫描的文件和目录列表除了这些插件自动扫描。

scanTargetPatterns

可选的。如果你有一长串的额外文件需要扫描,更方便使用模式匹配表达式来指定它们,而不是列举他们 scanTargetsList scanTargetPatterns组成的,每个目录,包括和/或不含参数指定的文件匹配模式。

scanClassesPattern

因为9.3.0。可选的。包括和排除模式,可以应用于classesDirectory扫描的目的, 不影响到类路径中。如果一个文件或目录排除由模式然后改变该文件(或子树的目录)被忽略,不会导致应用重新部署。模式指定为一个相对路径使用glob-like语法中描述javadoc为FileSystem.getPathMatcher.

scanTestClassesPattern

因为9.3.0。可选的。包括和排除模式,可以应用于testClassesDirectory扫描的目的, 不影响到类路径中。如果一个文件或目录排除由模式然后改变该文件(或子树的目录)被忽略,不会导致应用重新部署。模式指定为一个相对路径使用glob-like语法中描述javadoc为FileSystem.getPathMatcher.

这里有一个例子:

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
< project >
...
   < plugins >
...
     < plugin >
       < groupId >org.eclipse.jetty</ groupId >
       < artifactId >jetty-maven-plugin</ artifactId >
       < version >9.3.1-SNAPSHOT</ version >
       < configuration >
         < webAppSourceDirectory >${project.basedir}/src/staticfiles</ webAppSourceDirectory >
         < webApp >
           < contextPath >/</ contextPath >
           < descriptor >${project.basedir}/src/over/here/web.xml</ descriptor >
           < jettyEnvXml >${project.basedir}/src/over/here/jetty-env.xml</ jettyEnvXml >
         </ webApp >
         < classesDirectory >${project.basedir}/somewhere/else</ classesDirectory >
         < scanClassesPattern >
           < excludes >
              < exclude >**/Foo.class</ exclude >
           </ excludes >
         </ scanClassesPattern >
         < scanTargets >
           < scanTarget >src/mydir</ scanTarget >
           < scanTarget >src/myfile.txt</ scanTarget >
         </ scanTargets >
         < scanTargetPatterns >
           < scanTargetPattern >
             < directory >src/other-resources</ directory >
             < includes >
               < include >**/*.xml</ include >
               < include >**/*.properties</ include >
             </ includes >
             < excludes >
               < exclude >**/myspecial.xml</ exclude >
               < exclude >**/myspecial.properties</ exclude >
             </ excludes >
           </ scanTargetPattern >
         </ scanTargetPatterns >
       </ configuration >
     </ plugin >
   </ plugins >
</ project >
        

无论什么原因,如果你不能运行在一个未装配的应用,目标 run-war run-exploded工作未装配的webapp到一个错误或丢失的东西?这个文档 Github !(生成:2015 - 11 - 02 - t01:01:25 + 0)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值