Maven多模組項目 eclipse熱部署 Maven項目實現 tomcat熱部署

來源:互聯網
上載者:User

Maven 多模組項目在eclipse下面熱部署,即你可以體驗下無論你修改整個項目裡面的任何模組的代碼,都不需要用maven打包就可以看到效果,

1、首先準備好建立一個maven多項目的代碼,準備好一個eclipse,一個tomcat,什麼java環境,tomcat環境我就不多說了,如果這個你都沒有配好就別往下看了,

2、建立項目,mvn archetype:generate -DgroupId=com.laoshuisheng -DartifactId=test -Dversion=1.0

建立成功後到pom.xml檔案裡面修改pom.xml

 

project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.laoshuisheng</groupId> <artifactId>test</artifactId> <version>1.0</version> <packaging>jar</packaging> <name>test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>

 

修改成下面

 

oject xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.laoshuisheng</groupId> <artifactId>test</artifactId> <version>1.0</version> <packaging>pom</packaging> <name>test</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project>

 

進入該項目裡面隨便的建立幾個模組,我這裡為了方便就只建立兩個模組好了,一個就是web項目,一個就是業務代碼

 

.../test>mvn archetype:generate -DgroupId=com.laoshuisheng.web -DartifactId=test-web -DarchetypeArti
factId=maven-archetype-webapp -Dversion=1.0

 

建立業務代碼項目:

 

../test>mvn archetype:generate -DgroupId=com.laoshuisheng.web -DartifactId=test-core -DarchetypeArt
ifactId=maven-archetype-quickstart -Dversion=1.0

 

建立完了如下圖:

 

 

 

好了現在項目建立成功了,我們要匯入eclipse了,這一步也很重要,大家要細心的看了,匯入的命令是:

 

...test>mvn eclipse:eclipse -Dwtpversion=1.0

 

記得不要缺少 -Dwtpversion=1.0,這個說明了eclipse認出它就是web項目,而不是簡單的java項目,大家應該知道web項目在eclipse可以做一些其他的事情吧。

 

 

 

 

記住了,是一般的匯入,不是maven項目匯入。

 

 

 

看見了吧,項目上面有個小地球,證明eclipse認出了他就是java web項目,沒錯,就是這個會給我們意想不到的效果

接下來我們去把項目依賴加上,在web項目想的pom.xml裡面增加對test-core的依賴:

 

 

<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>test</artifactId> <groupId>com.laoshuisheng</groupId> <version>1.0</version> </parent> <groupId>com.laoshuisheng.web</groupId> <artifactId>test-web</artifactId> <version>1.0</version> <packaging>war</packaging> <name>test-web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.laoshuisheng.web</groupId> <artifactId>test-core</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>test-web</finalName> </build> </project>

 

加上後重新 mvn eclipse:clean eclipse:eclipse下重新整理下eclipse的項目發現:

 

 

 

 

這個時候就該配置到伺服器上了,因為雖然說是maven項目,但是我們開發的時候伺服器啟動並執行是web下面的classes下的class檔案和

webapp下面的jsp/css/js等檔案,所以並不需要打包什麼的,如果是依賴如test-core也是跑classes的編譯後的代碼,所以也不需要編譯,但是這裡有個問題就是設定檔必須是放在classpath下面,因為我剛剛已經說了原因了,,廢話不多說,看是配伺服器,

 

 

 

 

 

 

 這就是我們建立好的tomcat伺服器,然後大家點下   右鍵 ,記得是在伺服器上點右鍵哦, 彈出個對話方塊,不錯是彈出對話方塊,上面有個add and remove 的選項,你會發現有個test-web

 

 

 

 

看到了吧,知道為啥我一定要強調匯入項目的時候加上 -Dwtpversion=1.0了吧,這樣他才能在這裡加,eclipse才認出它就是web項目,哈哈,到這裡基本上已經成功了,看看加後的效果:

 

 

 

 

不錯~不錯,看到這樣說明連帶依賴的模組也成功載入了,嗯,現在你可以隨意的修改這兩塊裡面的代碼,都不需要重新打包、不需要重新啟動伺服器,因為我們是運用了eclipse的熱編譯來實現的,或者說我們根本不需要在開發中運用到maven的功能。

 

 

好了。到這裡maven的多模組的項目的熱編譯就完全可以無任何外掛程式的實現了,如果大家有什麼不明白的直接來問我,謝謝。

 

 

轉摘記得寫出處哦。。。。


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.