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 阅读(2033) 评论(0)  编辑  收藏 所属分类: 开源软件


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

公告

主要记录作者在学习java中的每一步足迹。除非特别说明,所有文章均为本blog作者原创,如需转载请注明出处和原作者,如用于商业目的,需跟作者本人联系。
欢迎大家访问:

常用链接

留言簿(16)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

java技术

人间百态

朋友们的blog

搜索

最新评论

阅读排行榜

评论排行榜