在Eclipse中建立Maven多模組工程
在平時的Javaweb專案開發中為了便於後期的維護,我們一般會進行分層開發,最常見的就是分為domain(域模型層)、dao(資料庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層之後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天我們就是使用Maven來構建分模組專案。
在這個分模組專案中主要分為兩個模組,Service模組和web模組,Service模組中包含dao,下面是專案結構
Complaints-parent
|----pom.xml(pom)
|----Complaints-web
|----pom.xml (war)
|----Complaints-service
|----pom.xml(jar)
Complaints-parent是父工程,同時承擔聚合模組和父模組的作用,沒有實際程式碼和資原始檔;
Complaints-web是web工程,最終形成最終的war包;
Complaints-service是業務模組的邏輯部分,包含了資料庫訪問層和業務邏輯層,但是不包括web相關的部分;
上述簡單示意圖中,有一個父專案(Complaints-parent)聚合很多子專案(Complaints-web,Complaints-service)。每個專案,不管是父子,都含有一個pom.xml檔案。而且要注意的是,小括號中標出了每個專案的打包型別。父專案是pom,也只能是pom。子專案有jar,或者war。
依賴關係是:Complaints-web需要依賴Complaints-service
接下來就是在Eclipse中搭建專案了:
1.父專案(Complaints-parent)
在Eclipse裡面New
-> Maven Project
;
在彈出介面中選擇“Create
a simple project”
父專案建好之後,目錄下面只有一個src和pom.xml檔案
將src資料夾刪除,然後修改pom.xml檔案,將<packaging>jar</packaging>修改為<packaging>pom</packaging>,
pom表示它是一個被繼承的模組。(如果再建立專案時已經選為了pom就不需要修改)
以下是父專案的pom.xml的內容
<?xml version="1.0" encoding="UTF-8"?>
<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.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Complaints-parent</name>
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.建立Complaints-service專案
選中剛建的父專案,在彈出選單中點選
New -> Maven Module
;
使用預設的Archetype(預設:GroupId:org.apache.maven.archetypes,Artifact
Id:maven-archetype-quickstart)
這樣一個子專案就建立完成了,在檔案系統中,子專案會建在父專案的目錄中。
細心一點會發現,此時父專案的pom.xml檔案中就多了一句
<modules>
<module>Complaints-service</module>
</modules>
3.建立web工程Complaints-web
選中剛建的父專案,在彈出選單中點選
New -> Maven Module
;
建立完之後就會發現父專案的pom.xml檔案中又自動多了一句
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules>
4.優化配置
Complaints-web是依賴service專案的
需要在web的pom.xml中新增依賴配置
<dependency>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-service</artifactId>
<version>${project.version}</version>
</dependency>
按上面步驟建立的子專案,在pom.xml
中有個parent
節點,所以,他可以繼承父專案的相關資訊。沒錯,父子專案中存在繼承關係。
在子專案的pom.xml
中,子專案的groupId
和version
一般和父專案相同,那麼可以把子專案的這兩個引數刪除,這樣會自動繼承父專案的取值。
同樣,如果其他的一些屬性,所有子專案都是一樣的,那麼可以上移到父專案中設定,子專案中無需重複設定。比如:<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>可以僅在父專案中設定一次。
Manen提供dependencyManagement
和pluginManagement
兩個標籤。使用這兩個標籤,可以在父專案中統一管理依賴和外掛的配置引數,比如版本號啥的。而在子專案中,僅需列出需要使用的依賴和外掛的groupId
和artifactId
就可以了,其他資訊會自動從父專案管理的資訊裡面獲取。在父專案中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies></dependencyManagement>
在子專案中
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
由於web專案依賴service專案,子專案中只需service中新增配置,web專案會自動載入過去的
最後是打包配置:
打包時需要把service專案中的xml檔案和別的property檔案也打進去,就得在pom檔案中說明
<!-- 打包時把需要的xml檔案一塊打進去 -->
<build>
<finalName>Complaints-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
一下是三個專案的pom.xml中的全部內容
Complaints-parent
<?xml version="1.0" encoding="UTF-8"?>
<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.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Complaints-parent</name>
<modules>
<module>Complaints-service</module>
<module>Complaints-web</module>
</modules>
<dependencyManagement>
<dependencies>
<span style="white-space:pre"> </span><dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Complaints-service:
<?xml version="1.0"?>
<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>
<parent>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Complaints-service</artifactId>
<name>Complaints-service</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 打包時把需要的xml檔案一塊打進去 -->
<build>
<finalName>Complaints-service</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
<!-- 依賴配置 -->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!-- spring beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
</project>
Complaints-web
<?xml version="1.0"?>
<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>
<parent>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Complaints-web</artifactId>
<packaging>war</packaging>
<name>Complaints-web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>com.hrtel</groupId>
<artifactId>Complaints-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<finalName>Complaints-web</finalName>
</build>
</project>