Maven聚合和继承

Part one

我们在开发过程中,创建了2个以上的模块,每个模块都是一个独立的maven project,在开始的时候我们可以独立的编译和测试运行每个模块,但是随着项目的不断变大和复杂化,我们期望能够使用简单的操作来完成编译等工作,这时Maven给出了聚合的配置方式。

所谓聚合,顾名思义,就是把多个模块或项目聚合到一起,我们可以建立一个专门负责聚合工作的Maven project ---  aggregator。

建立该project的时候,我们要注意以下几点:

1.该aggregator本身也做为一个Maven项目,它必须有自己的POM

2.它的打包方式必须为: pom

3.引入了新的元素:modules---module

4.版本:聚合模块的版本和被聚合模块版本一致

5.relative path:每个module的值都是一个当前POM的相对目录

6.目录名称:为了方便的快速定位内容,模块所处的目录应当与其artifactId一致(Maven约定而不是硬性要求),总之,模块所处的目录必须和<module>模块所处的目录</module>相一致。

7.习惯约定:为了方便构建,通常将聚合模块放在项目目录层的最顶层,其它聚合模块作为子目录存在。这样当我们打开项目的时候,第一个看到的就是聚合模块的POM

8.聚合模块减少的内容:聚合模块的内容仅仅是一个pom.xml文件,它不包含src/main/Java、src/test/java等目录,因为它只是用来帮助其它模块构建的工具,本身并没有实质的内容。

9.聚合模块和子模块的目录:他们可以是父子类,也可以是平行结构,当然如果使用平行结构,那么聚合模块的POM也需要做出相应的更改。

二、为什么要继承?

做面向对象编程的人都会觉得这是一个没意义的问题,是的,继承就是避免重复,maven的继承也是这样,它还有一个好处就是让项目更加安全

情景分析二:我们在项目开发的过程中,可能多个模块独立开发,但是多个模块可能依赖相同的元素,比如说每个模块都需要Junit,使用spring的时候,其核心jar也必须都被引入,在编译的时候,maven-compiler-plugin插件也要被引入

如何配置继承:

1.说到继承肯定是一个父子结构,那么我们在aggregator中来创建一个parent project

2.<packaging>: 作为父模块的POM,其打包类型也必须为POM

3.结构:父模块只是为了帮助我们消除重复,所以它也不需要src/main/java、src/test/java等目录

4.新的元素:<parent> , 它是被用在子模块中的

5.<parent>元素的属性:<relativePath>: 表示父模块POM的相对路径,在构建的时候,Maven会先根据relativePath检查父POM,如果找不到,再从本地仓库查找

6.relativePath的默认值: ../pom.xml

7.子模块省略groupId和version: 使用了继承的子模块中可以不声明groupId和version, 子模块将隐式的继承父模块的这两个元素

三、可被继承的POM元素

groupId:项目组ID,项目坐标的核心元素

version: 项目版本, 项目坐标的核心元素

description: 项目的描述信息

organization: 项目的组织信息

inceptionYear: 项目的创始年份

url: 项目的URL地址

developers: 项目开发者信息

contributors: 项目的贡献者信息

distributionManagement: 项目的部署配置

issueManagement: 项目的缺陷跟踪系统信息

ciManagement: 项目的持续集成系统信息

scm: 项目的版本控制系统信息

mailingLists: 项目的邮件列表信息

properties: 自定义的maven属性

dependencies: 项目的依赖配置

dependencyManagement: 项目的依赖管理配置

repositories: 项目的仓库配置

build: 包括项目的源码目录配置、输出目录配置、插件配置、插件管理配置等

reporting: 包括项目的报告输出目录配置、报告插件配置等

四、maven的依赖管理

我们知道dependencies是可以被继承的,这个时候我们就想到让我们的发生了共用的依赖元素转移到parent中,这样我们又进一步的优化了配置。可是问题也随之而来,如果有一天我创建了一个新的模块,但是这个模块不需要这些parent的依赖,这时候如何处理?

是的,maven的依赖管理就是来解决这个问题的

增加一个新的元素:dependencyManagement

