您的位置:首页 > 其它

Jetty9.3 翻译

2015-09-28 12:16 288 查看

配置 Jetty Maven Plugin

Jetty Maven插件正在被用于快速开发的开发和测试。你能把它加到任何的用Maven的webapp项目中。这款插件能精确的扫描你的项目,如果发生变化就自动发布你的webapp。这使得开发周期更加高效,消除构建和部署的步骤:你用你的IDE来修改该项目,并正在运行的Web容器自动挑选他们,让您对它们进行测试,立竿见影。

Important



You need to use Maven 3 and Java 1.8 for this plugin.



快速开始: 开始与运行

首先,添加jetty-maven-plugin 插件到项目的pomx.xml上进行如下定义:

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


接着,在你的项目的根目录上的pom.xml上 运行,如下:

mvn jetty:run

这样会开启 Jetty 和 启动你的项目 在

This starts Jetty and serves up your project on http://localhost:8080/.

Jetty 继续运行直到你停止他。当他在运行的时候,会定期扫描更改你的项目文件,因此,如果你保存更改并从新编译的类文件,Jetty会从新部署你的web应用,你可以即时测试你刚才所做的更改。

你可以在你运行jetty的终端窗口上按 ‘ctrl-c’ 终止jetty的运行。

支持的命令(Goals)

Jetty Maven插件有许多不同的Maven命令。可以说是最有用的是,我们在其中运行Jetty上的未组装的web应用快速启动部分看到运行的目标。还有其他的目标,帮助您完成不同的任务。例如,您可能需要在Jetty的分叉实例中运行你的web应用,而不是运行Maven的过程中;或者你可能需要更细粒度地控制中,你要部署你的webapp Maven的生命周期阶段。有不同的目标,以完成这些任务,以及其他几个人。

你可以通过以下命令来查看mvn jetty 的命令:

mvn jetty:help

如上,可以为特定的目标进行配置,除了其描述参数的详细列表,如下:

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

Configuring the Jetty Container

这些配置元素设置你的web应用执行中的Jetty的环境。他们是常见的大多数目标:

httpConnector

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

port

要监听的端口,默认是8080

host

连接监听端口的主机,例如localhost。

name

连接器的名称,在特定的连接器配置上下文响应。

idleTimeout

连接处理时间的最大值

soLinger

套接字逗留时间。

你可以在在标准的jetty xml配置文件上安装配置连接器 输入你的配置信息在 jettyXML 的参数 就可以。注意:因为jetty-9.0可能不再直接在pom配置https连接器。xml你需要使用jetty 的xml配置它的信息。

jettyXml

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

scanIntervalSeconds

如果检测到任何自动热部署,在几秒内的停顿中完成扫描webapp检查更改。在默认情况下这是0,表示禁止热部署扫描,使其数大于0就可以启用它。

reload

默认值是“automatic”,用于有一个非0的scanIntervalSeconds参数,引起自动热从新部署,当检测到更改的时候。设置为“manual”手动而不是扫描触发,你可以在控制台上输入 运行的插件。这是有用的,当你在做一系列的变化时,你想忽略,直到你完成了。你应该用重载参数。

dumpOnStart

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

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 Lo特别是,如果你使用jettyXml元素,那么你一般不想定义这个元素,正如你可能使用jettyXml文件来配置一个服务器与一种特殊的构造函数参数,如自定义threadpool。如果您定义一个服务器元素,并使用一个jettyXml元素指向一个配置文件,一行像 “” xml配置会覆盖你在pom为服务器配置。gs for more information.

server

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

stopPort

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

stopKey

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

systemProperties

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

systemPropertiesFile

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

skip

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

useProvidedScope

默认值是false的。如果这是true的,提供的依赖与<范围> < / >范围被放置到容器类路径中。请注意,这不是应用类路径,作为“提供”表明,这些依赖关系通常会将容器提供的。你应该很少需要使用这个。相反,你应该复制提供插件依赖关系的显式的依赖关系。

excludedGoals

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

Configuring a Https Connector

为了配置一个https连接,你需要一个jetty xml配置文件。这个文件能从jetty的根路径 etc/directory,下找到,但是你也可以建立一个你自己的xml配置文件,我们会用下面的文件:

