Maven讲解之 聚合

来源:互联网 发布:淘宝猛犸象牙是真的吗 编辑:程序博客网 时间:2024/05/18 11:03

Maven讲解之 聚合

概念

将多个工程拆分为模块后,需要手动逐个安装到仓库后依赖才能够生效。修改源码后也需要逐个手动进
行 clean 操作。而使用了聚合之后就可以批量进行 Maven 工程的安装、清理工作。


  • 实例讲解

    • 父工程

      • 新建父工程,以pom为打包方式的Maven工程

        这里写图片描述

        • 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>    <groupId>com.sstps.oscar</groupId>    <artifactId>Adoie</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>pom</packaging>    <!-- 配置聚合 -->    <modules>        <module>../Brokers</module>        <module>../Market</module>        <module>../Customer</module>    </modules>    <!-- 统一管理依赖 -->    <dependencyManagement>        <dependencies>            <!-- https://mvnrepository.com/artifact/junit/junit -->            <dependency>                <groupId>junit</groupId>                <artifactId>junit</artifactId>                <version>4.0</version>                <scope>test</scope>            </dependency>        </dependencies>    </dependencyManagement></project>
    • 新建Maven子项目

      • Brokers

        • 项目结构
          这里写图片描述

        • 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>    <artifactId>Brokers</artifactId>    <packaging>war</packaging>    <!-- 子工程中声明父工程 -->    <parent>        <groupId>com.sstps.oscar</groupId>        <artifactId>Adoie</artifactId>        <version>0.0.1-SNAPSHOT</version>        <!-- 声明继承父工程的pom.xml路径 -->        <relativePath>../Adoie/pom.xml</relativePath>    </parent>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <scope>test</scope>        </dependency>    </dependencies></project>
      • Market

        • 项目结构
          这里写图片描述

        • 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>  <groupId>com.sspts.oscar</groupId>  <artifactId>Market</artifactId>  <packaging>war</packaging>  <!-- 子工程中声明父工程 -->    <parent>        <groupId>com.sstps.oscar</groupId>        <artifactId>Adoie</artifactId>        <version>0.0.1-SNAPSHOT</version>        <!-- 声明继承父工程的pom.xml路径 -->        <relativePath>../Adoie/pom.xml</relativePath>    </parent>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <scope>test</scope>        </dependency>    </dependencies></project>
      • Customer

        • 项目结构

          这里写图片描述

      • 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>    <groupId>com.sspts.oscar</groupId>    <artifactId>Customer</artifactId>    <packaging>war</packaging>    <!-- 子工程中声明父工程 -->    <parent>        <groupId>com.sstps.oscar</groupId>        <artifactId>Adoie</artifactId>        <version>0.0.1-SNAPSHOT</version>        <!-- 声明继承父工程的pom.xml路径 -->        <relativePath>../Adoie/pom.xml</relativePath>    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <spring.version>4.0.4.RELEASE</spring.version>    </properties>    <dependencies>        <!-- spring start -->        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>${spring.version}</version>            <exclusions>                <exclusion>                    <groupId>commons-logging</groupId>                    <artifactId>commons-logging</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aspects</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-beans</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <!-- spring end -->        <!-- <dependency>            <groupId>com.sspts.oscar</groupId>            <artifactId>Market</artifactId>            <version>0.0.1-SNAPSHOT</version>            <scope>compiler</scope>        </dependency> -->        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>3.1.0</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <scope>test</scope>        </dependency>    </dependencies></project>

聚合操作

  • clean

    这里写图片描述

  • install

    这里写图片描述


注意

  • 聚合时子工程必须引用父工程GAV即pom.xml。

  • 子工程之间应避免相互存在依赖的情况。

  • 在聚合操作过程中,子工程被执行的顺序与其在父工程中的Models配置顺序有关,从上至下逐个执行。

  • 当某个子工程执行失败之后,该子工程之后的所有子工程都将跳过不执行。