這篇文章將為大家詳細講解有關IDEA多module項目maven依賴的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
不管eclipse有沒有被被時代拋棄,反正是被我拋棄了,因為IDEA是真的好用
現在公司的項目基本都是基于maven的多module項目,controller,service,model,dao等都被分成了不同的module,這樣做當然也是為了解耦。
這些module可根據需要在pom.xml配置來打成war包或者jar包
<packaging>jar</packaging>
web主項目設置packaging級別為war,dao、model這些module設置packaging級別為jar。
module之間可以通過module自己的pom.xml來進行相互引用或依賴,如:
<dependency> <groupId>cn.com.autohome.mall</groupId> <artifactId>mall-common</artifactId> </dependency> <dependency> <groupId>cn.com.autohome.mall</groupId> <artifactId>mall-api-model</artifactId> </dependency>
這樣在 File -> project structure 下,選中主web項目
從上面的截圖可以看出來依賴的第三方jar和依賴項目子module的區別。
maven在執行install,packaging是jar的會被打成jar放在target目錄下,packaging是war的會被打成war放在target目錄下。
另外兩個target目錄會有一點區別,war的target目錄會多出來一個和module同名的文件夾,這個文件夾和war解壓后完全一致。
所有依賴的jar(包括依賴的module,被打成jar)都會被放lib下
這樣在部署的時候,只需要部署相應的war即可。
關于Maven pom.xml中的元素modules、parent、properties以及import
多個module不需要分別執行mvn命令,可以使用聚合(aggregator)來一次構建全部模塊
modules
在父pom.xml中通過
<modules> <!-- 模塊都寫在此處 --> <module>mall-common</module> <module>mall-api-model</module> </modules>
來引用所有需要構建的子模塊
parent
繼承,和java中的繼承相當,作用就是復用
場景
若每個子模塊都都用的了spring,那么我們是不是每個子模塊都需要單獨配置spring依賴了?這么做是可以的,但是我們有更優的做法,那就是繼承,用parent來實現。
實現
父(account-aggregator)pom.xml
<modules> <!-- 模塊都寫在此處 --> <module>mall-common</module> <module>mall-api-model</module> </modules> <dependencies> <!-- 配置共有依賴 --> <!-- spring 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.2.RELEASE</version> </dependency> ······ <!-- junit 依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> </dependencies>
子pom.xml
<parent> <groupId>xx.xx.xx</groupId> <artifactId>aggregator</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../pom.xml</relativePath> <!-- 與不配置一樣,默認就是尋找上級目錄下得pom.xml --> </parent> <dependencies> <!-- 配置自己獨有依賴 --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.3</version> </dependency> <dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>1.4.1</version> <scope>test</scope> </dependency> </dependencies>
依賴管理
繼承可以消除重復,那是不是就沒有問題了? 答案是存在問題,假設將來需要添加一個新的子模塊util,只是提供一些簡單的幫助工具,不需要依賴spring、junit,那么繼承后就依賴上了,有沒有什么辦法了?
有,maven已經替我們想到了,那就是dependencyManagement元素,既能讓子模塊繼承到父模塊的依賴配置,又能保證子模塊依賴使用的靈活性。在dependencyManagement元素下得依賴聲明不會引入實際的依賴,不過它能夠約束dependencies下的依賴使用。
在父pom.xml中配置dependencyManagement元素
<modules> <!-- 模塊都寫在此處 --> <module>mall-common</module> <module>mall-api-model</module> </modules> <dependencyManagement> <dependencies> <!-- 配置共有依賴 --> <!-- spring 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.0.2.RELEASE</version> </dependency> ······ </dependencies> </dependencyManagement>
子pom.xml
<dependencies> <!-- spring 依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- junit 依賴 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.0.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.16</version> </dependency> </dependencies>
使用這種依賴管理機制似乎不能減少太多的POM配置,就少了version(junit還少了個scope),感覺沒啥作用呀;其實作用還是挺大的,父POM使用dependencyManagement能夠統一項目范圍中依賴的版本,當依賴版本在父POM中聲明后,子模塊在使用依賴的時候就無須聲明版本,也就不會發生多個子模塊使用版本不一致的情況,幫助降低依賴沖突的幾率。如果子模塊不聲明依賴的使用,即使該依賴在父POM中的dependencyManagement中聲明了,也不會產生任何效果。
關于“IDEA多module項目maven依賴的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。