jetty.xml

设置各种org.eclipse.jetty.server的特性供插件使用服务器实例。重要的是,它设置org.eclipse.jetty.server。HttpConfiguration元素,我们可以参考在随后配置连接器的xml文件中的到体现:

[code]<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-distribution中的jetty-ssl.xml文件

[code]<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-https.xml

用jetty.xml中的HttpConfiguration 和 jetty-ssl 的ssl 配置 https 连接器 ;

[code]<?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>


现在,你需要让插件去知道使用上面的配置文件:

[code]<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文件。

[code]<?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>


现在我们把配置文件配置到插件中,让我们去使用:

[code]<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>


另外,你可以使用httpsConnector 配置元素 把配置插入pom中,以代替上面的配置;

Configuring Your WebApp

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

webApp

代表一个org.eclipse.jetty.webapp.WebAppContext扩展类。您可以使用任何物体的setter方法来配置您的应用。这里有一些最有用的:

contextPath

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

descriptor

webapp的web.xml路径。

defaultsDescriptor

是一个webdefault.xml文件路径,这是一个在web.xml加载前执行的文件,如果你没有设置,那么jetty会默认加载一个jetty-webapp.jar。

overrideDescriptor

这是配置web.xml的路径文件,他会被部署在读取你的web.xml之后。你能用它取代或增加你的web.xml配置。

tempDirectory

这是一个文件夹路径,Jetty能使用或者拷贝 jars 和jsp 编译文件,在你的webapp在运行的时候。他的默认路径是${project.build.outputDirectory}/tmp.

baseResource

这个路径是Jetty的静态资源文件;默认是 src/main/webapp。

resourceBases

用于代替 “basseResources” 如果你有多个包含服务静态文件内容的文件夹。这是可以设置为 文件名的 数组属性

baseAppFirst

默认是 “true” 。控制是否覆盖竞争之前或之后添加原基础资源的应用。有关更多信息,请参见部分覆盖竞争。

contextXml

上下文路径的xml文件应用到你的应用后。

jetty:run

run命令 运行一个没有被打成war包的webapp, 此外,Jetty 发布在他的resources中 webapp,他在Maven默认的项目路径中被看作是webapp的一部分,尽管你可以在插件配置中修改他的路径。例如,他默认的路径是这样的:


resources in ${project.basedir}/src/main/webapp

classes in ${project.build.outputDirectory}

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



插件会自动确保 classes 在被发布前,被建立和更新。如果你修改了你的源码以及你的IDE在后台自动编译它,插件就会机会更新class编译文件。

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

这里是一个小的例子,这取决于扫描变化每十秒钟,并设置webapp /测试上下文路径:

[code]<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>


Configuration

除了应用元素webApp是最常见的目标外,jetty:运行目标支持:

- classesDirectory

是webapp编译文件的路径。你应该很少需要设置这个参数,你应该设置 buildoutputDirectory 在你的pom.xml中。

testClassesDirectory

是你webapp测试编译文件的路径.默认是 ${project.build.testOutputDirectory}.

useTestScope

应用测试范围,如果是设置为true, classess 编译路径为 testClassesDirectory 和依赖的Maven 范围为 “test” 会在classpath 中设置为首选。默认是false。

webAppSourceDirectory

默认设置为 ${project.basedir}/src/main/webapp。如果你的静态文件是在不同与默认路径的目录,设置相应的参数。

jettyEnvXml

可选,路径为jetty-env.xml的文件,它允许你用JNDI绑定满足 env-entry,resource-env-ref,以及 resources-ref ,在web.xml中联系。他的范围是 本机的webapp 而不能与其他与你一起同时发布的webapps 共享。

scanTargets

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

scanTargetPatterns

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

scanClassesPattern

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

scanTestClassesPattern

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

这里是一个例子:

[code]<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>


如果,无论什么原因,你不能运行一个未装配的webapp,run-war 命令和run-exploded 能为未装配的webapp工作;

jetty:run-war

这个命令首先会打包你的webapp成为一个WAR包,以及发布它到jetty上。如果你设置0为scanIterval的值,Jetty会查看你的pom.xml 和 WAR文件;如果如果他们都有改变,它会war包;

