maven 项目 多模块拆分

转自 :http://blog.csdn.net/liupin_2011/article/details/40017837


1.  多模块拆分的必要性

使用Java技术开发的工程项目,无论是数据处理系统还是Web网站,随着项目的不断发展,需求的不断细化与添加,工程项目中的代码越来越多,包结构也越来越复杂这时候工程的进展就会遇到各种问题:

(1)不同方面的代码之间相互耦合,这时候一系统出现问题很难定位到问题的出现原因,即使定位到问题也很难修正问题,可能在修正问题的时候引入更多的问题。

(2)多方面的代码集中在一个整体结构中,新入的开发者很难对整体项目有直观的感受,增加了新手介入开发的成本,需要有一个熟悉整个项目的开发者维护整个项目的结构(通常在项目较大且开发时间较长时这是很难做到的)。

(3)开发者对自己或者他人负责的代码边界很模糊,这是复杂项目中最容易遇到的,导致的结果就是开发者很容易修改了他人负责的代码且代码负责人还不知道,责任追踪很麻烦。

        将一个复杂项目拆分成多个模块是解决上述问题的一个重要方法,多模块的划分可以降低代码之间的耦合性(从类级别的耦合提升到jar包级别的耦合),每个模块都可以是自解释的(通过模块名或者模块文档),模块还规范了代码边界的划分,开发者很容易通过模块确定自己所负责的内容。


2. 使用maven构建模型进行多模块拆分

maven的多模块划分还是比较直观的,每个模块都是一个独立的maven项目,模块之间的相互引用和maven中对jar包依赖的解决是一致的,这使得物理(jar)和逻辑(dependency)上得以保持一致。我们先来看一个完整的多模块项目的组成(使用Maven The Definitive Guide上的例子):

(1)首先是整体的模块关系

引用书上对这些模块的介绍:

simple-model

该模块定义了一个简单的对象模型,对从Yashoo! Weather信息源返回的数据建模。该对象模型 包含了Weather,Condition,Atmosphere,Location,和Wind对象。当我们的应用程序解析Yahoo! Weather信息源的时候,simple-weather中定义的解析器会解析XML并创建供应用程序使用的Weather 对象。该项目还包含了使用hibernate 3标注符标注的模型对象,它们在simple-persist的逻辑中被用来 映射每个模型对象至关系数据库中对应的表。

simple-weather

该模块包含了所有用来从Yahoo! Weather数据源获取数据并解析结果XML的逻辑。从数据源返回的XML被转换成simple-model中定义的模型对象。simple-weather有一个对simple-model的依赖。simple-weather定义了一个WeatherService对象,该对象会被simple-command和simple-webapp项目引用。

simple-persist

该模块包含了一些数据访问对象(DAO),这些对象将Weather对象存储在一个内嵌数据库中。这 个多模块项目中的两个应用都会使用simple-persist中定义的DAO来将数据存储至内嵌数据库中。本项 目中定义的DAO能理解并返回simplemodel定义的模型对象。simple-persist有一个对simple-model的依 赖,它也依赖于模型对象上的Hibernate标注。

simple-webapp

这个web应用项目包含了两个spring MVC控制器实现,控制器使用了simpleweather中定义的WeatherService,以及simple-persist中定义的DAO。simplewebapp有对于simple-weather和simple-persist的直接依赖;还有一个对于simple-model的传递性依赖。

simple-command

该模块包含了一个用来查询Yahoo! Weather信息源的简单命令行工具。它包含了一个带有静态m ain()方法的类,与simple-weather中定义的WeatherService和simple-persist中定义的DAO交互。simpl e-command有对于simple-weather和simple-persist的直接依赖;还有一个对于simple-model的传递性 依赖。

2)simple-parent项目

