Maven的多子產品可以讓項目結構更明确,提高功能的内聚,降低項目的耦合度,真正的展現出分層這一概念。
我們在操作中,要明白為什麼這樣做,要了解到更深的層次,這樣,我們就不限于個别軟體了。
話不多說,直入主題:
如果對Maven還不夠熟悉,請看該部落格:Maven基礎
整個項目做完之後的結構是這樣的:

在開始之前我放出這兩張圖是maven多子產品項目做完後的目錄結構,以免大家被eclipse的結構迷惑了。
首先,建立一個maven項目,如圖:
選project,注意是Create a simple project,然後特别注意,root目錄的聚合子產品的packing是pom!!!
完後在該maven項目上建立子產品:
結構說明
到了這一步,我們已經将這些子產品建好了,接下來我要深入一下項目結構,以及多子產品的原理,然後我們再配置pom檔案。
這是一個項目大概運作機制,ssm-integration作為聚合父級項目,就是為了管理其他子項目(子產品),多子產品就是基于聚合父級項目的。子子產品service是對資料處理的子產品,在這裡是ssm架構,那麼它負責完成對資料庫的操作的封裝(大概結構,具體不細說),然後對外暴露一個接口供web子產品調用即可。也就是當web子產品引用了ssm-service.jar之後,供web項目調用的隻有你對外暴露的接口,這就真正實作分層。而web子產品,隻負責servlet、視圖模型、前端頁面和一些簡單的邏輯,需要時調用ssm-service.jar的接口方法就行了。當對項目進行部署時,隻需部署ssm-web即可,因為ssm-integration僅僅負責管理,你可以将它了解為承載着多子產品的大架構,而ssm-service已經打包給ssm-web了(ssm-web裡面已經有ssm-service了)。
pom.xml分析
在這之前,我聲明一下,聚合和繼承是可以分開的,隻是大家想友善一下,是以統稱為父項目。
我們開始看pom的配置,首先時父項目的pom.xml(這裡隻截取一部分,了解方法為重點):
父類pom.xml分析
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 <modelVersion>4.0.0</modelVersion>
5 <groupId>cn.zuoyu.ssm</groupId>
6 <artifactId>ssm-integration</artifactId>
7 <version>0.0.1-SNAPSHOT</version>
8 <packaging>pom</packaging>
9 <name>ssm-integration</name>
10 <description>spring&springMVC&mybatis的整合</description>
11
12 <modules>
13 <module>ssm-service</module>
14 <module>ssm-controller Maven Webapp</module>
15 </modules>
16
17 <properties>
18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19 <spring.version>4.3.8.RELEASE</spring.version>
20 </properties>
21
22 <build>
23 <pluginManagement>
24 <plugins>
25 <!-- 編譯插件 -->
26 <plugin>
27 <groupId>org.apache.maven.plugins</groupId>
28 <artifactId>maven-compiler-plugin</artifactId>
29 <version>2.3.2</version>
30 <configuration>
31 <source>1.8</source>
32 <target>1.8</target>
33 </configuration>
34 </plugin>
35
36 <!-- 用UTF-8編碼處理資源檔案 -->
37 <plugin>
38 <groupId>org.apache.maven.plugins</groupId>
39 <artifactId>maven-resources-plugin</artifactId>
40 <version>2.6</version>
41 <configuration>
42 <encoding>UTF-8</encoding>
43 </configuration>
44 </plugin>
45 </plugins>
46 </pluginManagement>
47 <!-- <resources>
48 <!--資源位置配置-->
49 <resource>
50 <directory>src/main/java</directory>
51 <includes>
52 <include>**/*.xml</include>
53 </includes>
54 </resource>
55 <!--指定資源的位置-->
56 <resource>
57 <directory>src/main/resources</directory>
58 </resource>
59 </resources> -->
60 </build>
61
62
63 <dependencyManagement>
64 <dependencies>
65 <!-- 單元測試 -->
66 <dependency>
67 <groupId>junit</groupId>
68 <artifactId>junit</artifactId>
69 <version>4.12</version>
70 <scope>test</scope>
71 </dependency>
72 <!-- spring核心包 -->
73 <dependency>
74 <groupId>org.springframework</groupId>
75 <artifactId>spring-core</artifactId>
76 <version>${spring.version}</version>
77 </dependency>
78
79 <!-- 掃描用包 -->
80 <dependency>
81 <groupId>org.springframework</groupId>
82 <artifactId>spring-context</artifactId>
83 <version>${spring.version}</version>
84 </dependency>
85
86 <!-- 緩存掃描用包 -->
87 <dependency>
88 <groupId>org.springframework</groupId>
89 <artifactId>spring-context-support</artifactId>
90 <version>${spring.version}</version>
91 </dependency>
92
93 <!-- bean支援 -->
94 <dependency>
95 <groupId>org.springframework</groupId>
96 <artifactId>spring-beans</artifactId>
97 <version>${spring.version}</version>
98 </dependency>
99 </dependencyManagement>
100 </project>
在這裡,<modules>xxx<modules>是将需要管理的子產品放進去,放進去的子產品歸該聚合項目管理。例如<pluginManagement>xxx</pluginManagement>或<dependencyManagement>xxx</dependencyManagement>是父類裡獨有的,為的是統一資源包,友善管理。
子類pom.xml分析
看一下子類的代碼,以web子產品為例
1 <?xml version="1.0"?>
2 <project
3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5 <modelVersion>4.0.0</modelVersion>
6 <parent>
7 <groupId>cn.zuoyu.ssm</groupId>
8 <artifactId>ssm-integration</artifactId>
9 <version>0.0.1-SNAPSHOT</version>
10 </parent>
11 <artifactId>ssm-controller</artifactId>
12 <packaging>war</packaging>
13 <name>ssm-controller</name>
14 <url>http://maven.apache.org</url>
15
16
17 <dependencies>
18
19 <dependency>
20 <groupId>junit</groupId>
21 <artifactId>junit</artifactId>
22 </dependency>
23 <!-- springMVC核心包 -->
24 <dependency>
25 <groupId>org.springframework</groupId>
26 <artifactId>spring-webmvc</artifactId>
27 </dependency>
28
29 <!-- jstl支援 -->
30 <dependency>
31 <groupId>jstl</groupId>
32 <artifactId>jstl</artifactId>
33 </dependency>
34
35 <dependency>
36 <groupId>org.apache.taglibs</groupId>
37 <artifactId>taglibs-standard-impl</artifactId>
38 </dependency>
39
40 <dependency>
41 <groupId>org.apache.taglibs</groupId>
42 <artifactId>taglibs-standard-spec</artifactId>
43 </dependency>
44
45 <!-- 對service的依賴 -->
46 <dependency>
47 <groupId>${project.groupId}</groupId>
48 <artifactId>ssm-service</artifactId>
49 <version>${project.version}</version>
50 </dependency>
51 </dependencies>
52 <build>
53 <plugins>
54 <!-- 編譯插件 -->
55 <plugin>
56 <groupId>org.apache.maven.plugins</groupId>
57 <artifactId>maven-compiler-plugin</artifactId>
58 </plugin>
59
60 <!-- 編碼設定 -->
61 <plugin>
62 <groupId>org.apache.maven.plugins</groupId>
63 <artifactId>maven-resources-plugin</artifactId>
64 </plugin>
65 </plugins>
66 <finalName>ssm-controller</finalName>
67 </build>
68 </project>
你會發現多了<parent></parent>标簽,這裡面寫的就是它要繼承的父項目。你還會發現項目沒有聲明groupId和version,因為它已經有父項目了,父項目已經管理了它的來源和版本号。你還會發現plugin插件和dependency依賴沒有聲明version,這就是父項目裡寫pluginManagement和dependencyManagement的優點,父項目裡的這個如果子項目沒有聲明則不會導入該依賴或插件,如果需要,隻需聲明groupId和artifactId即可,友善管理。另外,必須将該項目依賴的其他項目的pack引用過來,不然多子產品就失去了意義,根本運作不起來。
注意:
在maven多子產品項目裡,子項目的classpath共用的。
部署:
在IntelliJ IDEA裡部署MavenWeb項目和MyEclipse部署是有差異的,MyEclipse要很簡單:
就好了,隻部署ssm-web即可。
到了這一步,還差很關鍵的一部,就是在你運作之前,要保證你的maven倉庫有ssm-service,就是你service層的jar包,不然ssm-web根本找不到ssm-service.jar,是以,我們要這樣:
否則無法運作。
然後...
看一下整體結構:
就可以運作了...
若有不對的地方歡迎+感謝評論指出或郵件我 [email protected]
共同進步