这里看看如何配置它:

[code]<project>
... 
  <plugins>
...
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.3.1-SNAPSHOT</version>
      <configuration>
        <war>${project.basedir}/target/mycustom.war</war>
      </configuration>
    </plugin>
  </plugins>
</project>


jetty:run-exploded

run-exploded 命令 首先装配你的webapp WAR文件,接着发布它到jetty.如果你设置scanInterval为0,jetty会查看你的pom.xml, WEB-INF/lib, WEB-INF/ classes and WEB-INF/web.xml 的那些改变了,在需要的时候从新发布。

war

发布的WAR包的位置,这个默认为project.build.director/{project.build.director}/{project.build.finalName},但是,你也可以重写它的默认参数。

这里是这样重写它的参数的:

Configuration

[code]<project> 
... 
  <plugins>
... 
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>maven-jetty-plugin</artifactId>
      <version>9.3.1-SNAPSHOT</version>
      <configuration>
        <war>${project.basedir}/target/myfunkywebapp</war>
      </configuration>
    </plugin>
  </plugins>
</project>


jetty:deploy-war

jetty:deploy-war 命令基本上类似与jetty:run-war命令,

但是并没有在装配当前模块的WAR包,你可以指定任何你要运行的WAR包的位置。并不像run-war,这个插件执行的阶段不是绑定到“包”阶段——你可以将其绑定到任何阶段使用它。

Configuration

war

发布的WAR包的位置,这个默认为project.build.director/{project.build.director}/{project.build.finalName},但是,你也可以重写它的默认参数。

daemon

参数如果为true,插件会开启插件,同时也让它继续build工程。这将非常有用,如果你想开启Jetty来绑定到某个特殊的阶段和停止它在另一个阶段(如build和test阶段)。另外,如果你能设置这个参数为false,这会引起jetty阻塞,你需要使用Cntrl+C 来停止它。

这里是它的配置实例:

[code]<project>
... 
  <plugins>
... 
  <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.3.1-SNAPSHOT</version>
    <configuration>
      <war>/opt/special/some-app.war</war>
      <stopKey>alpha</stopKey>
      <stopPort>9099</stopPort>
    </configuration>
    <executions>
      <execution>
        <id>start-jetty</id>
        <phase>test-compile</phase>
        <goals>
          <goal>deploy-war</goal>
        </goals>
      </execution>
      <execution>
        <id>stop-jetty</id>
        <phase>test</phase>
        <goals>
          <goal>stop</goal>
        </goals>
      </execution>
     </executions>
    </plugin>
  </plugins>
</project>


jetty:run-forked

这个命令允许你在一个新的JVM进程中开启你的webapp,可选参数传递给新的JVM。这个命令支持相同的配置参数,例如:jetty:run 命令与几个临时变量来配置新的进程。

Configuration

下面是一些可以利用的的配置参数—此外,也可以用在jetty:run 命令上,如下:

jvmArgs

可选的。一个字符串代表任意参数传递给新JVM进程的。

waitForChild

默认为true,这会引起父进程去等待分支JVM进程推出.例如,你能用cntrl+C 终止两个过程。如果你设置它为false将会更有用的,在这种情况下,父进程终止而离开子进程运行。你可以使用jetty-stop命令阻止子进程的运行。

maxStartupLines

默认配置是50,这是最大的行数的父进程对子进程进行读取,来接收子进程已经开始的信息。如果子进程在标准输出流上产生过量的输出,你可能需要增加这个数字50.

下面是一些额外的配置参数,它们是不能被利用在这个命令上的:

scanIntervalSeconds

不支持,分进程的jetty不会监控和重新部署应用。

reload

不支持的。分进程的jetty不会重新部署应用。

httpConnector

不支持的。定义自定义连接器使用jetty的xml文件。

loginServices

不支持的。定义LoginServices 使用jetty的xml文件 或 context的xml文件替代。

requestLog

不支持的。定义一个RequestLog设置中,使用jetty的xml文件或context的xml文件替代。

systemProperties