simple-parent是所有子模块的父类或者祖先模块,基本上父模块会被用来做为一个“抽象”模块,它不含有实际的可运行或被引用的逻辑代码,parent的pom文件中会声明一些公有的依赖还用一些构建配置例如使用的jdk版本,构建过程中的资源处理,通常在parent的pom中会声明子模块的引用,被引用的子模块在构建parent项目时也会被引用构建,子模块被构建的顺序和其在pom中的声明顺序关系不大,主要由子模块之间的依赖关系决定。maven使用一个被称为reactor(反应堆)的概念来描述这个子模块的构建顺序和依赖处理,记住循环依赖和相互引用在maven解析pom时就会被发现,这与其在parent的pom中声明的顺序是无关的。parent的pom结构如下:


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   <artifactId>simple-parent</artifactId>  
  13.   <packaging>pom</packaging>  
  14.   <name>Multi-Spring Chapter Simple Parent Project</name>  
  15.    
  16.   <modules>  
  17.     <module>simple-command</module>  
  18.     <module>simple-model</module>  
  19.     <module>simple-weather</module>  
  20.     <module>simple-persist</module>  
  21.     <module>simple-webapp</module>  
  22.   </modules>  
  23.   
  24.   <build>  
  25.     <pluginManagement>  
  26.     <plugins>  
  27.       <plugin>  
  28.         <groupId>org.apache.maven.plugins</groupId>  
  29.         <artifactId>maven-compiler-plugin</artifactId>  
  30.         <configuration>  
  31.           <source>1.5</source>  
  32.           <target>1.5</target>  
  33.         </configuration>  
  34.       </plugin>  
  35.     </plugins>  
  36.     </pluginManagement>  
  37.   </build>  
  38.   
  39.   <dependencies>  
  40.     <dependency>  
  41.       <groupId>junit</groupId>  
  42.       <artifactId>junit</artifactId>  
  43.       <version>3.8.1</version>  
  44.       <scope>test</scope>  
  45.     </dependency>  
  46.   </dependencies>  
  47. </project>  

其中的modules元素之间的内容声明了子模块列表,不在此列表中的子模块在parent构建过程中不会被加入reactor,也就不会被构建。还有编译器的目标配置成Java 5 JVM,构建parent的结果如下:

3)simple-model模块

model模块是系统中最基础的模块,它建立了项目的对象模型,项目中的所有其他模块(parent除外)都依赖于model模块,在上面的构建结果中也能看出(model是第一个被构建的子模块)。在Java项目中不管是什么样的项目我们都需要将系统中的概念建模成对象模型,在Maven项目中,将这个对象模型分割成单独的项目以被广泛引用,是一种常用的实践。


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>simple-parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   <artifactId>simple-model</artifactId>  
  13.   <packaging>jar</packaging>  
  14.   
  15.   <name>Multi-Spring Chapter Simple Object Model</name>  
  16.   
  17.   <dependencies>  
  18.     <dependency>  
  19.       <groupId>org.hibernate</groupId>  
  20.       <artifactId>hibernate-annotations</artifactId>  
  21.       <version>3.3.0.ga</version>  
  22.     </dependency>  
  23.     <dependency>  
  24.       <groupId>org.hibernate</groupId>  
  25.       <artifactId>hibernate</artifactId>  
  26.       <version>3.2.5.ga</version>  
  27.       <exclusions>  
  28.         <exclusion>  
  29.           <groupId>javax.transaction</groupId>  
  30.           <artifactId>jta</artifactId>  
  31.         </exclusion>  
  32.       </exclusions>  
  33.     </dependency>  
  34.   </dependencies>  
  35. </project><span style="font-size:18px;">  
  36. </span>  
(4)simple-weather模块

