隨筆-126  評(píng)論-247  文章-5  trackbacks-0

          本機(jī)環(huán)境

          JDK 7 Maven 3.2 Jetty 9.2 Eclipse Luna

          pom.xml 配置

          在你的 pom.xml 文件中添加 jetty 插件的描述信息(查看Jetty更多的版本信息):
          [...]
          <build>
            <plugins>
              <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.8.v20150217</version>
              </plugin>
            </plugins>
          </build>
          [...]

          啟動(dòng) & 停止

          命令行方式啟動(dòng) jetty mvn jetty:run,可以通過 Ctrl + C 停止 jetty 服務(wù)。
          或者,在 eclipse 中選中項(xiàng)目 --> 右鍵 --> Run As --> Maven build...,在 Goals 欄輸入 jetty:run(與命令行方式相比,僅僅是
          少了 mvn 前綴,為方便起見,以下均以命令行方式介紹。)

          jetty 9 部署的項(xiàng)目的 Context path 默認(rèn)是 /,也就是說,項(xiàng)目的訪問入口地址是:http://localhost:8080(不帶項(xiàng)目名)
          如果你希望通過命令 mvn jetty:stop 執(zhí)行關(guān)閉 jetty 服務(wù),你需要像下面一樣在你的 pom.xml 配置文件中添加一個(gè)特殊的端口和控制鍵:
          <configuration>
            [...]
            <stopKey>shutdown</stopKey>
            <stopPort>9966</stopPort>
            [...]
          </configuration>
          你仍可以通過 mvn jetty:run 啟動(dòng) jetty 服務(wù),可以通過 mvn jetty:stop 來停止 jetty 服務(wù)。

          取消文件映射緩存

          jetty 默認(rèn)開啟了 useFileMappedBuffer,在 jetty 運(yùn)行期間,頁面所使用的靜態(tài)文件(如 css 文件等)不允許修改。如果你嘗試去修改它
          們,保存的時(shí)候就會(huì)出現(xiàn) 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>
          先確認(rèn) jetty 服務(wù)已經(jīng)停止,將原文件 jetty-webapp-9.2.8.v20150217.jar/org/eclipse/jetty/webapp/webdefault.xml 刪除,將剛
          才那份修改好的 webdefault.xml 文件重新壓縮進(jìn)去即可。

          端口配置

          jetty 默認(rèn)使用的端口是 8080,命令行的方式修改端口的命令是:mvn -Djetty.port=8081 jetty:run 。pom.xml 配置方式如下:
          <configuration>
            [...]
            <httpConnector>
              <port>8081</port>
            </httpConnector>
            [...]
          </configuration>

          自動(dòng)熱部署

          在你的 pom.xml 中添加如下配置:
          <configuration>
            [...]
            <scanIntervalSeconds>2</scanIntervalSeconds>
            [...]
          </configuration>
          默認(rèn)值是 0。大于 0 的數(shù)值表示開啟,0 表示關(guān)閉,單位為秒。以配置數(shù)值為一個(gè)周期,自動(dòng)的掃描文件檢查其內(nèi)容是否有變化,如果發(fā)現(xiàn)文件的
          內(nèi)容被改變,則自動(dòng)重新部署運(yùn)用。命令行的方式:mvn -Djetty.scanIntervalSeconds=2 jetty:run 。

          手動(dòng)重加載

          在你的 pom.xml 文件中添加如下配置,reload 的可選值 :[automatic|manual]
          <configuration>
            [...]
            <reload>manual</reload>
            [...]
          </configuration>
          默認(rèn)值為 automatic,它與大于 0 的 scanIntervalSeconds 節(jié)點(diǎn)一起作用,實(shí)現(xiàn)自動(dòng)熱部署的工作。設(shè)為 manual 的好處是,當(dāng)你改變文件
          內(nèi)容并保存時(shí),不會(huì)馬上觸發(fā)自動(dòng)掃描和重部署的動(dòng)作,你還可以繼續(xù)的修改,直至你在 Console 或命令行中敲回車鍵(Enter)的時(shí)候才觸發(fā)重
          新加載的動(dòng)作。這樣可以更加的方便調(diào)試修改。命令行的方式是: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 的一個(gè)實(shí)現(xiàn)類。
          org.eclipse.jetty.server.NCSARequestLog 是一種偽標(biāo)準(zhǔn)的 NCSA 日志格式。下面是一些節(jié)點(diǎn)參數(shù)的解釋:
          filename:日志文件的名稱
          filenameDateFormat:日志文件的名稱的日期格式,它要求日志文件名必須含有 yyyy_mm_dd 串
          logDateFormat:日志內(nèi)容的時(shí)間格式
          logTimeZone:時(shí)區(qū)
          append:追加到日志
          logServer:記錄訪問的主機(jī)名
          retainDays:日志文件保存的天數(shù), 超過刪除
          logCookies:記錄 cookies
          啟動(dòng) jetty 服務(wù),在項(xiàng)目的 target 目錄下會(huì)生成一個(gè) 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"

          轉(zhuǎn)儲(chǔ)快照

          在你的 pom.xml 文件添加如下配置:
          <configuration>
            [...]
            <dumpOnStart>true</dumpOnStart>
            [...]
          </configuration>
          dumpOnStart 默認(rèn)值為 false,如果設(shè)為 true,jetty 在啟動(dòng)時(shí)會(huì)把當(dāng)前服務(wù)進(jìn)程的內(nèi)存信息輸出到控制臺(tái)中,但這并不會(huì)保存到文件中。

          WEB上下文

          最常用的是 contextPath,它的配置如下:
          <configuration>
            [...]
            <webApp>
              <contextPath>/${project.artifactId}</contextPath>
            </webApp>
            [...]
          </configuration>
          contextPath 的默認(rèn)值的 /,${project.artifactId} 引用了 <artifactId> 節(jié)點(diǎn)的值,即項(xiàng)目的名稱。
          項(xiàng)目的靜態(tài)資源文件目錄默認(rèn)是 src/main/webapp,如果靜態(tài)資源目錄有多個(gè),或者不在默認(rèn)的 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>
          引用靜態(tài)資源文件時(shí),路徑不包含資源目錄的名稱,如 commons/main.css,引用方式為:<link href="main.css" rel="stylesheet" /> 
          更多參數(shù)信息可參考 jetty-maven-plugin.html#configuring-your-webapp

          完整的配置

          附 pom.xml 文件中 jetty 插件的完整配置片段:
          <build>
            [...]
            <plugins>
              <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>9.2.8.v20150217</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>
          更多有關(guān) jetty 的配置信息可參考 http://www.eclipse.org/jetty/documentation/current/jetty-maven-plugin.html


            
          posted on 2015-06-23 15:05 fancydeepin 閱讀(49103) 評(píng)論(5)  編輯  收藏 所屬分類: maven

          評(píng)論:
          # re: maven jetty plugin 2013-07-30 10:48 | 工作
          寫的不錯(cuò)哦  回復(fù)  更多評(píng)論
            
          # re: maven jetty plugin 2014-08-15 17:34 | 黑客的蝸牛
          +1,博主加油!  回復(fù)  更多評(píng)論
            
          # re: maven jetty 插件使用 2015-07-22 14:59 | yuxh
          內(nèi)容格式都很清晰,希望博主提供jetty更深入一點(diǎn)的介紹和應(yīng)用(優(yōu)點(diǎn)、為什么在maven中使用這個(gè)插件、和tomcat插件比怎么樣、和CARGO怎么聯(lián)系)  回復(fù)  更多評(píng)論
            
          # re: maven jetty 插件使用 2015-12-25 10:06 | 邱哥
          博文寫的很詳細(xì),非常棒!!  回復(fù)  更多評(píng)論
            
          # re: maven jetty 插件使用 2016-01-21 16:16 | hyuga
          贊,樓主寫得真棒  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 清水河县| 平南县| 淮安市| 靖边县| 加查县| 舒城县| 英吉沙县| 扬州市| 高尔夫| 洛阳市| 博白县| 宁陵县| 濉溪县| 晋江市| 淮安市| 怀柔区| 昌江| 青神县| 商丘市| 海林市| 特克斯县| 聂拉木县| 得荣县| 永吉县| 平江县| 顺平县| 大方县| 枣强县| 古蔺县| 霍林郭勒市| 高淳县| 商河县| 资阳市| 奎屯市| 恩平市| 叙永县| 赤水市| 庄浪县| 晋州市| 兰西县| 成安县|