不支持的。通过使用jvmArgs 参数去传递 系统属性参数 给分支子进程。

把未装配的web应用程序部署到Jetty的新JVM中运行:

mvn jetty:run-forked


Jetty继续执行,直到你遇到:

* 在window终端上按Ctrl-c 去停止插件,同时也会停止JVM的子进程。(only if you started with waitForChild=true)

* 使用jetty:stop命令,停止JVM子进程,同时也会停止插件。

######Note

If you want to set a custom port for the jetty connector you need to specify it in a jetty xml file rather than setting the connector and port tags. You can specify the location of the jetty.xml using the jettyXml parameter.

jetty:start

这个命令是被执行绑定到pom.xml中使用的。它与jetty:run命令是非常相似的。然而,它并不是第一个执行构建的,直到”test-compile” 阶段的全部编译classes和webapp的文件得到确保生成后才执行。这是最有用的,当你想控制启动和停止的Jetty pom.xml通过执行绑定。

下面给出一个例子,你可以配置你的插件去开启你的webapp,在单元测试阶段,并在单元测试完成后停止它。为了完成这些,你需要为Jetty配置一组 execution 来告诉Jetty如何去做。你可以使用
pre-integration-test
post-integration-test
在Maven构建阶段去告诉触发器 jetty的执行和终止。

[code]<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.1-SNAPSHOT</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <stopKey>foo</stopKey>
    <stopPort>9999</stopPort>
  </configuration>
  <executions>
    <execution>
      <id>start-jetty</id>
      <phase>pre-integration-test</phase>
      <goals>
        <goal>start</goal>
      </goals>
      <configuration>
        <scanIntervalSeconds>0</scanIntervalSeconds>
      </configuration>
    </execution>
    <execution>
      <id>stop-jetty</id>
      <phase>post-integration-test</phase>
       <goals>
         <goal>stop</goal>
       </goals>
     </execution>
  </executions>
</plugin>


jetty:stop

stop命令停止一个正在运行的jetty实例。使用它,你需要用一个特殊的端口号(port)和关键字(key)配置插件。相同的port和key也会被其它的命令所使用,在jetty开启的时候。

stopPort

要监听的端口号jetty收到停止命令使它关闭。

stopKey

一个字符串值发送到stopPort验证停止命令。

stopWait

插件的最大时间间隔,以秒为单位将等待确认jetty已经停止。如果虚假或不指定,插件不等待确认但发出停止命令后退出。

Here’s a configuration example:

[code]<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.1-SNAPSHOT</version>
  <configuration>
    <stopPort>9966</stopPort>
    <stopKey>foo</stopKey>
    <stopWait>10</stopWait>
  </configuration>
</plugin>


Then, while Jetty is running (in another window), type:

[code]mvn jetty:stop


在你运行jetty时 ,stopPort 端口必须没有被你机器上的其他程序所占有。如果你不能

确定,你可以 在 “Started SelectedChannelConnector …”信息中设置 “Address already in use” 的错误提示。

jetty:effective-web-xml

This goal calculates a synthetic web.xml (the “effective web.xml”) according to the rules of the Servlet Specification taking into account all sources of discoverable configuration of web components in your application: descriptors (webdefault.xml, web.xml, web-fragment.xmls, web-override.xml) and discovered annotations (@WebServlet, @WebFilter, @WebListener). The effective web.xml from these combined sources is generated and displayed as maven log output. Other useful information about your webapp that is produced as part of the analysis is also stored as context parameters in the effective-web.xml. The effective-web.xml can be used in conjunction with the Quickstart feature to quickly start your webapp (note that Quickstart is not appropriate for the mvn jetty goals).

下列配置参数允许您保存文件:

deleteOnExit

默认为true。如果设置为false,在构建的目标目录上,有效的web.

xml被生成的文件,叫effective-web.xml 。

effectiveWebXml

文件的完整路径名,需要有效的web xml生成。

注意,没有servlet的程序化的声明,过滤器和听众会考虑在内。

Using Overlaid wars

If your webapp depends on other war files, thejetty:run and jetty:run-forked goals are able to merge resources from all of them. It can do so based on the settings of the maven-war-plugin, or if your project does not use the maven-war-plugin to handle the overlays, it can fall back to a simple algorithm to determine the ordering of resources.