weather模块是一个服务模块,即为上层应用(web,桌面等)提供API接口。这个simple-weather模块包含了所有从Yahoo! Weather RSS数据源获取数据并解析的必要逻辑。虽然simple-weather只包含了三个Java类和一个JUnit测试,它还将展现为一个单独的组件,WeatherService,同时为简单web应用和简单命令行工具服务。


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>simple-parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   <artifactId>simple-weather</artifactId>  
  13.   <packaging>jar</packaging>  
  14.   <name>Multi-Spring Chapter Simple Weather API</name>  
  15.   <dependencies>  
  16.     <dependency>  
  17.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  18.       <artifactId>simple-model</artifactId>  
  19.       <version>0.8-SNAPSHOT</version>  
  20.     </dependency>  
  21.     <dependency>  
  22.       <groupId>log4j</groupId>  
  23.       <artifactId>log4j</artifactId>  
  24.       <version>1.2.14</version>  
  25.     </dependency>  
  26.     <dependency>  
  27.       <groupId>dom4j</groupId>  
  28.       <artifactId>dom4j</artifactId>  
  29.       <version>1.6.1</version>  
  30.     </dependency>  
  31.     <dependency>  
  32.       <groupId>jaxen</groupId>  
  33.       <artifactId>jaxen</artifactId>  
  34.       <version>1.1.1</version>  
  35.     </dependency>  
  36.     <dependency>  
  37.       <groupId>org.apache.commons</groupId>  
  38.       <artifactId>commons-io</artifactId>  
  39.       <version>1.3.2</version>  
  40.       <scope>test</scope>  
  41.     </dependency>  
  42.   </dependencies>  
  43. </project><span style="font-size:14px;">  
  44. </span>  

weather模块直接依赖于model模块,因而在reactor的构建顺序中weather在model之后

(5)simple-persist模块

persist模块提供了对数据库访问的操作,也即是我们通常所熟知的DAO

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>simple-parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   <artifactId>simple-persist</artifactId>  
  13.   <packaging>jar</packaging>  
  14.   
  15.   <name>Multi-Spring Chapter Simple Persistence API</name>  
  16.   
  17.   <dependencies>  
  18.     <dependency>  
  19.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  20.       <artifactId>simple-model</artifactId>  
  21.       <version>0.8-SNAPSHOT</version>  
  22.     </dependency>  
  23.     <dependency>  
  24.       <groupId>org.hibernate</groupId>  
  25.       <artifactId>hibernate</artifactId>  
  26.       <version>3.2.5.ga</version>  
  27.       <exclusions>  
  28.         <exclusion>  
  29.           <groupId>javax.transaction</groupId>  
  30.           <artifactId>jta</artifactId>  
  31.         </exclusion>  
  32.       </exclusions>  
  33.     </dependency>  
  34.     <dependency>  
  35.       <groupId>org.hibernate</groupId>  
  36.       <artifactId>hibernate-annotations</artifactId>  
  37.       <version>3.3.0.ga</version>  
  38.     </dependency>  
  39.     <dependency>  
  40.       <groupId>org.hibernate</groupId>  
  41.       <artifactId>hibernate-commons-annotations</artifactId>  
  42.       <version>3.3.0.ga</version>  
  43.     </dependency>  
  44.     <dependency>  
  45.       <groupId>org.apache.geronimo.specs</groupId>  
  46.       <artifactId>geronimo-jta_1.1_spec</artifactId>  
  47.       <version>1.1</version>  
  48.     </dependency>  
  49.     <dependency>  
  50.       <groupId>org.springframework</groupId>  
  51.       <artifactId>spring</artifactId>  
  52.       <version>2.0.7</version>  
  53.     </dependency>  
  54.   </dependencies>  
  55. </project><span style="font-size:14px;">  
  56. </span>  

persist模块也是依赖于model模块,persist模块和weather模块在依赖链中的层次是一致的,reactor在处理处于同一依赖层次中的顺序与其在parent的pom文件中声明顺序相关。

(6)simple-webapp模块

