Jetty使用教程(四:23)—Jetty開發指南

二十3、Maven和Jetty

  這一章節將說明如何經過Maven管理Jetty和使用Jetty的Maven插件。html

23.1 使用Maven

  Apache Maven是一個款軟件項目管理工具。基於項目對象模型(POM)的概念,Maven能夠管理一個項目的構建生成報告和文檔。這是一個理想的工具用來構建一個web應用程序,這樣的項目也可使用jetty-maven-plugin來輕鬆運行並能夠節省開發時間,你也可使用Maven來構建、測試、運行一個嵌入式Jetty項目。java

  首先咱們要看一個很是簡單的Jetty嵌入式的HelloWorld程序,而後再看如何用jetty-maven-plugin來加速開發一個web應用。web

23.1.1 經過Maven使用嵌入式Jetty

  爲了理解構建和運行Jetty的基本操做,首先請回顧如下內容(點擊我):算法

  • 嵌入式Jetty
  • Jetty的HelloWorld例子

  Maven多使用約定而不是配置,因此最好的作法是使用Maven推薦的命令來構建項目。你可使用archetypes 模板來快速生成一個Maven項目,可是對於這個簡單的入門例子,咱們將手動進行配置:apache

> mkdir JettyMavenHelloWorld
> cd JettyMavenHelloWorld
> mkdir -p src/main/java/org/example

23.1.1.1 建立一個HelloWorld類

  可使用文本編輯器建立文件src/main/java/org/example/HelloWorld.java,有以下內容:api

package org.example;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler
{
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());

        server.start();
        server.join();
    }
}

23.1.1.2 建立POM描述文件

  pom.xml用來描述項目名稱和依賴關係。使用文本編輯器建立pom.xml,包含以下內容:數組

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>Jetty HelloWorld</name>

  <properties>
      <!-- 具體版本信息可在此查看 http://central.maven.org/maven2/org/eclipse/jetty/jetty-maven-plugin/
        -->
      <jettyVersion>9.0.2.v20130417</jettyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${jettyVersion}</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution><goals><goal>java</goal></goals></execution>
        </executions>
        <configuration>
          <mainClass>org.example.HelloWorld</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

23.1.1.3 構建和運行嵌入式的HelloWorld

  你可使用以下命令來編譯並執行HelloWorld:瀏覽器

> mvn clean compile exec:java

  你能夠在你的瀏覽器中輸入http://localhost:8080來查看歡迎頁。你能夠觀察到使用mvn dependency:tree命令後Maven到底作了什麼,主要是解決依賴和下載文件:安全

> mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Jetty HelloWorld
[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] org.example:hello-world:jar:0.1-SNAPSHOT
[INFO] \- org.eclipse.jetty:jetty-server:jar:9.0.0:compile
[INFO]    +- org.eclipse.jetty:javax.servlet:jar:3.0.0.v201112011016:compile
[INFO]    +- org.eclipse.jetty:jetty-continuation:jar:9.0.0:compile
[INFO]    \- org.eclipse.jetty:jetty-http:jar:9.0.0:compile
[INFO]       \- org.eclipse.jetty:jetty-io:jar:9.0.0:compile
[INFO]          \- org.eclipse.jetty:jetty-util:jar:9.0.0:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Jan 24 16:19:08 EST 2013
[INFO] Final Memory: 11M/68M
[INFO] ------------------------------------------------------------------------

23.1.2 經過Jetty和Maven來部署一個標準的web應用

  上面一節演示瞭如何經過Maven來建立嵌入式Jetty應用。如今咱們要研究如何經過Maven和Jetty來開發一個標準的web應用。首先建立Maven項目結構(若是你喜歡你也可使用maven的webapp模板來構建)。session

> mkdir JettyMavenHelloWarApp
> cd JettyMavenHelloWebApp
> mkdir -p src/main/java/org/example
> mkdir -p src/main/webapp/WEB-INF

23.1.2.1 建立一個Servlet

  使用編輯器建立一個src/main/java/org/example/HelloServlet.java文件,內容以下:

package org.example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet
{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>Hello Servlet</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

  你須要將這個servlet聲明在部署描述中,因此建立src/main/webapp/WEB-INF/web.xml文件,並增長以下內容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
   metadata-complete="false"
   version="3.1">

  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>org.example.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>

</web-app>

23.1.2.2 建立POM描述文件

  pom.xml用來描述項目名稱和依賴關係。使用文本編輯器建立pom.xml,包含以下內容,特別注意 jetty-maven-plugin的聲明:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>hello-world</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Jetty HelloWorld WebApp</name>

  <properties>
      <jettyVersion>9.3.11.v20160721</jettyVersion>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>${jettyVersion}</version>
      </plugin>
    </plugins>
  </build>

</project>

23.1.2.3 構建運行web應用

  如今你能夠同時構建和運行一個web應用,不須要把項目打成war包,而是使用jetty-maven-plugin插件命令:

> mvn jetty:run

  你能夠在http://localhost:8080/hello看到靜態和動態的內容。

23.1.2.4 生成一個War文件

  你可使用以下命令來說一個web項目打包成war包:

> mvn package

  打包後的war包能夠部署到標準的servlet容器裏,包括Jetty。

23.2 配置Jetty的Maven插件

  Jetty Maven插件很是適合快速開發和測試。根據一般的Maven約定你能夠把它添加到任何web項目中。這個插件能夠週期性的掃描你的項目,如有改變會自動部署。這樣經過消除構建的時間,可讓開發更有效率,部署的步驟是:你使用你的開發工具對項目有所改變,那麼運行中的web容器會自動更換它們,容許你能夠當即測試代碼。

 重要提示:

  爲了使用這個插件應該使用Maven 3.3+ 和 Java1.8版本。

  雖然Jetty Maven的插件對開發很是有用,可是咱們依然不建議用戶在生產環境上使用。爲了讓插件能正常工做,時用了不少maven內部的API,可是maven自己並非一個生產部署工具。因此咱們建議使用傳統方式部署應用或者使用嵌入式Jetty。

23.2.1 快速開始:起步運行

  首先新增jetty-maven-plugin 到你的 pom.xml中:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
</plugin>

  而後,在pom.xml的目錄下執行以下命令:

mvn jetty:run

  能夠經過訪問http://localhost:8080/來查看啓動的Jetty服務。

  Jetty會持續運行知道你中止它,當它運行時,它會按期掃描你項目下的文件,因此若是你對保存修改的文件並從新編譯成class文件,那麼Jetty會從新部署你的應用,你就能夠當即對你剛纔作的改變進行測試。

  你能夠在控制檯使用Ctrl+C,來強行終止Jetty運行。

 注意:

  運行中的Jetty程序的classpath和它的部署webapp都是由Maven進行管理的,因此有可能不像你想的那麼精確。例如:web應用依賴的jar包有可能引用的是本地文件,而不是WEB-INF/lib目錄下。

23.2.2 實現目標

  Jetty Maven插件有不少不一樣於Maven的功能,能夠說最有用的命令run能夠用在一個原始的web應用上。這還有另外一個方法一樣能夠幫助你實現目標,例如:你有可能須要將你的應用運行在Jetty下,而不是maven下;或者你想經過Jetty來部署應用。有不一樣的方法來實現你想要的。

  使用以下命令能夠列出Jetty Maven 插件全部的功能

mvn jetty:help

  若是想要查看具體的參數配置,能夠添加以下參數:

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

23.2.3 配置Jetty容器

  下面這些Jetty環境屬性的設置在你的web應用中執行,最經常使用的配置以下: 

httpConnector

  可選擇的配置,若是沒有設置,Jetty將建立ServerConnector實例來監聽8080端口。你能夠在命令行上使用系統屬性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中了;你可使用Jetty的xml進行配置。

jettyXml

  可選擇的配置,一般,能夠把以逗號分割的Jetty xml配置文件的地址字符串增長到任何插件的配置參數中。若是你有另外一個web應用、處理器、特別是鏈接器,你就可使用它,可是若你有另一個Jetty對象,則不能經過插件獲得配置信息。

scanIntervalSeconds

  自動掃描文件改變並進行熱部署的時間間隔,單位爲秒。默認值爲0,這表明着禁用掃描並熱部署,只有一個大於0的配置可使它生效。

reload

  從新加載選項,默認值是"automatic"(自動),通常用來和配置不爲0的scanIntervalSeconds一同使用。默認配置下當發現有文件改變會自動進行熱部署。若是設置爲"manual" (手動),這樣設置的話,部署將會經過插件被手動觸發,這在當你頻繁改動文件時比較有用,這樣會忽略你的改動,直到你作完全部改變。

dumpOnStart

  可選擇的配置,默認爲false,若是設置爲true。那麼Jetty會在啓動時打印出server的結構。

loginServices

  可選擇的配置。是一系列org.eclipse.jetty.security.LoginService的實現類。注意,沒有指定默認的域,若是你須要在web.xml中配置域,那麼就能夠配置一個統一的域。固然也能夠在Jetty的xml裏面進行配置。並把配置文件的地址增長到jettyXml中。

requestLog

  可選擇的配置。一個實現了org.eclipse.jetty.server.RequestLog接口的請求日誌記錄。有三種方式配置請求日誌:

    • 在Jetty xml配置文件,並加到jettyXML參數中。
    • 在context xml配置文件中,並加到contextXml參數中。
    • 在webAPP元素中

server

  jetty-9.3.1之後是可選擇配置。這能夠配置org.eclipse.jetty.server.Server 實例用來支持插件的使用,然而一般是不須要配置的,由於插件會自動爲你配置。特別是你在使用jettyXml的時候你一般不肯意使用這個元素。若是你同時定義了server元素和在xml文件中進行了包含「<Configure id="Server" class="org.eclipse.jetty.server.Server">」的配置,那麼xml文件的配置將會覆蓋pom中的配置。

stopPort

  可選擇配置。一個用來監聽中止命令的端口。

stopKey

  可選擇的配置。和stopPort結合使用。

systemProperties

  可選擇的配置。容許你爲了執行插件而配置系統參數。

systemPropertiesFile

  可選擇的配置。一個包含執行插件系統參數的文件。默認狀況你在文件中設置的參數不會覆蓋在命令行中寫的參數,無論經過JVM仍是經過POM的systemProperties。

skip

  默認爲false。若是爲true的話,插件的執行會退出。一樣可使用命令-Djetty.skip進行設置。這在一體化測試你能夠經過配置取消執行時很是有用。

useProvidedScope

  默認爲false。若是爲true的話, <scope>provided</scope所依賴的位置將會被添加到容器的classpath中。注意這並非webapp的classpath,只是提供給容器使用的。因此你應該會少使用這一功能,而是明確拷貝須要的依賴。

excludedGoals

  可選擇的配置。一系列Jetty插件的名稱可使插件打印出有效的信息並退出。這在你想禁止用戶使用指定功能的時候頗有用。

 23.2.3.1 配置https鏈接器

   爲了配置https鏈接器,你須要使用Jetty xml配置文件。下面的例子是直接拷貝 etc/文件夾下的文件,固然你也能夠本身編寫你本身xml文件。咱們將使用下面的文件:

jetty.xml

  設置 org.eclipse.jetty.server.Server 實例的各類屬性,爲了讓插件可使用。重點提示,下面設置了org.eclipse.jetty.server.HttpConfiguration元素,咱們也可使用一個字xml文件來配置它。這是一個相關的部分:

    <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>
    </New>

jetty-ssl.xml

  爲https鏈接配置ssl。下面一個jetty-ssl.xml例子來自jetty-distribution:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- ============================================================= -->
<!-- SSL基礎配置                                                    -->
<!-- 這個配置文件須要和至少一個或多個                                   -->
<!-- etty-https.xml 或 jetty-http2.xml文件同時使用                   -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- 不使用協議工廠增長一個SSL鏈接                                   -->
  <!-- =========================================================== -->
  <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">
            <!-- 註釋掉用於支持代理
            <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>

  <!-- =========================================================== -->
  <!-- 基於定義在jetty.xml配置文件裏的HttpConfiguration               -->
  <!-- 建立一個基於TLS的HttpConfiguration                            -->
  <!-- 增長一個SecureRequestCustomizer來管理證書和session信息         -->
  <!-- =========================================================== -->
  <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 name="sniHostCheck" type="boolean"><Property name="jetty.ssl.sniHostCheck" default="true"/></Arg>
          <Arg name="stsMaxAgeSeconds" type="int"><Property name="jetty.ssl.stsMaxAgeSeconds" default="-1"/></Arg>
          <Arg name="stsIncludeSubdomains" type="boolean"><Property name="jetty.ssl.stsIncludeSubdomains" default="false"/></Arg>
        </New>
      </Arg>
    </Call>
  </New>

</Configure>

  如今你須要讓插件來應用上面的這個文件:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
  <configuration>
    <jettyXml>jetty.xml,jetty-ssl.xml,jetty-https.xml</jettyXml>
  </configuration>
</plugin>

 !警告

  對於Jetty的安裝來講,xml配置文件的順序是很重要的。

  你也可使用jetty xml文件來配置http鏈接供插件使用。在這咱們使用Jetty程序包中的jetty-http.xml文件:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- ============================================================= -->
<!-- 使用"Server"的ID來配置Server實例,並添加一個HTTP鏈接               -->
<!-- 這個配置文件必須和jetty.xml文件結合使用                           -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- 增長一個HTTP鏈接                                              -->
  <!-- =========================================================== -->
  <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>
                <Arg name="compliance"><Call class="org.eclipse.jetty.http.HttpCompliance" name="valueOf"><Arg><Property name="jetty.http.compliance" default="RFC7230"/></Arg></Call></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>

  如今講這個文件添加到文件列表中:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
  <configuration>
    <jettyXml>jetty.xml,jetty-http.xml,jetty-ssl.xml,jetty-https.xml</jettyXml>
  </configuration>
</plugin>

  固然也可使用httpConnector元素來配置同上面的例子同樣。

23.2.4 配置你的WebApp

  這些配置參數應用到你的webapp中。它們能夠實現全部的要求。

webApp

  這是一個繼承了org.eclipse.jetty.webapp.WebAppContext的org.eclipse.jetty.maven.plugin.JettyWebAppContext實例。你可使用這個對象上全部的方法來配置你的webapp。下面幾個是最經常使用的:

contextPath

  你web應用的根路徑。默認設置爲「/」,若是你能夠設置一個路徑在「/」下面,例如/mycontext

descriptor

  當前web應用的web.xml路徑

defaultsDescriptor

  webdefault.xml的路徑,會在web.xml以前應用這個文件內容。若是你不指定一個,那麼Jetty會使用一個在jetty-webapp.jar裏面的webdefault.xml。

overrideDescriptor

  當Jetty讀取web.xml後要覆蓋的配置文件。你能夠用這個文件來覆蓋或者增長配置。

tempDirectory

  在web應用運行時,Jetty用來擴展或者拷貝jar文件和JSP編譯後類的文件夾路徑,默認路徑是${project.build.outputDirectory}/tmp

baseResource

  Jetty靜態資源的根目錄,默認爲src/main/webapp

resourceBases

  用來替代baseResource,若是你有多個靜態目錄。這是一個地址名字的數組。

baseAppFirst

  默認爲true。控制是否要在web應用的原始資源加載前或者加載後覆蓋war包。

containerIncludeJarPattern

  默認爲"./javax.servlet-[^/]\.jar$|./servlet-api-[^/]\.jar$|.javax.servlet.jsp.jstl-[^/]\.jar|.taglibs-standard-impl-.\.jar"。這是一個匹配規則,用來匹配容器classpath(注意:是容器的classpath不是web應用的classpath)裏jar包的名字中應該被掃描的fragments,tlds和annotations 。這和context中org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern 屬性比較類似。用戶也能夠定義額外的須要被掃描的jar包。

contextXml

  應用到web應用的context的xml的路徑,在元素webApp之後。

23.2.5 jetty:run

  run 命令運行在一個web應用上,而且不須要將應用打成一個war包。相反,Jetty直接從源代碼處進行部署。它將會在Maven默認的項目路徑下尋找webapp的組成部分,你也能夠經過插件配置來覆蓋默認路徑。例如,默認它將尋找以下內容:

  • ${project.basedir}/src/main/webapp下的資源
  • ${project.build.outputDirectory}下的classes
  • ${project.basedir}/src/main/webapp/WEB-INF/下的web.xml

  插件會在部署前確保classes被從新編譯過並保證是最新的。若是你改變class的來源那麼你的IDE工具將會自動在後臺從新編譯,插件也將得到修改後的class。

  用戶也不須要將webapp打成一個war包,在部署時能夠節省時間。一旦被調用,用戶能夠配置插件來不斷的運行並掃描項目的改變,而且在有須要的時候進行熱部署。你一旦對項目作出了改變,那麼將會當即在Jetty實例中展示出來,讓你很快的進行代碼的測試,這樣比編碼、編譯、打包、測試這樣的循環要好。

  這裏有一個簡單的例子,用來每10秒鐘進行一次掃描獲取改變,而且設置web應用的根路徑爲「/」:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
  </configuration>
</plugin>

23.2.5.1 配置

  除了webApp這一個經常使用的元素外,jetty:run還支持如下:

classesDirectory

  你webapp應用編譯後classes文件的地址。你應該儘可能不要設置這個屬性,而是應該在pom.xml設置build outputDirectory這個屬性。

testClassesDirectory

  你webapp應用測試代碼編譯後classes文件的地址。默認是${project.build.testOutputDirectory}

useTestScope

  默認爲false,若爲true的話,testClassesDirectory 路徑下的classes和「test」依賴的將會被放置在classpath下。

webAppSourceDirectory

  默認的設置爲${project.basedir}/src/main/webapp,若是逆境在資源的在不一樣路徑下,能夠設置這個路徑。

jettyEnvXml

  可選擇的,jetty-env.xml文件的路徑,這個容許你對JNDI進行綁定,並將env-entry, resource-env-ref 和 resource-ref關聯到web.xml的合適位置。這將只對當前web應用有效,不能共享到其它web應用,因此你須要在部署項目的同時部署此文件(例如,經過使用jettyConfig文件)。

scanTargets

  可選擇的,須要被插件自動掃描的文件或者文件夾的列表。

scanTargetPatterns

  可選擇的,若是你有大量的文件想要掃描,最方便的作法是使用規則來匹配文件這樣要比把全部文件列出來要好。

scanClassesPattern

  9.3.0之後可選擇配置,在掃描的時候包含或者排除的規則。若是一個文件發生被修改了,但這個文件符合排除規則,那麼將不會進行從新部署。

scanTestClassesPattern

  同scanClassesPattern,用於測試類。

  下面有一個例子:

<project>
...
  <plugins>
...
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.3.11.v20160721</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 。

23.2.6 jetty:run-war

  首先打包你的應用成一個war包,而後將它部署到Jetty。若是你把scanInterval設置爲非0數值,那麼Jetty將會監視pom.xml和war文件,若是文件被改變,那麼Jetty將會從新部署這個war。

配置

war

  war包的地址。默認爲${project.build.directory}/${project.build.finalName}.war。若是這個不合適,那麼將其設置到你經常使用的地點。

下面有配置的例子:

<project>
...
  <plugins>
...
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-maven-plugin</artifactId>
      <version>9.3.11.v20160721</version>
      <configuration>
        <war>${project.basedir}/target/mycustom.war</war>
      </configuration>
    </plugin>
  </plugins>
</project

23.2.7 jetty:run-exploded

  run-exploded命令的目的是首先將你的webapp與一個已經打開的war文件關聯,而後把它部署到Jetty上。若是scanInterval參數設置了一個非0的數,那麼Jetty將會監視你的pom.xml,WEB-INF/lib,WEB-INF/classes和WEB-INF/web.xml文件,若是檢測到改變則從新部署。

配置

war

  解壓的war文件地址,默認的爲${project.build.directory}/${project.build.finalName},可是你能夠經過配置覆蓋默認值。

下面有一個例子展現如何使用:

<project>
...
  <plugins>
...
    <plugin>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>maven-jetty-plugin</artifactId>
      <version>9.3.11.v20160721</version>
      <configuration>
        <war>${project.basedir}/target/myfunkywebapp</war>
      </configuration>
    </plugin>
  </plugins>
</project>

23.2.8 jetty:deploy-war

  這個命令和jetty:run-war基本同樣,可是不會把當前模塊打成war包,你能夠指定任何路徑下的war文件來運行。

配置

war

  war文件的路徑。默認爲${project.build.directory}/${project.build.finalName},可是你能夠經過設置參數來覆蓋。

daemon

  若是設置爲true,那麼插件會運行Jetty並讓構建繼續。這是很是有用的,若是你想啓動jetty做爲執行綁定在一個特定的階段,而後阻止它在另外一個。固然,你也能夠把這個參數設置爲false,在這種狀況下,Jetty將會堵塞運行,你須要使用Ctrl+C來終止運行它。

下面有一個配置文件:

<project>
...
  <plugins>
...
  <plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>9.3.11.v20160721</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>

23.2.9 jetty:run-forked

  這個命令的做用是容許你將當前webapp運行在一個新的JVM上,能夠傳入參數到新的JVM上。命令支持同jetty:run同樣的參數配置。

配置

  可用的配置參數,比jetty:run多的以下:

jvmArgs

  可選擇配置。表示須要傳入到JVM的任意參數的字符串。

waitForChild

  默認爲true。這個須要父進程等待全部進程結束後才能退出。在這種狀況下你也可使用Ctrl+C來同時終止它們。把這個參數設置爲false或許更有效,在這種狀況下,父進程退出了,而子進程能夠繼續運行。你可使用 jetty:stop來終止子進程。

maxStarupLines

  默認爲50。子進程運行後,父進程能夠讀取子進程傳入的最大行數。若是子進程產生大量的輸出,那麼你有可能須要把這個配置的數字增大。

這個命令不會支持容器的某些配置:

scanIntervalSeconds

  不支持。不會監控並重部署應用。

reload

  不支持。不會從新部署應用。

httpConnector

  不支持。定義connectors使用jetty xml配置文件。

loginService

  不支持。定義LoginServices使用jetty xml或者容器xml文件來替代。

requestLog

  不支持。定義requestLog使用jetty xml或者容器xml文件來替代。

systemProperties

  不支持。使用 jvmArgs 參數來說系統屬性傳入命令中。

  部署你未打包的程序到新的JVM使用以下命令:

mvn jetty:run-forked

  Jetty將持續運行直到你作了以下操做的一種:

  • 輸入Ctrl+C來終止命令窗口運行來終止插件運行,這將會同時終止分出的JVM(僅當你使用waitForChild=true來啓動時)。
  • 使用jetty:stop命令,來終止分出去的JVM運行,這樣講會同時中止插件運行。

23.2.10 jetty:start

  這個命令的目的是執行時綁定你的pom.xml。它和jetty:run命令有些類似,然而它第一次運行不會編譯除非輸入「test-compile」命令來確保全部須要的classes和webapp下的全部文件都是新生成的。這個你想經過綁定的pom.xml控制啓動和終止的狀況下頗有用。

  例如,你能夠配置插件爲在你測試前啓動webapp並在你測試完成後中止webapp。爲了這麼作,你能夠設置Jetty插件的一對execution 情景。你是用 pre-integration-test和post-integration-test的Maven插件來設置Jetty啓動和終止的觸發器。

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</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>

23.2.11 jetty:stop

  stop命令會終止一個運行中的Jetty實例。爲了使用它,你須要爲這個插件配置一個特別的端口和key。相同的端口和key將被另外一個運行的Jetty使用。

stopPort

  一個Jetty的端口數字,又來監聽接收一個終止的命令並將其終止。

stopKey

  一個發送到stopPort端口的有效命令。

stopWait

  等待Jetty中止確認的最大時間間隔,單位秒。若是爲false或者爲設置,那麼插件將不會再發出命令後等待結果。

  下面有一個配置的例子。

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
  <configuration>
    <stopPort>9966</stopPort>
    <stopKey>foo</stopKey>
    <stopWait>10</stopWait>
  </configuration>
</plugin>

  而後當Jetty運行的時候,在另外一個窗口輸入如下命令:

mvn jetty:stop

  stopPort端口必須是有效的。若是不是這種狀況,那麼你將再輸入命令後獲得一個「端口已經被佔用」的錯誤消息。

23.2.12 jetty:effective-web-xml

  這個命令的做用是計算一個合成的web.xml(一個有效的web.xml),經過Servlet規範把應用組件中的全部描述文件(webdefault.xml、web.xml、web-fragment.xmls、web-override.xml),發現的註解(@WebServlet、 @WebFilter、 @WebListener)。經過有效的來源組成一個web.xml並輸出日誌。關於你應用的一些其餘有用的信息,也會存儲在effective-web.xml中。

  下面的參數配置容許你保存文件:

deleteOnExit

  默認爲true。若是設置爲false,那麼原始的web.xml將會生成到輸出路徑下的一個名爲effective-web.xml的文件中。

effectiveWebXml

  實際web.xml文件生成的全路徑名。

  注意,沒有被聲明的servlets,filters和listeners將不會被使用。

23.2.13 使用war包覆蓋

  若是你的應用須要依賴其餘war文件,jetty:run和jetty:run-forked命令能夠合併全部的資源文件。它能夠基於maven-war-plugin的配置,若是你的項目不是用maven-war-plugin,那麼它將經過簡單的算法來得到全部資源文件。

23.2.13.1 使用maven-war-plugin

  maven-war-plugin插件有合併資源的豐富經驗。 jetty:run和jetty:run-forked的命令有能力解釋你運行中程序的大多數。能夠經過如下兩個例子來了解。

  讓你的webapp依賴如下war文件:

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

  包含以下:

WebAppX:

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

WebAppY:

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

  它們能夠被maven-war-plugin配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>9.3.11.v20160721</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>

  執行 jetty:run將得到如下資源:com.acme.X.war : com.acme.Y.war: ${project.basedir}/src/main/webapp。注意,當前項目的資源將會被放在最後一位<overlay/>元素中。你可使用它或者定義<baseAppFirst>false</baseAppFirst>命令到jetty-maven-plugin.。

  此外,因爲上面配置的排除策略,請求資源 bar.jsp 將會從com.acme.Y.war中得到,而請求baz.jsp資源,將會返回404。

23.2.13.2 不是用maven-war-plugin

  這個算法是很是簡單的,經過依賴war包的聲明順序,而且不支持排除策略。<baseAppFirst>配置屬性能夠用來控制那個webapp的資源放在前面或者放在後面。

  例如,讓咱們當前webapp依賴以下兩個war:

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

  依賴的webapp包含以下:

WebAppX:

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

WebAppY:

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

  最終webapp中有效的文件爲:

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

23.2.14 配置安全設置

  你能夠在這個插件中配置LoginServices,下面有一個爲webapp設置HashLoginSercice的例子:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</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>

23.2.15 使用多個web應用根目錄

  若是你有額外的資源你想把它們包含在運行中的webAPP中,可是沒有被打進war包中,你不能使用覆蓋war包的方法,可是你能夠告訴Jetty你想添加的目錄的地址。當運行時,Jetty接收到一個資源的請求,它將搜索它配置下的全部資源文件。它和覆蓋war包的狀況很是類似,可是這不是一個war。下面有一個配置的例子:

<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>

23.2.16 運行多個web應用

  你可使用jetty.xml文件來配置你想要部署的額外的webapp,或者你可使用<contextHandlers> 配置元素來這麼作。若是你想部署webapp A和webapp B和webapp C到你的Jetty實例中:

  將這些配置信息放到webapp A的pom.xml文件中:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</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>

 重要:

  若是你是用ContextHandler 部署的是一個webapp,最好的作法是使用org.eclipse.jetty.maven.plugin.JettyWebAppContext實例而不是使用org.eclipse.jetty.webapp.WebAppContext實例。只有前者纔會容許webapp 在maven環境中正常工做。

  一樣的,你也能夠增長一個jetty.xml文件到你的webapp A中。而後複製這個到其餘,併爲其它兩個應用增長WebAppContexts:

<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>

  這將把jetty.xml的地址配置到A的 Jetty插件中:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>9.3.11.v20160721</version>
  <configuration>
    <scanIntervalSeconds>10</scanIntervalSeconds>
    <webApp>
      <contextPath>/test</contextPath>
    </webApp>
    <jettyXml>src/main/etc/jetty.xml</jettyXml>
  </configuration>
</plugin>

  對於上面兩種解決方案,其它webapp必須是已經構建好,而且它們沒有被自動監聽文件改變。

23.2.17 設置系統屬性

  你能夠特別的指定 name/value 對設置到系統屬性中,爲Jetty插件使用。這樣的特性能夠整理輸入命令並節省大量的輸入。

  然而,有時不能使用這種功能來設置系統屬性 - 有時使用系統屬性的軟件組件已經在maven運行的時候進行了初始化(這種狀況下,你須要在命令行上提供系統屬性),或者Jetty運行的時候。在後一種狀況,你可使用maven的屬性插件來定義系統屬性。下面有一個例子又來配置logback日誌系統做爲Jetty的日誌記錄:

<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>

  注意,若是一個系統屬性已經被設置(例如,從命令行或者被JVM本身設置),那麼默認這些屬性將不會被覆蓋(查看下面的<force>屬性)。

23.2.17.1 在POM中設置系統屬性

  這裏有一個例子展現如何在POM中設置系統屬性:

<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>

  爲了改變默認行爲,使用<force>參數來保證系統屬性覆蓋命令行參數:

<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>

23.2.17.2 在一個文件中設置系統屬性

  你也能夠在一個文件中指定系統屬性。在這種方式指定的系統屬性將不會覆蓋命令行的參數的系統屬性、JVM的系統屬性、POM中的系統屬性。

  包含以下相似內容的一個名爲mysys.props的文件:

fooprop=222

  這能夠被配置到插件中,以下所示:

<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>

  你也可使用經過系統屬性jetty.systemPropertiesFile來設置文件位置。

23.3 Jetty Maven插件的文件掃描

  若是你設置了一個非0的scanInterval 屬性,那麼Jetty的maven插件將會在每間隔指定的時間掃描肯定的文件,若是發現改變在須要的狀況下將會從新部署。掃描的文件將依賴於執行的目標。

 
命令 掃描文件
jetty:run

pom.xml, <dependencies>, <classesDirectory>, <testClassesDirectory>, <webXml>

或者 <webAppSourceDirectory>/WEB-INF/web.xml, <jettyEnvXml>

或者 <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <webAppSourceDirectory>/WEB-INF/jetty-web.xml,

<scanTargets>, <scanTargetPatterns>, 任何webapp描述的默認描述文件和覆蓋描述文件。

jetty:run-war pom.xml, <war>
jetty:run-exploded

pom.xml, <war>/WEB-INF/web.xml, <war>/WEB-INF/jetty-web.xml, <war>/WEB-INF/jetty-env.xml,

<war>/WEB-INF/classes, <war>/WEB-INF/lib

jetty:deploy-war pom.xml, <war>
jetty:run-forked  
jetty:start

pom.xml, <dependencies> from the pom, <classesDirectory>, <testClassesDirectory>, <webXml>

或者 :

<webAppSourceDirectory>/WEB-INF/web.xml, <jettyEnvXml>

或者:

 <webAppSourceDirectory>/WEB-INF/jetty-web.xml, <webAppSourceDirectory>/WEB-INF/jetty-web.xml,

<scanTargets>, <scanTargetPatterns>, 任何webapp描述的默認描述文件和覆蓋描述文件。

jetty:stop  

23.4 Jetty Jspc的Maven插件

   這個插件將幫助你預編譯你的JSP並和maven war插件結合起來時候,將它們放到封裝的war包中。

23.4.1 配置

  將jspc插件放到你的構建環境中有一個級別的安裝操做:

<plugin>
  <groupId>org.eclipse.jetty</groupId>
   <artifactId>jetty-jspc-maven-plugin</artifactId>
   <version>9.3.11.v20160721</version>
   <executions>
     <execution>
       <id>jspc</id>
       <goals>
         <goal>jspc</goal>
       </goals>
       <configuration>
       </configuration>
     </execution>
   </executions>
 </plugin>

  配置的參數以下:

webXmlFragment

  默認值爲: $\{project.basedir}/target/webfrag.xml,一個包含servlet聲明的文件,將會被合併到已經存在的web.xml中。

webAppSourceDirectory

  默認值:$\{project.basedir}/src/main/webapp ,用來防止jsps、tags等資源的根目錄。

webXml

  默認值:$\{project.basedir}/src/main/webapp/WEB-INF/web.xml,web.xml文件用來和已經存在的片斷合成。

includes

  默認值:\/.jsp, \/.jspx ,一系列匹配須要的文件的條件。

excludes

  默認值:\/.svn\/,一系列須要排除的條件。

classesDirectory

  默認值:$\{project.build.outputDirectory},webapp的classes的路徑。

generatedClasses

  默認值: $\{project.build.outputDirectory} ,用來放置jsps生成的classes位置。

insertionMarker

  默認值:none,在原web.xml文中的標記字符串,代表須要被合併到web.xml中的片斷。注意,這個標記的字符串在插入的時候不會被隱藏掉。能夠留空,這種狀況下將會被插入到</web-app>以前。

useProvidedScope

  默認值:false,若是爲true,標有<scope>provided</scope> 中的jar包將會被classpath中的jar包替換。

mergeFragment

  默認爲true,不管是否將與源web.xml進行合併。合併後的文件將會放到與webXmlFragment文件同一路徑。

keepSources

  默認爲false,若是爲true的話,默認 .java 文件不會再執行的時候刪除。

sourceVersion

  jetty-9.3.6 版本之後的屬性。jsp源文件的Java版本。默認爲1.7。

targetVersion

  jetty-9.3.6 版本之後的屬性。jsps生成class文件的版本。默認爲1.7。

tldJarNamePatterns

  默認值:taglibs[^/]\.jar|.jstl-impl[^/]\.jar$,系統路徑下,包含tlds文件的jar包匹配條件。使用 | 來分割每個條件。

jspc

  默認值:被配置過的the org.apache.jasper.JspC。JspC類用來進行預編譯。全部在JspC類上的setter都是有效的。

  使用全部默認設置,下面有一個使用普通web.xml,包含全部jsp servlet聲明的war插件配置:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <configuration>
    <webXml>${project.basedir}/target/web.xml</webXml>
  </configuration>
</plugin>

23.4.2 只在構建時預編譯

  通常只有在準備正式版本的時候編譯jsp 可是在開發過程當中不太常常編譯,最方便的作法是將插件放置到<profile> 中,這樣能夠僞裝準備生產版本而進行編譯。

  例如,下面的profile只有在 -Dprod被輸入時纔會被執行:

<profiles>
    <profile>
      <id>prod</id>
      <activation>
        <property><name>prod</name></property>
      </activation>
      <build>
      <plugins>
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-jspc-maven-plugin</artifactId>
          <version>9.3.11.v20160721</version>
          <!-- put your configuration in here -->
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <!-- put your configuration in here -->
        </plugin>
      </plugins>
      </build>
    </profile>
  </profiles>

  這樣,下面的調用將會讓你的代碼進行編譯,讓jsp進行編譯,<servlet>和<servlet-mapping>被插入到web.xml是,並將你的項目打成war包。

$ mvn -Dprod package

23.4.3 覆蓋war時預編譯

  覆蓋war時對jsp預編譯須要多一點的配置。這是由於你須要分步驟解壓須要覆蓋的war文件,並將它們從新打包。因此jetty-jspc-maven-plugin講有可能訪問到覆蓋的資源。

  像下面例子中展現的,咱們將使用一個覆蓋的war。覆蓋的war文件將提供web.xml,可是jsps將被放置到src/main/webapp。咱們將解壓war文件,編譯jsps而且將servlet的定義合併到已經存在的web.xml中,而後將其打成war包。

  這裏有一個配置war插件的例子,分爲解壓步驟和打包步驟:

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack</id>
        <goals><goal>exploded</goal></goals>
        <phase>generate-resources</phase>
        <configuration>
          <webappDirectory>target/foo</webappDirectory>
          <overlays>
            <overlay />
            <overlay>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>test-jetty-webapp</artifactId>
            </overlay>
          </overlays>
        </configuration>
      </execution>
      <execution>
        <id>pack</id>
        <goals><goal>war</goal></goals>
        <phase>package</phase>
        <configuration>
          <warSourceDirectory>target/foo</warSourceDirectory>
          <webXml>target/web.xml</webXml>
        </configuration>
      </execution>
    </executions>
</plugin>

  接下來你讓須要配置jetty-jspc-maven-plugin,這樣插件才能使web.xml與額外的war文件中的servlets定義進行合併。這時在target/foo/WEB-INF/web.xml 中。使用默認的配置,web.xml將於jsp servlet 定義進行合併並放到target/web.xml中。

<plugin>
    <groupId>org.eclipse.jetty</groupId>
     <artifactId>jetty-jspc-maven-plugin</artifactId>
     <version>9.3.11.v20160721</version>
     <executions>
       <execution>
         <id>jspc</id>
         <goals>
           <goal>jspc</goal>
         </goals>
         <configuration>
            <webXml>target/foo/WEB-INF/web.xml</webXml>
            <includes>**/*.foo</includes>
            <excludes>**/*.fff</excludes>
        </configuration>
      </execution>
    </executions>
</plugin>

 

 

附言:

  Jetty文檔的目錄詳見:http://www.cnblogs.com/yiwangzhibujian/p/5832294.html

  Jetty第一章翻譯詳見:http://www.cnblogs.com/yiwangzhibujian/p/5832597.html

  Jetty第四章(21-22)詳見:http://www.cnblogs.com/yiwangzhibujian/p/5845623.html

  這是翻譯的第四部分的第23小節,主要是Maven的使用,若是項目不使用Maven管理能夠跳過本文。一開始翻譯老是感受表達不出英文的本意,不過慢慢以爲我要作的不是一字不差的把原文翻譯過來,而是要用中文的思惟來表達英文的意思,我以爲之後的翻譯會容易理解些。

相關文章
相關標籤/搜索