1. 程式人生 > >Maven集成jetty插件

Maven集成jetty插件

res java server edi logserver interval like val ref

本機環境

JDK 7 Maven 3.2 Jetty 8.1.9 Eclipse Luna

pom.xml 配置

在你的 pom.xml 文件裏加入 jetty 插件的描寫敘述信息(查看Jetty很多其它的版本號信息):
[...]  
<build>  
  <plugins>  
    <plugin>  
      <groupId>org.mortbay.jetty</groupId>  
      <artifactId>jetty-maven-plugin</artifactId>  
      <version>8.1.9.v20130131</version>  
    </plugin>  
  </plugins>  
</build>  
[...]  


啟動 & 停止

命令行方式啟動 jetty mvn jetty:run。能夠通過 Ctrl + C 停止 jetty 服務。
或者,在 eclipse 中選中項目 --> 右鍵 --> Run As --> Maven build...。在 Goals 欄輸入 jetty:run與命令行方式相比,不過
少了 mvn 前綴,為方便起見,下面均以命令行方式介紹。
技術分享
jetty 8 部署的項目的 Context path 默認是 /。也就是說,項目的訪問入口地址是:http://localhost:8080(不帶項目名)
假設你希望通過命令 mvn jetty:stop
運行關閉 jetty 服務。你須要像以下一樣在你的 pom.xml 配置文件裏加入一個特殊的port和控制鍵:
<configuration>  
  [...]  
  <stopKey>shutdown</stopKey>  
  <stopPort>9966</stopPort>  
  [...]  
</configuration>  


你仍能夠通過 mvn jetty:run 啟動 jetty 服務,能夠通過 mvn jetty:stop 來停止 jetty 服務。

取消文件映射緩存

jetty 默認開啟了 useFileMappedBuffer。在 jetty 執行期間,頁面所使用的靜態文件(如 css 文件等)不同意改動。假設你嘗試去改動它
們,保存的時候就會出現 Save could not be completed.
技術分享
解決的方法。找到 %repo%/org/eclipse/jetty/jetty-webapp/9.2.8.v20150217/jetty-webapp-9.2.8.v20150217.jar(%repo% 表示你
本地的 maven 倉庫的文件夾。另外。將 9.2.8.v20150217 換成你所使用的版本號)。用壓縮工具打開它, 找到 jetty-webapp-9.2.8.v2015021
7.jar/org/eclipse/jetty/webapp/webdefault.xml,將 webdefault.xml 文件解壓縮一份出來,用文本編輯器打開它,搜索找到
useFileMappedBuffer 配置的行,將 true 改成 false 以禁掉緩存。

<init-param>  
  <param-name>useFileMappedBuffer</param-name>  
  <param-value>false</param-value>  
</init-param>  


先確認 jetty 服務已經停止。將原文件 jetty-webapp-9.2.8.v20150217.jar/org/eclipse/jetty/webapp/webdefault.xml 刪除,將剛
才那份改動好的 webdefault.xml 文件又一次壓縮進去就可以。

port配置

jetty 默認使用的port是 8080,命令行的方式改動port的命令是:mvn -Djetty.port=8081 jetty:run 。pom.xml 配置方式例如以下:

<configuration>  
  [...]  
  <httpConnector>  
    <port>8081</port>  
  </httpConnector>  
  [...]  
</configuration> 


自己主動熱部署

在你的 pom.xml 中加入例如以下配置:

<configuration>  
  [...]  
  <scanIntervalSeconds>2</scanIntervalSeconds>  
  [...]  
</configuration>  


默認值是 0。

大於 0 的數值表示開啟,0 表示關閉,單位為秒。

以配置數值為一個周期,自己主動的掃描文件檢查其內容是否有變化,假設發現文件的
內容被改變。則自己主動又一次部署運用。命令行的方式:mvn -Djetty.scanIntervalSeconds=2 jetty:run

手動重載入

在你的 pom.xml 文件裏加入例如以下配置,reload 的可選值 :[automatic|manual]

<configuration>  
  [...]  
  <reload>manual</reload>  
  [...]  
</configuration>  


默認值為 automatic,它與大於 0 的 scanIntervalSeconds 節點一起作用,實現自己主動熱部署的工作。設為 manual 的優點是,當你改變文件

內容並保存時。不會立即觸發自己主動掃描和重部署的動作,你還能夠繼續的改動。直至你在 Console 或命令行中敲回車鍵(Enter)的時候才觸發重
新載入的動作。

這樣能夠更加的方便調試改動。命令行的方式是:mvn -Djetty.reload=manual jetty:run

訪問日誌

在你的 pom.xml 文件加入例如以下配置:

<configuration>  
  [...]  
  <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">  
    <filename>target/access-yyyy_mm_dd.log</filename>  
    <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>  
    <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>  
    <logTimeZone>GMT+8:00</logTimeZone>  
    <append>true</append>  
    <logServer>true</logServer>  
    <retainDays>120</retainDays>  
    <logCookies>true</logCookies>  
  </requestLog>  
  [...]  
</configuration>  


org.eclipse.jetty.server.NCSARequestLogorg.eclipse.jetty.server.RequestLog 的一個實現類。

org.eclipse.jetty.server.NCSARequestLog 是一種偽標準的 NCSA 日誌格式。以下是一些節點參數的解釋:
filename:日誌文件的名稱
filenameDateFormat:日誌文件的名稱的日期格式,它要求日誌文件名稱必須含有 yyyy_mm_dd 串
logDateFormat:日誌內容的時間格式
logTimeZone:時區
append:追加到日誌
logServer:記錄訪問的主機名
retainDays:日誌文件保存的天數, 超過刪除
logCookies:記錄 cookies
啟動 jetty 服務,在項目的 target 文件夾下會生成一個 access-2015_06_23.log 文件,該文件裏的當中一條記錄例如以下: localhost 0:0:0:0:0:0:0:1 - - [2015-06-23 01:17:05] "GET /css/main.css HTTP/1.1" 304 -
"http://localhost:8081/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0" "JSESSIONID=2gyikovul2iz168116l2afo4f"

轉儲快照

在你的 pom.xml 文件加入例如以下配置:

<configuration>  
  [...]  
  <dumpOnStart>true</dumpOnStart>  
  [...]  
</configuration>  


dumpOnStart 默認值為 false,假設設為 true。jetty 在啟動時會把當前服務進程的內存信息輸出到控制臺中,但這並不會保存到文件裏。

WEB上下文

最經常使用的是 contextPath,它的配置例如以下:

<configuration>  
  [...]  
  <webApp>  
    <contextPath>/${project.artifactId}</contextPath>  
  </webApp>  
  [...]  
</configuration>  


contextPath 的默認值的 /,${project.artifactId} 引用了 <artifactId> 節點的值,即項目的名稱。

項目的靜態資源文件文件夾默認是 src/main/webapp,假設靜態資源文件夾有多個。或者不在默認的 src/main/webapp 文件夾下。可做例如以下配置:

<configuration>  
  [...]  
  <webApp>  
    <contextPath>/${project.artifactId}</contextPath>  
    <resourceBases>  
      <resourceBase>${project.basedir}/src/main/webapp</resourceBase>  
      <resourceBase>${project.basedir}/commons</resourceBase>  
    </resourceBases>  
  </webApp>  
  [...]  
</configuration>  


引用靜態資源文件時,路徑不包括資源文件夾的名稱,如 commons/main.css,引用方式為:<link href="main.css" rel="stylesheet" />

很多其它參數信息可參考 jetty-maven-plugin.html#configuring-your-webapp

完整的配置

附 pom.xml 文件裏 jetty 插件的完整配置片段:

<build>  
  [...]  
  <plugins>  
    <plugin>  
      <groupId>org.eclipse.jetty</groupId>  
      <artifactId>jetty-maven-plugin</artifactId>  
      <version>8.1.9.v20130131</version>  
      <configuration>  
        <httpConnector>  
          <port>8081</port>  
        </httpConnector>  
        <stopKey>shutdown</stopKey>  
        <stopPort>9966</stopPort>  
        <!-- 
        <scanIntervalSeconds>2</scanIntervalSeconds> 
        -->  
        <reload>manual</reload>  
        <dumpOnStart>true</dumpOnStart>  
        <webApp>  
          <contextPath>/${project.artifactId}</contextPath>  
          <!--  
          <resourceBases>  
            <resourceBase>${project.basedir}/src/main/webapp</resourceBase>  
            <resourceBase>${project.basedir}/commons</resourceBase>  
          </resourceBases>  
          -->  
        </webApp>  
        <requestLog implementation="org.eclipse.jetty.server.NCSARequestLog">  
          <filename>target/access-yyyy_mm_dd.log</filename>  
          <filenameDateFormat>yyyy_MM_dd</filenameDateFormat>  
          <logDateFormat>yyyy-MM-dd HH:mm:ss</logDateFormat>  
          <logTimeZone>GMT+8:00</logTimeZone>  
          <append>true</append>  
          <logServer>true</logServer>  
          <retainDays>120</retainDays>  
          <logCookies>true</logCookies>  
        </requestLog>  
      </configuration>  
    </plugin>  
  </plugins>  
  [...]  
</build>  


很多其它有關 jetty 的配置信息可參考 http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html

Maven集成jetty插件