从上面的列表中我们发现dependencyManagement也是可以被继承的,这恰恰满足了我们的需要,它既能够让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性

dependencyManagement的特性:在dependencyManagement中配置的元素既不会给parent引入依赖,也不会给它的子模块引入依赖,仅仅是它的配置是可继承的

最佳实践:

这时候我们就可以在父POM中声明这些依赖:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span><properties>  
  2.         <target.version>2.5.6</target.version>  
  3.     </properties>  
  4.   
  5.     <dependencyManagement>  
  6.         <dependencies>  
  7.             <dependency>  
  8.                 <groupId>your groupId</groupId>  
  9.                 <artifactId>your artifactId</artifactId>  
  10.                 <version>${target.version}</version>  
  11.             </dependency>  
  12.         </dependencies>  
  13.     </dependencyManagement>  

子模块的POM继承这些配置:子模块继承这些配置的时候,仍然要声明groupId和artifactId,表示当前配置是继承于父POM的,从而直接使用父POM的版本对应的资源

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">        </span><dependencies>  
  2.             <dependency>  
  3.                 <groupId>your groupId</groupId>  
  4.                 <artifactId>your artifactId</artifactId>  
  5.             </dependency>  
  6.         </dependencies>  

这个可以有效的避免多个子模块使用依赖版本不一致的情况,有助于降低依赖冲突的几率。注:只有子模块配置了继承的元素,才会真正的有效,否则maven是不会加载父模块中声明的元素。

五、Maven的插件管理

再增加一个新的元素:<pluginManagement>

这个元素和<dependencyManagement>相类似,它是用来进行插件管理的。

在我们项目开发的过程中,也会频繁的引入插件,所以解决这些复杂配置的方法就是使用插件管理

我们可以在父POM中做如下声明:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span><build>  
  2.         <pluginManagement>  
  3.             <plugins>  
  4.                 <plugin>  
  5.                     <groupId></groupId>  
  6.                     <artifactId></artifactId>  
  7.                     <version></version>  
  8.                     <executions>  
  9.                         <execution>  
  10.                             <id></id>  
  11.                             <goals>  
  12.                                 <goal></goal>  
  13.                             </goals>  
  14.                             <phase></phase>  
  15.                             <configuration>  
  16.                                 <source></source>  
  17.                                 <target></target>  
  18.                             </configuration>  
  19.                         </execution>  
  20.                     </executions>  
  21.                 </plugin>  
  22.             </plugins>  
  23.         </pluginManagement>  
  24.     </build>  


在子模块中,我们可以直接继承

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="white-space:pre">    </span><build>  
  2.         <plugins>  
  3.             <plugin>  
  4.                 <groupId>org.apache.maven.plugins</groupId>  
  5.                 <artifactId>maven-compiler-plugin</artifactId>  
  6.             </plugin>  
  7.         </plugins>  
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <pre name="code" class="html"><span style="white-space:pre">    </span></build>  

 我们会发现,继承的细节上和<dependencyManagement>几乎一样。 

六、聚合与继承的总结

当我们明白聚合与继承的细节之后,我们会发现:

对于聚合模块来说,它知道有哪些被聚合的模块,而对于被聚合的模块来说,它们不知道被谁聚合了,也不知道它的存在

对于继承关系的父POM来说,它不知道自己被哪些子模块继承了,对于子POM来说,它必须知道自己的父POM是谁

在一些最佳实践中我们会发现:一个POM既是聚合POM,又是父POM,这么做主要是为了方便。

Part Two

 Maven的聚合特性能够把项目的各个模块聚合在一起构件,而Maven的继承特性则能帮助抽取各模块相同的依赖和插件等配置。在简化POM的同时,还能促进各个模块配置的一致性。下面以具体项目来讲解:

1. 项目结构


以上有五个项目分别为user-parent, user-core, user-log, user-dao, user-service. 其中user-parent项目为其他项目的父项目,只是帮助其他模块构建的工具,它本身并无实质的内容,只有一个pom.xml文件。

