Maven 继承与聚合

1.继承

继承这个概念对于java程序员并不陌生,那在maven中的继承作用也和java中的一样么?maven为什么需要继承呢?
我们在上一章 Maven 依赖 中提到了依赖的范围概念,了解到junit 依赖是test范围的依赖,是不可以传递的,因此在多模块项目中我们在每个模块都需要依赖。那么问题来了既然是多模块那就是不同的组甚至是不同的部门来开发,junit依赖的version 很有可能就会不一样,但是为了项目后期维护以及项目人员调度尽可能的方便,我们需要将junit的版本设为统一的。所以就需要有一个机制来给统一这个标准,maven提供了一种机制就是 继承。统一的步骤:

  1. 通过定义一个父工程,并且在该父工程的 pom 文件中使用 dependencyManagement来声明junit的统一version
    父工程的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.javxuam.maven</groupId>
   <artifactId>MavenProject</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <version>4.12</version>
               <scope>test</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
</project>

2.A模块继承父工程pom.xml,定义junit依赖时候不声明version,去除A中pom.xml 与父工程pom.xml中重复的部分

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>MavenProject</artifactId>
        <groupId>com.javxuam.maven</groupId>
        <version>1.0-SNAPSHOT</version>
        <!--最好声明一下以当前pom文件目录为基准
        的父工程的pom文件-->
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <!--本来A模块也是需要 三点确定的,
    但是 groupId 与version与父工程的相同
    所以去掉,不去掉也不会报错-->
    <groupId>com.javxuam.maven</groupId>
    <artifactId>A</artifactId>
    <version>1.0-SNAPSHOT</version>

  <dependencies>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <!--不声明version-->
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>4.2.6.RELEASE</version>
      </dependency>
  </dependencies>
</project>

同样给 B,C模块继承父模块,再查看A,B,C三个模块的junit依赖
这里写图片描述

至此 继承就解决了 统一依赖版本的问题。

2.聚合

聚合在java 中是在A对象功能完整需要 另外B对象来实现,所以A类中有着B类对象的引用,但是A对象的生命周期结束,B对象依然可以存在,简而言之就两个对象的生命周期不同。当然在maven中,聚合又是为了解决另外一个问题而存在的一种机制。通常我们的项目都是都模块的,而每个模块又是一个 maven项目,所以每次开发完了编译都需要一个一个模块的去执行,这就违背了maven的自动化理念。
所以聚合应运而生,具体做法:

<?xml version="1.0" encoding="UTF-8"?>
<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.javxuam.maven</groupId>
   <artifactId>MavenProject</artifactId>
   <version>1.0-SNAPSHOT</version>
   <packaging>pom</packaging>

   <modules>
       <module>A</module>
       <module>B</module>
       <module>C</module>
       <module>D</module>
       <module>WebProject</module>
   </modules>

   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <version>4.12</version>
               <scope>test</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>
</project>
  1. 然后执行一下 父模块的 clean 看 A,B,C,D模块是否也会一起clean
    这里写图片描述
    从图中可以看出所有的模块都执行了clean,这就避免了一个一个模块的clean操作了。

猜你喜欢

转载自blog.csdn.net/u014297148/article/details/79939656