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
因為較大(
<
dependency
>
???????????
<
groupId
>
tomcat
</
groupId
>
???????????
<
artifactId
>
jasper-compiler
</
artifactId
>
???????????
<
version
>
??????????
<
scope
>
test
</
scope
>
????????
???????
</
dependency
>
???????
<
dependency
>
???????????
<
groupId
>
tomcat
</
groupId
>
???????????
<
artifactId
>
jasper-runtime
</
artifactId
>
???????????
<
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
>
??????????
<
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
>
??????????
<
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
"-//
???
"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 閱讀(2047) 評論(0) 編輯 收藏 所屬分類: 開源軟件