jetty與maven集成

          自己做個小項目,使用了 maven2 ,一直使用 tomcat ,但很不方便。采用直接改 server.xml 指向項目路徑的方法,但這樣要求把編譯路徑改向 WEB-INF/classes ,而且要求把需要的 jar 放到 WEB-INF/lib 下去。而如果修改了 pom 文件,就需要重新 mvn eclipse:eclipse ,修改項目文件,然后編譯路徑又會變成 target/classes ,然后還需要把 jar 拷到 lib 下去,很是麻煩。嘗試使用 resin ,也是采用 maven 的方式,但官方 maven repository 上只有一個版本,然后需要 jca1.5 ,結果上面又沒有,自己作弊吧,終歸不爽。正好 alin 建議用 jetty ,就按他的文檔嘗試了一下。

          Pom 文件中加上以下依賴,然后 mvn eclipse:eclipse ,會到官方的 repo down 這些 jar ,其中 ant 因為較大( 1M )可能會失敗,可以重試解決。如果這些 jar 的同一版本以前 maven 下過就不會重復下,這也體現了 maven 的一些優勢。

          < dependency >

          ??????????? < groupId > tomcat </ groupId >

          ??????????? < artifactId > jasper-compiler </ artifactId >

          ??????????? < version > 5.5.15 </ version >

          ?????????? < scope > test </ scope > ????????

          ??????? </ dependency >

          ??????? < dependency >

          ??????????? < groupId > tomcat </ groupId >

          ??????????? < artifactId > jasper-runtime </ artifactId >

          ??????????? < version > 5.5.15 </ version >

          ?????????? < scope > test </ scope > ????????

          ??????? </ dependency >

          ?????? < dependency >

          ??????????? < groupId > commons-el </ groupId >

          ??????????? < artifactId > commons-el </ artifactId >

          ??????????? < version > 1.0 </ version >

          ?????????? < scope > test </ scope > ????????

          ??????? </ dependency > ????????????

          ???? ??? < dependency >

          ??????????? < groupId > org.mortbay.jetty </ groupId >

          ??????????? < artifactId > org.mortbay.jetty </ artifactId >

          ??????????? < version > 5.1.10 </ version >

          ?????????? < scope > test </ scope > ????????

          ??????? </ dependency >

          ??????? < dependency >

          ??????????? < groupId > javax.servlet </ groupId >

          ??????????? < artifactId > jsp-api </ artifactId >

          ??????????? < version > 2.0 </ version >

          ?????????? < scope > test </ scope > ????????

          ??????? </ dependency >

          ??????? < dependency >

          ??????????? < groupId > ant </ groupId >

          ??????????? < artifactId > ant </ artifactId >

          ??????????? < version > 1.6.5 </ version >

          ?????????? < scope > test </ scope >

          ??????? </ dependency >

          然后放寫一個 RunJetty.Java ,同包放一個 jetty-config.xml (建議放 test 相關包里)。在 eclipse 里運行 RunJetty ,就啟動 jetty 了,它會使用當前項目的 class loader ,所以不用更改編譯路徑,也不用拷貝 jar 了。 jetty-config 的配置很簡單,大家一看就知道了。具體文件如下:

          import java.net.URL;

          import org.apache.commons.logging.Log;

          import org.apache.commons.logging.LogFactory;

          import org.mortbay.jetty.Server;

          ?

          public class RunJetty {

          ?????? private static Log log = LogFactory.getLog(RunJetty.class);

          ?

          ?????? /**

          ?????? ?* @param args

          ?????? ?*/

          ?????? public static void main(String[] args) {

          ????????????? Server jettyServer = null;

          ????????????? try {

          ???????????????????? URL jettyConfig = RunJetty.class.getResource("jetty-config.xml");

          ???????????????????? if (jettyConfig == null) {

          ??????????????????????????? log

          ????????????????????????????????????????? .fatal("Unable to locate jetty-test-config.xml on the classpath");

          ???????????????????? }

          ???????????????????? jettyServer = new Server(jettyConfig);

          ???????????????????? jettyServer.start();

          ????????????? } catch (Exception e) {

          ???????????????????? log.fatal("Could not start the Jetty server: " + e);

          ???????????????????? if (jettyServer != null) {

          ??????????????????????????? try {

          ?????????????????????????????????? jettyServer.stop();

          ??????????????????????????? } catch (InterruptedException e1) {

          ?????????????????????????????????? log.fatal("Unable to stop the jetty server: " + e1);

          ??????????????????????????? }

          ???????????????????? }

          ????????????? }

          ?????? }

          ?

          }

          ----------------------------------------------?

          <? xml version = "1.0" ?>

          <! DOCTYPE Configure PUBLIC "-//MortBay Consulting//DTD Configure 1.2//EN"

          ??? "http://jetty.mortbay.org/configure_1_2.dtd" >

          < Configure class = "org.mortbay.jetty.Server" >

          ??? < Call name = "addListener" >

          ?????? < Arg >

          ?????????? < New class = "org.mortbay.http.SocketListener" >

          ????????????? < Set name = "Port" type = "int" >

          ????????????????? < SystemProperty name = "jetty.port" default = "80" />

          ????????????? </ Set >

          ?????????? </ New >

          ?????? </ Arg >

          ??? </ Call >

          ??? < Call name = "addWebApplication" >

          ?????? < Arg > / </ Arg >

          ?????? < Arg > web </ Arg >

          ??? </ Call >

          ??? < Set name = "requestsPerGC" type = "int" > 2000 </ Set >

          ??? < Set name = "statsOn" type = "boolean" > false </ Set >

          </ Configure >

          posted on 2006-05-15 11:19 pesome 閱讀(2057) 評論(0)  編輯  收藏 所屬分類: 開源軟件

          <2006年5月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導航

          統計

          公告

          主要記錄作者在學習java中的每一步足跡。除非特別說明,所有文章均為本blog作者原創,如需轉載請注明出處和原作者,如用于商業目的,需跟作者本人聯系。
          歡迎大家訪問:

          常用鏈接

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          java技術

          人間百態

          朋友們的blog

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 英超| 茶陵县| 馆陶县| 泸溪县| 西藏| 兰坪| 铜鼓县| 佛冈县| 精河县| 泗洪县| 乐平市| 施秉县| 罗江县| 巴青县| 江川县| 兴仁县| 江门市| 澄迈县| 吉首市| 林州市| 山阴县| 玉树县| 通城县| 增城市| 五寨县| 大姚县| 浙江省| 贵定县| 方城县| 盐池县| 永安市| 聂荣县| 甘南县| 商水县| 尖扎县| 酒泉市| 河间市| 湘乡市| 巴楚县| 娱乐| 襄垣县|