说明

  1. 现在有了父工程,可以实现构建父工程就相当于构建所有子工程了

  2. 有没有这种可能,有的人可能就是某一天,突然就想升级spring版本到5.0,结果其他人用的都是4.0,最后导致项目集成的时候就出各种问题


  1. 比如项目里现在需要用一个组件,activiti,结果每个模块的负责人各自加入自己的依赖,有的人用1.0,有的人用2.0,有的人用3.0,完全没有统一起来


  1. 那么maven继承功能就是用来解决这种情况的,通常对同一个系统,需要将各个模块的工程,其中的基础性的、相同的依赖,全部放入一个父工程中,集中式统一约束所有模块的依赖,避免每个模块负责的人胡乱使用依赖版本


默认情况

  1. 我们在父模块的pom中添加整个项目都依赖的jar包

    1. <dependencies>
    2. <dependency>
    3. <groupId>junit</groupId>
    4. <artifactId>junit</artifactId>
    5. <version>4.11</version>
    6. <scope>test</scope>
    7. </dependency>
    8. </dependencies>
  2. 在子模块的pom中声明parent,继承父工程的依赖

    <parent>
     <artifactId>taobao-parent</artifactId>
     <groupId>com.chenshuyi</groupId>
     <version>1.0-SNAPSHOT</version>
    </parent>
    
  3. 缺点:实际的场景下,不同的工程需要的依赖是不一样的,可能有的工程需要从父工程继承20个依赖和3个插件;有的工程需要从父工程继承10个依赖和2个插件。如果按照上面那种最low最普通的做法,会导致每个子工程都强制性从父工程继承所有的依赖,本来某个子工程只要10个依赖,现在导致每次打包,assembly的大包,可能导致每个工程的jar包打包速度变慢,打完以后的包比较大。不推荐


DM/PM

  1. 最推荐的做法就是在父工程中,使用两个元素,来声明要被子工程继承的依赖和插件


  1. 默认子工程什么都不继承,只有当子工程声明了某个依赖或者插件的groupId+artifactId,但是不指定版本时,才会从父工程继承那个依赖

    配置

  2. 父模块使用元素来定义依赖 ```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">
    
    4.0.0

    com.wisdom.oa oa-parent 1.0-SNAPSHOT

    pom

    oa-parent

    http://maven.apache.org oa-organ oa-auth oa-flow UTF-8 nexus-releases Nexus Release Repository http://localhost:8081/repository/maven-releases/ nexus-snapshots Nexus Snapshot Repository http://localhost:8081/repository/maven-snapshots/ org.springframework spring-core 3.2.8.RELEASE org.springframework spring-webmvc 3.2.8.RELEASE org.springframework spring-context 3.2.8.RELEASE org.springframework spring-context-support 3.2.8.RELEASE org.springframework spring-aop 3.2.8.RELEASE org.springframework spring-aspects 3.2.8.RELEASE org.springframework spring-tx 3.2.8.RELEASE org.springframework spring-jdbc 3.2.8.RELEASE org.springframework spring-web 3.2.8.RELEASE junit junit 4.12 test org.springframework spring-test 3.2.8.RELEASE test log4j log4j 1.2.12 org.slf4j slf4j-api 1.6.6 org.slf4j slf4j-log4j12 1.6.6 org.mybatis mybatis 3.2.1 org.mybatis mybatis-spring 1.2.0 mysql mysql-connector-java 5.1.29 org.apache.maven.plugins maven-source-plugin 2.1.1 attach-sources verify jar-no-fork


2. 子模块使用parent来指定夫模块,然后需要哪个依赖就填哪个依赖,不需要加版本号
```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>

    <artifactId>oa-organ</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.wisdom.oa</groupId>
        <artifactId>oa-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- 部署自定义jar包 -->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/repository/maven-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

    <dependencies>
        <!-- spring依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>

        <!--单元测试依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 日志依赖 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </dependency>

        <!--mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

        <!-- mysql驱动包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>


</project>

集中配置版本号

  1. 还有一个问题,类似像Spring这种框架,一般来说都有很多依赖,依赖的版本号都是一样的如3.2.8.RELEASE,那么如果公司要统一升级版本,岂不是要一个个进行修改,这太麻烦了,Maven还可以统一配置版本号

  2. 一般都会在properties元素里面,统一定义一个版本号,然后在这个类似spring的依赖里面,全部用${}占位符来引用一个版本号,那么每次修改,升级spring版本,就直接修改properties元素中的一个地方就可以了


  1. 配置如下 ```xml

    properties元素中统一定义一个版本号变量,名称可以随便起

    3.2.8.RELEASE

在version中引用该变量

org.springframework spring-webmvc ${springframework.release.version} ```