一直对此问题好奇,正好有这兴致和时间,有必要了解一下。
所谓聚合项目,实际上就是对项目分模块。
互联网项目一般来说按照业务分(订单模块、VIP模块、支付模块、CMS模块…);
传统的软件项目,大多采用分层的方式(Dao、Serivce、Controller):
1 聚合工程:实践案例篇
[demo]#1 多模块结构
bobo-parent pom 管理 (父聚合)
|-- bobo-manager pom 管理 (子聚合)
|-- bobo-manager-dao jar
|-- bobo-manager-service jar
|-- bobo-manager-model jar
|-- bobo-manager-commons jar
|-- bobo-manager-controller war (含controller/webapp)
1-1 项目结构
[demo]#2
现在项目越来越复杂,一般情况下,一个项目包括多个模块。
假设有个 orm 项目,包括2个子模块:orm-server和orm-support。
它们在文件系统的目录结构,如下所示
orm
--pom.xml
--orm -support
--src
--pom.xml
--orm -server
--src
--pom.xml
那么如何把它们聚合成一个多模块的maven项目配置呢?
1-2 项目的聚合: orm/pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>orm</artifactId>
<version>0.1</version>
<packaging>pom</packaging>
<name>orm</name>
<description>The orm project of example</description>
<url>http://example.com</url>
<modules>
<module>orm-server</module>
<module>orm-support</module>
</modules>
这个聚合工程的pom.xml
的配置关键在两点:
- (1)packaging类型为pom;
- (2)
模块的引入。
<module>orchid-server</module>
表示在当前目录(也就是上面pom.xml所在目录)包括一个文件夹orchid-server。如果orchid-server和orchid在同一目录的话,那么配置应该是
<module>../orm-server</module>
1-3 项目的继承: orchid-server|orchid-support/pom.xml
在上面的例子中只有聚合,也就是说:
orm-support
和orm-server
是不知道有个总项目或者说父项目存在的,
因为我们没有在orm-support/pom.xml和orm-server/pom.xml中加入任何东西。
那么orm-support和orm-server也是不能从orchid项目继承任何东西的。
如果需要继承的话,那么需要在子项目的配置中加入<parent>
配置:
- orm-support
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>orm</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>orm-support</artifactId>
- orm-server
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>orm</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>orm-server</artifactId>
上面两个配置在<parent>
中都忽略了<relativePath>
标签,<relativePath>
的默认值是../pom.xml
,也就是从父目录中寻找pom.xml
。
如果你的父项目在其他地方,那么要手工加入改配置
<parent>
<groupId>com.stear.orchid</groupId>
<artifactId>orchid</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>parent project orchid directory/pom.xml</relativePath>
</parent>
这样子项目可以从父项目中继承元素。可继承的元素包括:
groupId
version
description
organization
inceptionYear
url
developers
contributors
distributionManagement
issueManagement
ciManagement
scm
mailingLists
properties
dependencies
dependencyManagement
repositories
build
reporting
2 聚合工程:理论篇
2-1 聚合配置
- 聚合工程(父工程)
[平行结构]
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules> <!-- 本项目要聚合的三个子项目(模块) -->
<module>../a-subproject/module<module>
<module>../b-subproject/module<module>
<module>../c-subproject/module<module>
</modules>
[父子结构]
<groupId>com.example</groupId>
<artifactId>wechat</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules> <!-- 本项目要聚合的三个子项目(模块) -->
<module>wechat-pojo<module>
<module>wechat-web-manager<module>
<module>wechat-web-backend<module>
</modules>
- 被聚合工程(子工程)
<parent>
<groupId>com.example</groupId>
<artifactId>wechat</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<packaging>war(或jar)</packaging>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>wechat-pojo</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
[补充]web(controller)项目中,一般还可添加如下配置:
<dependencies>
<!-- 因为是web项目所以需要servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 端口号 -->
<port>8082</port>
<!-- /表示访问路径 省略项目名 -->
<path>/</path>
<!-- 设置编码方式 -->
<uriEncoding>utf-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
2-2 Why
2-3 What
2-4 打包方式
2-5 依赖管理/依赖传递
2-6 统一插件管理
3 聚合工程:延申问题?
3-1 Maven聚合工程的优势在哪里?
- 方便统一管理(依赖等)
- 更快构建,更快开发
- 项目整合:纵向切分+横向切分
首先,纵向切分,使项目结构更加清楚:将一个项目的多个功能模块分开;
然后,再对每个模块进行横向切分:
就是我们平常说的3层架构,将项目分成了web层(也被叫做表现层),service层(也被叫做业务层)、dao层(也被叫做持久层),可以理解为将一个功能模块的不同调用过程进行了水平方向的拆分。
最后,项目整合:横向拆分后,每个功能模块进行了单独的开发之,项目整合的时候只需要有一个能够整合这些项目或者模块的工程即可