webapp模块是系统中的最上层应用,它提供了使用http协议访问系统的入口


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>simple-parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   
  13.   
  14.   <artifactId>simple-webapp</artifactId>  
  15.   <packaging>war</packaging>  
  16.   <name>Multi-Spring Chapter Simple Web Application</name>  
  17.   <dependencies>  
  18.     <dependency>  
  19.       <groupId>org.apache.geronimo.specs</groupId>  
  20.       <artifactId>geronimo-servlet_2.4_spec</artifactId>  
  21.       <version>1.1.1</version>  
  22.     </dependency>  
  23.     <dependency>  
  24.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  25.       <artifactId>simple-weather</artifactId>  
  26.       <version>0.8-SNAPSHOT</version>  
  27.     </dependency>  
  28.     <dependency>  
  29.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  30.       <artifactId>simple-persist</artifactId>  
  31.       <version>0.8-SNAPSHOT</version>  
  32.     </dependency>  
  33.     <dependency>  
  34.       <groupId>org.springframework</groupId>  
  35.       <artifactId>spring</artifactId>  
  36.       <version>2.0.7</version>  
  37.     </dependency>  
  38.     <dependency>  
  39.       <groupId>javax.servlet</groupId>  
  40.       <artifactId>jstl</artifactId>  
  41.       <version>1.1.2</version>  
  42.     </dependency>  
  43.     <dependency>  
  44.       <groupId>taglibs</groupId>  
  45.       <artifactId>standard</artifactId>  
  46.       <version>1.1.2</version>  
  47.     </dependency>  
  48.     <dependency>  
  49.       <groupId>org.apache.velocity</groupId>  
  50.       <artifactId>velocity</artifactId>  
  51.       <version>1.5</version>  
  52.     </dependency>  
  53.   </dependencies>  
  54.   <build>  
  55.     <finalName>simple-webapp</finalName>  
  56.     <plugins>  
  57.       <plugin>  
  58.         <groupId>org.mortbay.jetty</groupId>  
  59.         <artifactId>maven-jetty-plugin</artifactId>  
  60.         <dependencies>  
  61.           <dependency>  
  62.             <groupId>hsqldb</groupId>  
  63.             <artifactId>hsqldb</artifactId>  
  64.             <version>1.8.0.7</version>  
  65.           </dependency>  
  66.         </dependencies>  
  67.       </plugin>  
  68.       <plugin>  
  69.         <groupId>org.codehaus.mojo</groupId>  
  70.         <artifactId>hibernate3-maven-plugin</artifactId>  
  71.         <version>2.1</version>  
  72.         <configuration>  
  73.           <components>  
  74.             <component>  
  75.               <name>hbm2ddl</name>  
  76.               <implementation>annotationconfiguration</implementation>  
  77.             </component>  
  78.           </components>  
  79.         </configuration>  
  80.         <dependencies>  
  81.           <dependency>  
  82.             <groupId>hsqldb</groupId>  
  83.             <artifactId>hsqldb</artifactId>  
  84.             <version>1.8.0.7</version>  
  85.           </dependency>  
  86.         </dependencies>  
  87.       </plugin>  
  88.     </plugins>  
  89.   </build>  
  90. </project>  

webapp模块依赖于persist和weather模块,因而其在reactor中的构建时间要晚于persist和weather。

(7)simple-command模块

