在前段时间的许多项目开发中,特别是业务模块较为复杂的案例,都采用maven构造模块化开发,这样项目结构很清晰,更便于代码的维护和功能的扩展,其实不管是中型项目或者是超大型的项目,无非是将各个功能业务划分为不同的模块,基于maven的项目对象依赖,将各个模块紧密联合在一起,随着业务逻辑的增加,这两年微服务架构的开发更是得到行业的追捧
就以最近的一个项目为例子:
将整个项目按照功能需求在工程目录下划分为: 统一的依赖管理(dependencies)、 通用的工具类(commons)、领域模型(domain)、管理后台(admin)、商城前端(ui)和接口(api)共六个模块
其中,dependencies、commons、domain、admin、ui、和api这些都是整个工程的子模块
<groupId>com.funtl</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<modules>
<module>my-shop-commons</module>
<module>my-shop-dependencies</module>
<module>my-shop-domain</module>
<module>my-shop-external</module>
<module>my-shop-web-admin</module>
<module>my-shop-web-api</module>
<module>my-shop-web-ui</module>
</modules>
<packaging>pom</packaging>
而模块和模块之前也存在着依赖继承的关系
比如,commons、domain、admin、ui、和api的父模块都是dependencies
<parent>
<groupId>com.funtl</groupId>
<artifactId>my-shop-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../my-shop-dependencies/pom.xml</relativePath>
</parent>
显然,domain模块是存放项目实体类的,而Commons模块是存放着项目的通用方法和工具类的
其中,domain模块继承了commons模块
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-commons</artifactId>
<version>${project.parent.version}</version>
</dependency>
admin模块同时继承了Commons模块和domain模块
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-domain</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.funtl</groupId>
<artifactId>my-shop-commons</artifactId>
<version>${project.parent.version}</version>
</dependency>
等等
这样,整个项目的结构关系非常的清晰,这对于项目开发,特别是多人协作的项目开发非常有利,而且也便于日后的维护和扩展