最近學習在eclipse3.6環境下用maven2構建一個多模組的項目
分core/dal/dao/web四塊,當運行tomcat web工程調試需要把core/dal/dao的工程先mvn install一下,才能提交更新,反覆折騰以後總結出幾條很有用的訊息,希望和我一樣剛接觸maven的朋友不要再走錯路.在百度和google查了好幾天也沒有完整的資料,最後還是自己想到辦法了.
方法一:
tomcat
- 首先將maven web工程轉為eclipse wtp工程(網上搜一下),一般說來到maven工程目錄下執行命令列mvn eclipse:eclipse -Dwtpversion=2.0即可.
- 將工程添加到tomcat server中
- 添加其它工程的引用
- 因為tomcat外掛程式publish的內容是src/main裡的,所以web-inf/lib的外部包需要手動拷到外掛程式啟動並執行目錄,外掛程式運行目錄我的是在D:/EclipseWorkspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp/wtpwebapps/my-web
- 運行tomcat,在其它dao等工程做的修改,tomcat會自動reload了.
方法二.jetty
鑒於第一種方式需要手動拷jar的,我可受不了.於是最終選擇jetty外掛程式,就目前來看是用得最爽的.
1.在pom.xml加入jetty外掛程式資訊
<build><br /> <finalName>myweb</finalName><br /> <plugins><br /> <plugin><br /> <groupId>org.mortbay.jetty</groupId><br /> <artifactId>maven-jetty-plugin</artifactId><br /> <version>6.1.12</version><br /> <configuration><br /> <!-- 熱載入掃描間隔秒數 --><br /> <scanIntervalSeconds>5</scanIntervalSeconds><br /> <stopKey>/</stopKey><br /> <stopPort>9999</stopPort><br /> <!-- 額外掃描的class路徑 --><br /> <scanTargets><br /><scanTarget>D:/EclipseWorkspace/myweb/myweb-dal/target/classes</scanTarget><br /><scanTarget>D:/EclipseWorkspace/myweb/myweb-core/target/classes</scanTarget><br /></scanTargets><br /> <webAppConfig><br /> <contextPath>/</contextPath><br /><!-- jetty設定檔 --><br /> <defaultsDescriptor>src/main/resources/webdefault.xml</defaultsDescriptor><br /> <!-- 引用其它工程的class --><br /> <extraClasspath>D:/EclipseWorkspace/myweb/myweb-core/target/classes;D:/EclipseWorkspace/myweb/myweb-dal/target/classes</extraClasspath></p><p> </webAppConfig><br /> </configuration><br /></plugin><br /> </plugins><br /> </build>
2.配置eclipse run configurations
在goals中填寫jetty:run -e -q
run as -> Maven build就可以運行web工程以及布署和調試,最大的好處,修改無需重啟jetty.