2. user-parent的pom.xml详情如下(下文简称为父POM)

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.liangbo.user</groupId>
  <artifactId>user-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
      <module>../user-core</module>
      <module>../user-dao</module>
      <module>../user-log</module>
      <module>../user-service</module>
  </modules>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.version>4.10</junit.version>
    <mysql.driver>com.mysql.jdbc.Driver</mysql.driver>
    <mysql.url>jdbc:mysql://localhost:3306/mysql</mysql.url>
    <mysql.username>root</mysql.username>
    <mysql.password>password</mysql.password>
  </properties>
  
  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>com.liangbo.user</groupId>
            <artifactId>user-core</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.liangbo.user</groupId>
            <artifactId>user-log</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.liangbo.user</groupId>
            <artifactId>user-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        
        <dependency>
            <groupId>com.liangbo.user</groupId>
            <artifactId>user-log</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.9</version>
        </dependency>
    
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.10.Final</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
    
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        
    </dependencies>
  </dependencyManagement>
  
  <build>
      <pluginManagement>
          <plugins>
              <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>jar-no-fork</goal></goals>
                    </execution>
                </executions>
            </plugin>
            
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.5</version>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.26</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <driver>${mysql.driver}</driver>
                    <url>${mysql.url}</url>
                    <username>${mysql.username}</username>
                    <password>${mysql.password}</password>
                    <sqlCommand>
                        create database IF NOT EXISTS maven_test
                    </sqlCommand>
                </configuration>                
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                    </execution>                
                </executions>
            </plugin>
            
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-rar-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals><goal>rar</goal></goals>
                    </execution>
                </executions>
            </plugin>
          </plugins>
      </pluginManagement>
  </build>
</project>


上面的pom.xml文件既有Maven的聚合特性又有继承特性。这样做是为了更加方便管理其他模块。其中元素modules,是实现聚合的最核心配置。多个子模块之间的pom有很多的相同的配置,这时候可以全部提取出来放到父项目中。重复往往就意味着更多的劳动和更多的潜在的问题。那么子模块如何来继承他的父项目呢?

3. 下面来看下user-core项目pom.xml的配置

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent>
      <groupId>com.liangbo.user</groupId>
      <artifactId>user-parent</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <relativePath>../user-parent/pom.xml</relativePath>
  </parent> 

  <artifactId>user-core</artifactId>
  <packaging>jar</packaging>

  <name>user-core</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
    </dependency>
  </dependencies>
  
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
          </plugin>
          
          <plugin>
              <groupId>org.codehaus.mojo</groupId>
            <artifactId>sql-maven-plugin</artifactId>
          </plugin>
          
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-rar-plugin</artifactId>
          </plugin>
      </plugins>
  </build>
</project>


其中设置好relativePath非常重要,不然无法找到父POM。这时候子POM中只需要配置groupId和artifactId就可以了。

4. 依赖管理和插件管理

上述子POM配置简单了很多,是因为父POM引用了依赖管理和插件管理的概念
如果父POM没有配置依赖管理和插件管理的话,继承它的子模块会将父所有的依赖和插件都继承下来,显然这是不可行的。接下来讲下如何配置的:

Maven提供的dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证子模块依赖使用的灵活性(也就是说写在dependencyManagement中的jar包依赖不会自动被子模块继承,除非子模块中显示声明要使用到的jar包依赖,才会把该对应的jar包依赖从父模块那继承下来,当然在父模块中我们已经写了groupId,artifactId,version,所以子模块中只需要写groupId和artifactId就可以继承该依赖不必再写版本号)。在dependencyManagement元素下的依赖声明不会引入实际的依赖,不过它可以约束dependencies下的依赖使用。插件管理同上,需要在父POM中加入pluginManagement元素。

最后,说下聚合与继承的关系,多模块Maven中的聚合与继承其实是两个不同的概念,其目的是不同的。前者主要是为了方便快速构建项目(即方便将项目中多个模块进行管理,比如一起编译测试而不用挨个去执行命令),后者主要是为了消除重复配置。 当然聚合与继承的pom 一般写在同一个pom项目中主要是为了方便,  聚合一般写在pom.xml中最上边。

说明一下 版本号一般为 x.x.x-SNAPSHOT (snapshot即开发中的版本  开发快照) x.x.x-release 表示发布版本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值