個人覺得最好還是不要再pom.xml中寫入關於maven-jetty-plugin的信息,這個應該是和項目無關的。只需在eclipse設置變量即可。
Maven2代比1代改進很多,其中主要強調的是--它不僅僅是個依賴包管理器!
開始先要推薦一個專講Maven2的電子書給大家,對MAVEN學習相當有助益:Better Builds with Maven
下面就專門介紹下Maven2對WEBAPP在管理和調試方面的支持。
1.創建項目
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
也可參看這裏
創建要注意遵循MAVEN的目錄結構,尤其要注意源文件要放在main/java下:
2. POM文件的配置
這裏要特別注意對resource一節的配置,因爲我的SPRING以及WEB相關的XML是放在WEB-INF目錄,爲了在unit test的時候也能用,加入了對這些配置文件的引用。相當於加了一個classpath。
這裏還有個插曲:不知爲何MAVEN2裏沒有JTA的包,自動下載時會有提示教你如何手工通過命令加入,非常簡單。
JETTY的plugin是爲後面用它來調試做準備。
DWR也是目前WEB開發一個熱選。
另外,爲使用JAVA5代來編譯,加入了maven-compiler-plugin一節。
xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" >
< modelVersion > 4.0.0 </ modelVersion >
< groupId > com.exchangebit.nms </ groupId >
< artifactId > ebnms </ artifactId >
< packaging > war </ packaging >
< version > 1.0-SNAPSHOT </ version >
< name > ebnms Maven Webapp </ name >
< url > http://maven.apache.org </ url >
< build >
< finalName > ebnms </ finalName >
< resources >
< resource >
< directory > src/main/java </ directory >
< includes >
< include > **/*.xml </ include >
</ includes >
</ resource >
< resource >
< directory > src/main/webapp/WEB-INF </ directory >
< includes >
< include > **/*.xml </ include >
< include > **/log4j.properties </ include >
</ includes >
</ resource >
</ resources >
< plugins >
< plugin >
< groupId > org.apache.maven.plugins </ groupId >
< artifactId > maven-compiler-plugin </ artifactId >
< configuration >
< source > 1.5 </ source >
< target > 1.5 </ target >
</ configuration >
</ plugin >
< plugin >
< groupId > org.mortbay.jetty </ groupId >
< artifactId > maven-jetty-plugin </ artifactId >
</ plugin >
</ plugins >
</ build >
< dependencies >
< dependency >
< groupId > junit </ groupId >
< artifactId > junit </ artifactId >
< version > 3.8.1 </ version >
< scope > test </ scope >
</ dependency >
< dependency >
< groupId > org.hibernate </ groupId >
< artifactId > hibernate </ artifactId >
< version > 3.1 </ version >
</ dependency >
< dependency >
< groupId > log4j </ groupId >
< artifactId > log4j </ artifactId >
< version > 1.2.11 </ version >
</ dependency >
< dependency >
< groupId > mysql </ groupId >
< artifactId > mysql-connector-java </ artifactId >
< version > 3.1.11 </ version >
< scope > runtime </ scope >
</ dependency >
< dependency >
< groupId > javax.servlet </ groupId >
< artifactId > servlet-api </ artifactId >
< version > 2.4 </ version >
< scope > provided </ scope >
</ dependency >
< dependency >
< groupId > javax.servlet </ groupId >
< artifactId > jstl </ artifactId >
< version > 1.1.2 </ version >
< scope > runtime </ scope >
</ dependency >
< dependency >
< groupId > taglibs </ groupId >
< artifactId > standard </ artifactId >
< version > 1.1.2 </ version >
< scope > runtime </ scope >
</ dependency >
< dependency >
< groupId > org.springframework </ groupId >
< artifactId > spring </ artifactId >
< version > 1.2.6 </ version >
</ dependency >
< dependency >
< groupId > dwr </ groupId >
< artifactId > dwr </ artifactId >
< version > 1.1.3 </ version >
</ dependency >
</ dependencies >
</ project >
代碼放入/main/java後,可以在項目目錄下執行:
mvn compile來做編譯嘗試,
也可以用mvn war直接生成打包文件,
當然最後可以用 mvn jetty:run來運行你的WEBAPP!
3. 在Eclipse中配置jetty進行調試
要把之前的項目導入Eclipse首先讓maven爲我們生成Eclipse工程文件,執行:
mvn eclipse:eclipse
再把M2_REPO加入到Eclipse的classpath中,有兩種方法,其中的b)方法是有效的:
a) mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
b) Window > Preferences. Select the Java > Build Path > Classpath Variables page
之後,就可以通過Eclipse的File->Import功能將工程導入。
有人爲了使用WEBAPP開發功能,而裝象MYECLIPSE這樣的巨物。有了JETTY,通過輕鬆配置就可以實現比TOMCAT更快更便捷的容器,所以在調試時強力推薦這個東東。下面就來看下如何配置。
先下配置一個外部工具,來運行JETTY:
選擇菜單Run->External Tools->External Tools ...在左邊選擇Program,再點New:
配置Location爲mvn完整命令行。
選擇Working Directory爲本項目。
Arguments填寫:jetty:run
org.mortbay.jetty:maven-jetty-plugin:run
再點選Enviroment頁:加入MAVEN_OPTS變量,值爲:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=y
其中,如果suspend=n 表示不調試,直接運行。
然後,點APPLY,再關閉本對話框。
另外注意一點,好像external tool菜單項在java browering的perspective下才會出現。如果在java下看不見,可以切換下試試。
下面新建運行配置:
點選run->debug...
選中左樹中的Remote Java Application,再點New。
選擇你的項目,關鍵是要填和之前設置外部工具時相同的端口號。
配置就完成了,正面開始調試運行:
首先要把JETTY運行起來(有點象TOMCAT裏的運行APPSERVER),從Run->External Tools中選擇之前配置的外部工具運行,這時LOG裏會顯示:
listening at port 4000字樣,
再選擇Run->Debug選擇我們剛建的運行配置,這時程序就RUN起來了,可以通過WEB進行訪問,設置斷點調試了。
============================================================================
適用於6.1.3以上,包括6.1.5的JETTY。
它主要是利用了JDK的代碼自動更換性能(code hot replace),可以不用重啓JETTY就調試、更換資源文件。注意:一定是DEBUG方式運行纔有這項功能。
所以應該說這篇文章的方法更好:
在Run->Debug中,New一個Java Application的配置,填入:
org.mortbay.xml.XmlConfiguration
參數填入一個自己的JETTY配置文件:
完成的myjetty.xml配置文件,請將其中的相應目錄修改成自己項目的目錄:
<! DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd" >
<!-- =============================================================== -->
<!-- Configure the Jetty Server -->
<!-- -->
<!-- Documentation of this file format can be found at: -->
<!-- http://docs.codehaus.org/display/JETTY/jetty.xml -->
<!-- -->
<!-- =============================================================== -->
< Configure id ="Server" class ="org.mortbay.jetty.Server" >
<!-- =========================================================== -->
<!-- Server Thread Pool -->
<!-- =========================================================== -->
< Set name ="ThreadPool" >
<!-- Default bounded blocking threadpool
-->
< New class ="org.mortbay.thread.BoundedThreadPool" >
< Set name ="minThreads" > 10 </ Set >
< Set name ="maxThreads" > 250 </ Set >
< Set name ="lowThreads" > 25 </ Set >
</ New >
<!-- Optional Java 5 bounded threadpool with job queue
<New class="org.mortbay.thread.concurrent.ThreadPool">
<Set name="corePoolSize">250</Set>
<Set name="maximumPoolSize">250</Set>
</New>
-->
</ Set >
<!-- =========================================================== -->
<!-- Set connectors -->
<!-- =========================================================== -->
<!-- One of each type! -->
<!-- =========================================================== -->
<!-- Use this connector for many frequently idle connections
and for threadless continuations.
-->
< Call name ="addConnector" >
< Arg >
< New class ="org.mortbay.jetty.nio.SelectChannelConnector" >
< Set name ="port" >< SystemProperty name ="jetty.port" default ="8080" /></ Set >
< Set name ="maxIdleTime" > 30000 </ Set >
< Set name ="Acceptors" > 2 </ Set >
< Set name ="statsOn" > false </