Maven单继承问题

单继承parent

  在maven多模块项目中,为了方便依赖的统一管理,引入parent标签。以SpringBoot为例进行说明,在parent的dependencyManagement中对子模块的所有依赖进行声明,如下所示:

 <dependencyManagement>
        <dependencies>
            <!-- Spring Boot -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <version>1.5.3.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot</artifactId>
                <type>test-jar</type>
                <version>1.5.3.RELEASE</version>
            </dependency>
        <dependencies>
 <dependencyManagement>

子模块继承parent,在dependencies中添加所需的依赖即可(无需声明版本),如下所示:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

单继承存在问题

  1. 子模块中只能使用一个parent标签;
  2. 如果parent模块中,dependencyManagement中预定义太多的依赖,造成pom文件过长,而且很乱;

解决方案import scope

  1. 将众多的依赖分类,分别建立单独的pom文件;
  2. 子模块中通过dependencyManagement引入对应的pom,并将scope设为import,如下所示:
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

参考:

  1. https://www.cnblogs.com/huahua035/p/7680607.html
  2. https://blog.csdn.net/mn960mn/article/details/50894022

猜你喜欢

转载自blog.csdn.net/yangguosb/article/details/80967107