Maven學(xué)習(xí)教程之搭建多模塊企業(yè)級項目
首先,前面幾次學(xué)習(xí)已經(jīng)學(xué)會了安裝maven,如何創(chuàng)建maven項目等,最近的學(xué)習(xí),終于有點進展了,搭建一下企業(yè)級多模塊項目。
好了,廢話不多說,具體如下:
首先新建一個maven項目,pom.xml的文件如下:

搭建多模塊項目,必須要有一個packaging為pom的根目錄。創(chuàng)建好這個maven項目后,我們對著項目右鍵-->new

輸入你的項目名稱

這里就不重復(fù)說創(chuàng)建項目了,創(chuàng)建好的目錄結(jié)構(gòu)在eclipse中如下:

說明一下這些項目具體都是干嘛的:
easyframework-model:數(shù)據(jù)模型,與數(shù)據(jù)庫表字段對應(yīng)的實體類
easyframework-core:核心業(yè)務(wù)項目。主要是Service處理業(yè)務(wù)邏輯
easyframework-persist:數(shù)據(jù)持久層,操作低層數(shù)據(jù)庫。
easyframework-utils:工具類,所有工具類都提取出來寫在這個項目中。
easyframework-web :這個就是整個項目的web層了,頁面的顯示以及控制層
備注:創(chuàng)建這些項目的時候,只有easyframework-web是web項目即maven的:maven-archetype-webapp,其他的都是java項目:maven-archetype-quicktart
打開easyframework-root的pom.xml文件,你會看到模塊化是這樣的:

接下來是配置各個模塊的依賴關(guān)系,我個人認為的項目是這樣依賴的,不知道對不對,呵呵....

舉個例子easyframework-web這個項目依賴easyframework-core(業(yè)務(wù)核心)和easyframework-model(實體類),easyframework-utils(公共的工具類)這個三個模塊。
那么在怎么在easyframework-web的pom.xml中體現(xiàn)呢,具體如下:

打開項目的maven依賴你會發(fā)現(xiàn),已經(jīng)依賴了這三個項目

但是你應(yīng)該會感覺到奇怪,為什么會有那么jar包,明明只引用了這三個項目,哪來的那么多jar包。
你會發(fā)現(xiàn),我再pom.xml文件中,有個parent節(jié)點,繼承了根節(jié)點的pom,這就是maven的項目繼承依賴,會從父POM中繼承一些值。這對構(gòu)建一個大型的系統(tǒng)來說很有必要
這樣的話你就不需要一遍又一遍的重復(fù)添加同樣的依賴元素,當(dāng)然,如果你在子項目中也有同樣的依賴,則會覆蓋父POM中的值。
父POM的的依賴如下:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.easyframework</groupId>
<artifactId>easyframework-root</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>easyframework-root</name>
<url>http://maven.apache.org</url>
<modules>
<module>easyframework-web</module>
<module>easyframework-persist</module>
<module>easyframework-core</module>
<module>easyframework-utils</module>
<module>easyframework-model</module>
</modules>
<properties>
<!--指定Maven用什么編碼來讀取源碼及文檔 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--指定Maven用什么編碼來呈現(xiàn)站點的HTML文件 -->
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mysql.version>5.1.25</mysql.version>
<hibernate.version>4.2.2.Final</hibernate.version>
<spring.version>3.2.3.RELEASE</spring.version>
<aspectj.version>1.7.2</aspectj.version>
</properties>
<repositories>
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
</repository>
</repositories>
<dependencies>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- mysql數(shù)據(jù)庫驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- hibernate4 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- spring3 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<finalName>easyframework-root</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
當(dāng)然這個父POM只是一個例子,你可以根據(jù)自己的配置添加相關(guān)的依賴,這里給一個我認為是最好用的倉庫:
http://mvnrepository.com/相信地球人都知道這個!哈哈.....
到此就搭建好了企業(yè)級多模塊的項目環(huán)境了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot延遲執(zhí)行實現(xiàn)方法
本文介紹了在Spring Boot項目中延遲執(zhí)行方法的實現(xiàn),以及延遲執(zhí)行下聲明式事務(wù)和編程式事務(wù)的使用情況,感興趣的朋友一起看看吧2020-12-12
spring session同域下單點登錄實現(xiàn)解析
這篇文章主要介紹了spring session同域下單點登錄實現(xiàn)解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
Springboot日期轉(zhuǎn)換器實現(xiàn)代碼及示例
這篇文章主要介紹了Springboot日期轉(zhuǎn)換器實現(xiàn)代碼及示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
Java多線程編程中使用Condition類操作鎖的方法詳解
Condition是java.util.concurrent.locks包下的類,提供了對線程鎖的更精細的控制方法,下面我們就來看一下Java多線程編程中使用Condition類操作鎖的方法詳解2016-07-07

