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
再點選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</Set>
<Set name="confidentialPort">8443</Set>
<Set name="lowResourcesConnections">5000</Set>
<Set name="lowResourcesMaxIdleTime">5000</Set>
</New>
</Arg>
</Call>
<!-- Use this connector if NIO is not available.
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.bio.SocketConnector">
<Set name="port">8081</Set>
<Set name="maxIdleTime">50000</Set>
<Set name="lowResourceMaxIdleTime">1500</Set>
</New>
</Arg>
</Call>
-->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- To add a HTTPS SSL listener -->
<!-- see jetty-ssl.xml to add an ssl connector. use -->
<!-- java -jar start.jar etc/jetty.xml etc/jetty-ssl.xml -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- =========================================================== -->
<!-- Set up global session ID manager -->
<!-- =========================================================== -->
<!--
<Set name="sessionIdManager">
<New class="org.mortbay.jetty.servlet.HashSessionIdManager">
<Set name="workerName">node1</Set>
</New>
</Set>
-->
<!-- =========================================================== -->
<!-- Set handler Collection Structure -->
<!-- =========================================================== -->
<Set name="handler">
<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<Item>
<New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
</Item>
<Item>
<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<Set name="handler">
<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<!--Item>
<New id="RequestLog" class="org.mortbay.jetty.handler.RequestLogHandler"/>
</Item-->
<Item>
<New class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/ebnms</Set>
<Set name="resourceBase">E:/Prj2/ForMe/Src/flower/src/main/webapp</Set>
<Call name="addServlet">
<Arg>org.mortbay.jetty.servlet.DefaultServlet</Arg>
<Arg>/</Arg>
</Call>
</New>
</Item>
</Array>
</Set>
</New>
</Set>
<!-- =========================================================== -->
<!-- Configure Authentication Realms -->
<!-- Realms may be configured for the entire server here, or -->
<!-- they can be configured for a specific web app in a context -->
<!-- configuration (see $(jetty.home)/contexts/test.xml for an -->
<!-- example). -->
<!-- =========================================================== -->
<Set name="UserRealms">
<Array type="org.mortbay.jetty.security.UserRealm">
<!--
<Item>
<New class="org.mortbay.jetty.security.HashUserRealm">
<Set name="name">Test Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
</New>
</Item>
-->
</Array>
</Set>
<!-- =========================================================== -->
<!-- Configure Request Log -->
<!-- Request logs may be configured for the entire server here, -->
<!-- or they can be configured for a specific web app in a -->
<!-- contexts configuration (see $(jetty.home)/contexts/test.xml -->
<!-- for an example). -->
<!-- =========================================================== -->
<!--Ref id="RequestLog">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.mortbay.jetty.NCSARequestLog">
<Set name="filename"><SystemProperty name="jetty.logs" default="./logs"/>/yyyy_mm_dd.request.log</Set>
<Set name="filenameDateFormat">yyyy_MM_dd</Set>
<Set name="retainDays">90</Set>
<Set name="append">true</Set>
<Set name="extended">true</Set>
<Set name="logCookies">false</Set>
<Set name="LogTimeZone">GMT</Set>
</New>
</Set>
</Ref-->
<!-- =========================================================== -->
<!-- extra options -->
<!-- =========================================================== -->
<Set name="stopAtShutdown">true</Set>
<Set name="sendServerVersion">true</Set>
<!--Set name="sendDateHeader">true</Set-->
<!--Set name="gracefulShutdown">1000</Set-->
</Configure>
- Maven
1. 創建普通工程:mvn archetype:create -DgroupId=com.example -DartifactId=example
2. 創建Web工程:mvn archetype:create -DgroupId=com.example -DartifactId=example -DarchetypeArtifactId=maven-archetype-webapp - eclipse
1. 轉爲eclipse工程:mvn eclipse:eclipse
2. 添加repository路徑變量m2_repo:mvn eclipse:add-maven-repo -Declipse.workspace=your eclipse workspace path - Jetty
1. 修改POM.xml文件,指定工程使用Jetty及Java5xml 代碼- <build>
- <finalName>examplefinalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.pluginsgroupId>
- <artifactId>maven-compiler-pluginartifactId>
- <configuration>
- <source>1.5source>
- <target>1.5target>
- configuration>
- plugin>
- <plugin>
- <groupId>org.mortbay.jettygroupId>
- <artifactId>maven-jetty-pluginartifactId>
- plugin>
- plugins>
- build>
2. 工程調試:添加外部工具
Step 1
Go to the Run/External Tools/External Tools ..." menu item on the "Run" menu bar. Select "Program" and click the "New" button. On the "Main" tab, fill in the "Location:" as the full path to your "mvn" executable. For the "Working Directory:" select the workspace that matches your webapp. For "Arguments:" add jetty:run.Move to the "Environment" tab and click the "New" button to add a new variable named MAVEN_OPTS with the value:
-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n
If you supply suspend=n instead of suspend=y you can start immediately without running the debugger and launch the debugger at anytime you really wish to debug.
Step 2
Then, pull up the "Run/Debug/Debug ..." menu item and select "Remote Java Application" and click the "New" button. Fill in the dialog by selecting your webapp project for the "Project:" field, and ensure you are using the same port number as you specified in the address= property above.Now all you need to do is to Run/External Tools and select the name of the maven tool setup you created in step 1 to start the plugin and then Run/Debug and select the name of the debug setup you setup in step2.
3. js、css等靜態文件無法更新的問題(save could not be completed)
原文出處
Jetty啓動後,如果修改javascript文件,將不能保存,使調試很麻煩。這是因爲使用了CACHE,JETTY說是WINDOWS下的一個限制。可以通過如下方法修正:
解壓出jetty.jar中的org/mortbay/jetty/webapp/webdefault.xml文件,將這一選項由true改爲false,另存到src/main/resources目錄,或者其它自選目錄。xml 代碼<!-- change to false -->
- <init-param> <param-name>useFileMappedBufferparam-name>
- <param-value>trueparam-value> <!-- change to false --> init-param>
在pom.xml中加入對這個文件的指向:
xml 代碼- <plugin>
- <groupId>org.mortbay.jettygroupId>
- <artifactId>maven-jetty-pluginartifactId>
- <configuration>
- <webDefaultXml>src/main/resources/webdefault.xmlwebDefaultXml>
- >
- >
-
其他參考
1. 圖文並茂的介紹Maven2+Jetty -
經常查詢的地方
1.文件結構src/main/java Application/Library sources src/main/resources Application/Library resources src/main/filters Resource filter files src/main/assembly Assembly descriptors src/main/config Configuration files src/main/webapp Web application sources src/test/java Test sources src/test/resources Test resources src/test/filters Test resource filter files src/site Site LICENSE.txt Project's license README.txt Project's readme -
附件爲運行jetty的批處理文件
1. 使用時將批處理文件中的PROJECT_DIR、CURRENT_DISK設置爲實際的工程所有目錄及盤符