With maven-war-plugin

The maven-war-plugin has a rich set of capabilities for merging resources. The jetty:run and jetty:run-forked goals are able to interpret most of them and apply them during execution of your unassembled webapp. This is probably best seen by looking at a concrete example.

Suppose your webapp depends on the following wars:

[code]<dependency>
  <groupId>com.acme</groupId>
  <artifactId>X</artifactId>
  <type>war</type>
</dependency>
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>Y</artifactId>
  <type>war</type>
</dependency>


Containing:

[code]WebAppX: 

 /foo.jsp 
 /bar.jsp 
 /WEB-INF/web.xml 

WebAppY: 

 /bar.jsp
 /baz.jsp
 /WEB-INF/web.xml
 /WEB-INF/special.xml


They are configured for the maven-war-plugin:

[code]<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>9.3.1-SNAPSHOT</version>
  <configuration>
    <overlays>
      <overlay>
        <groupId>com.acme</groupId>
        <artifactId>X</artifactId>
        <excludes>
          <exclude>bar.jsp</exclude>
        </excludes>
      </overlay>
      <overlay>
        <groupId>com.acme</groupId>
        <artifactId>Y</artifactId>
        <excludes>
          <exclude>baz.jsp</exclude>
        </excludes>
      </overlay>
      <overlay>
      </overlay>
    </overlays>
  </configuration>
</plugin>


Then executing jetty:run would yield the following ordering of resources: com.acme.X.war : com.acme.Y.war: ${project.basedir}/src/main/webapp. Note that the current project’s resources are placed last in the ordering due to the empty element in the maven-war-plugin. You can either use that, or specify the false parameter to the jetty-maven-plugin.

Moreover, due to the exclusions specified above, a request for the resource bar.jsp would only be satisfied from com.acme.Y.war. Similarly as baz.jsp is excluded, a request for it would result in a 404 error.

Without maven-war-plugin

The algorithm is fairly simple, is based on the ordering of declaration of the dependent wars, and does not support exclusions. The configuration parameter (see the section on Configuring Your Webapp for more information) can be used to control whether your webapp’s resources are placed first or last on the resource path at runtime.

For example, suppose our webapp depends on these two wars:

[code]<dependency>
  <groupId>com.acme</groupId>
  <artifactId>X</artifactId>
  <type>war</type>
</dependency>
<dependency>
  <groupId>com.acme</groupId>
  <artifactId>Y</artifactId>
  <type>war</type>
</dependency>


Suppose the webapps contain:

[code]WebAppX: 
 /foo.jsp 
 /bar.jsp 
 /WEB-INF/web.xml 

WebAppY: 
 /bar.jsp 
 /baz.jsp
 /WEB-INF/web.xml 
 /WEB-INF/special.xml


Then our webapp has available these additional resources:

[code]/foo.jsp (X) 
/bar.jsp (X) 
/baz.jsp (Y) 
/WEB-INF/web.xml (X)
/WEB-INF/special.xml (Y)


Configuring Security Settings

You can configure LoginServices in the plugin. Here’s an example of setting up the HashLoginService for a webapp:

[code]<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>
    <loginServices>
      <loginService implementation="org.eclipse.jetty.security.HashLoginService">
        <name>Test Realm</name>
        <config>${project.basedir}/src/etc/realm.properties</config>
      </loginService>
    </loginServices>
  </configuration>
</plugin>


Using Multiple Webapp Root Directories

If you have external resources that you want to incorporate in the execution of a webapp, but which are not assembled into WARs, you can’t use the overlaid wars method described above, but you can tell Jetty the directories in which these external resources are located. At runtime, when Jetty receives a request for a resource, it searches all the locations to retrieve the resource. It’s a lot like the overlaid war situation, but without the war. Here’s a configuration example:

[code]<configuration>
  <webApp>
    <contextPath>/${build.finalName}</contextPath>
    <baseResource implementation="org.eclipse.jetty.util.resource.ResourceCollection">
      <resourcesAsCSV>src/main/webapp,/home/johndoe/path/to/my/other/source,/yet/another/folder</resourcesAsCSV>
    </baseResource>
  </webApp>