通常一个复杂的项目不仅仅只有一个上层应用,面对不同的用户不同的环境可能需要提供更多的上层应用以应对复杂的生产需求。simple-command项目是simple-webapp的一个命令行版本。


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  3.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  5.                              http://maven.apache.org/maven-v4_0_0.xsd">  
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   <parent>  
  8.     <groupId>org.sonatype.mavenbook.multispring</groupId>  
  9.     <artifactId>simple-parent</artifactId>  
  10.     <version>0.8-SNAPSHOT</version>  
  11.   </parent>  
  12.   
  13.   <artifactId>simple-command</artifactId>  
  14.   <packaging>jar</packaging>  
  15.   <name>Multi-Spring Chapter Simple Command Line Tool</name>  
  16.   
  17.   <build>  
  18.     <finalName>${project.artifactId}</finalName>  
  19.     <plugins>  
  20.       <plugin>  
  21.         <groupId>org.apache.maven.plugins</groupId>  
  22.         <artifactId>maven-surefire-plugin</artifactId>  
  23.         <configuration>  
  24.           <testFailureIgnore>true</testFailureIgnore>  
  25.         </configuration>  
  26.       </plugin>  
  27.       <plugin>  
  28.        <artifactId>maven-assembly-plugin</artifactId>  
  29.         <configuration>  
  30.           <descriptorRefs>  
  31.             <descriptorRef>jar-with-dependencies</descriptorRef>  
  32.           </descriptorRefs>  
  33.         </configuration>  
  34.       </plugin>  
  35.        <plugin>  
  36.         <groupId>org.codehaus.mojo</groupId>  
  37.         <artifactId>hibernate3-maven-plugin</artifactId>  
  38.         <version>2.1</version>  
  39.         <configuration>  
  40.           <components>  
  41.             <component>  
  42.               <name>hbm2ddl</name>  
  43.               <implementation>annotationconfiguration</implementation>  
  44.             </component>  
  45.           </components>  
  46.         </configuration>  
  47.         <dependencies>  
  48.           <dependency>  
  49.             <groupId>hsqldb</groupId>  
  50.             <artifactId>hsqldb</artifactId>  
  51.             <version>1.8.0.7</version>  
  52.           </dependency>  
  53.         </dependencies>  
  54.       </plugin>  
  55.     </plugins>  
  56.   </build>  
  57.   
  58.   <dependencies>  
  59.     <dependency>  
  60.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  61.       <artifactId>simple-weather</artifactId>  
  62.       <version>0.8-SNAPSHOT</version>  
  63.     </dependency>  
  64.     <dependency>  
  65.       <groupId>org.sonatype.mavenbook.multispring</groupId>  
  66.       <artifactId>simple-persist</artifactId>  
  67.       <version>0.8-SNAPSHOT</version>  
  68.     </dependency>  
  69.     <dependency>  
  70.       <groupId>org.springframework</groupId>  
  71.       <artifactId>spring</artifactId>  
  72.       <version>2.0.7</version>  
  73.     </dependency>  
  74.     <dependency>  
  75.       <groupId>org.apache.velocity</groupId>  
  76.       <artifactId>velocity</artifactId>  
  77.       <version>1.5</version>  
  78.     </dependency>      
  79.     <dependency>  
  80.       <groupId>hsqldb</groupId>  
  81.       <artifactId>hsqldb</artifactId>  
  82.       <version>1.8.0.7</version>  
  83.     </dependency>  
  84.   </dependencies>  
  85. </project>  

webapp模块依赖于persist和weather模块,因而其在reactor中的构建时间要晚于persist和weather。


(8)多模块结构

在Eclipse中的结构如下

                                                                

文件系统中的结构

                                  

3. maven多模块拆分中的问题

(1)划分模块的方式

模块划分主要是根据程序的职责单一性和耦合性,如果是项目建立初期就使用多模块来规划整个项目,那么职责单一性原则应该是首要考虑的也就是通常意义上的按照层次划分(和上面的例子类似),如果是从一个已经十分复杂的项目开始拆分那么在划分模块的时候就不仅仅是考虑职责单一了,职责单一会造成大量的子模块产生导致pom文件臃肿且不容易识别,如果将耦合性考虑进去就应该将关系较为紧密的模块合并降低模块的数量提高实用性。

  (2)公有依赖的抽象

 parent中的依赖配置主要是一些公有的依赖,例如log,apache commons,spring等,怎么界定一个依赖是否属于公有呢?一般情况下如果这个依赖被超过2/3的子模块所依赖就可以将其认定为公有依赖,另外也可以将依赖的某些配置(例如版本号)以parent中pom的属性的形式加以声明,这样在升级某些依赖的时候就只改动一处就行了(很类似C语言中的#define)

  (3)模块的存在与消亡

 模块的划分并不是一成不变的,模块的存在就是为了方便维护和提高生产效率,如果某些模块不合理并且影响了开发效率,那么这些模块就需要再好好斟酌一下。一般情况下这种情况出现不是因为模块过于庞大就是因为模块的碎片化,对于前者需要拆分更多模块以提高复用和去除冗余,后者则需要酌情合并一些耦合性较高的模块。

  (4)子模块的子模块

 这种情况的出现往往就意味着这个项目本身就应该被分成多个项目,多个项目之间也可以继承同一个parent的pom.xml这主要是为了方便统一构建多个项目。所以子模块的子模块应该避免出现

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值