</configuration>


Running More than One Webapp

You can use either a jetty.xml file to configure extra (pre-compiled) webapps that you want to deploy, or you can use the configuration element to do so. If you want to deploy webapp A, and webapps B and C in the same Jetty instance:

Putting the configuration in webapp A’s pom.xml:

[code]<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>
    <contextHandlers>
      <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
        <war>${project.basedir}../../B.war</war>
        <contextPath>/B</contextPath>
      </contextHandler>
      <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
        <war>${project.basedir}../../C.war</war>
        <contextPath>/C</contextPath>
      </contextHandler>
    </contextHandlers>
  </configuration>
</plugin>


Important
If the ContextHandler you are deploying is a webapp, it is essential that you use an org.eclipse.jetty.maven.plugin.JettyWebAppContext instance rather than a standard org.eclipse.jetty.webapp.WebAppContext instance. Only the former will allow the webapp to function correctly in the maven environment.

Alternatively, add a jetty.xml file to webapp A. Copy the jetty.xml file from the jetty distribution, and then add WebAppContexts for the other 2 webapps:

[code]<Ref refid="Contexts">
  <Call name="addHandler">
    <Arg>
      <New class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
        <Set name="contextPath">/B</Set>
        <Set name="war">../../B.war</Set>
      </New>
    </Arg>
  </Call>
  <Call> 
    <Arg>
      <New class="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
        <Set name="contextPath">/C</Set>
        <Set name="war">../../C.war</Set>
      </New>
    </Arg>
  </Call>
</Ref>


Then configure the location of this jetty.xml file into webapp A’s jetty plugin:

[code]<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>
    <jettyXml>src/main/etc/jetty.xml</jettyXml>
  </configuration>
</plugin>


For either of these solutions, the other webapps must already have been built, and they are not automatically monitored for changes. You can refer either to the packed WAR file of the pre-built webapps or to their expanded equivalents.

Setting System Properties

You can specify property name/value pairs that Jetty sets as System properties for the execution of the plugin. This feature is useful to tidy up the command line and save a lot of typing.

However, sometimes it is not possible to use this feature to set System properties - sometimes the software component using the System property is already initialized by the time that maven runs (in which case you will need to provide the System property on the command line), or by the time that jetty runs. In the latter case, you can use the maven properties plugin to define the system properties instead. Here’s an example that configures the logback logging system as the jetty logger:

[code]<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>properties-maven-plugin</artifactId>
  <version>1.0-alpha-2</version>
  <executions>
    <execution>
      <goals>
        <goal>set-system-properties</goal>
      </goals>
      <configuration>
        <properties>
          <property>
            <name>logback.configurationFile</name>
            <value>${project.baseUri}/resources/logback.xml</value>
          </property>
        </properties>
      </configuration>
    </execution>
  </executions>
</plugin>


Note that if a System property is already set (for example, from the command line or by the JVM itself), then by default these configured properties DO NOT override them (see below for use of the parameter).

Specifying System Properties in the POM

Here’s an example of how to specify System properties in the POM:

[code]<plugin> 
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration> 
    <systemProperties>
      <systemProperty>
        <name>fooprop</name>
        <value>222</value>
      </systemProperty>
    </systemProperties>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration> 
</plugin>


To change the default behaviour so that these system properties override those on the command line, use the parameter:

[code]<plugin> 
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration>
    <systemProperties>
      <force>true</force>
      <systemProperty>
       <name>fooprop</name>
       <value>222</value>
     </systemProperty>
    </systemProperties>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>


Specifying System Properties in a File

You can also specify your System properties in a file. System properties you specify in this way DO NOT override System properties that set on the command line, by the JVM, or directly in the POM via systemProperties.

Suppose we have a file called mysys.props which contains the following:

[code]fooprop=222


This can be configured on the plugin like so:

[code]<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <configuration>
    <systemPropertiesFile>${project.basedir}/mysys.props</systemPropertiesFile>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>


You can instead specify the file by setting the System property (!) jetty.systemPropertiesFile on the command line.

参考:http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html#configuring-your-webapp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: