【JAVA企业级开发】关于maven中的dependency通过(type pom /type),(scope import /scope)的依赖方式解决POM单继承问题的思考理解

一级目录

二级目录

三级目录

一在maven多模块项目中,为了保持模块间依赖的统一,常规做法是在parent model中,使用dependencyManagement预定义所有模块需要用到的dependency(依赖)

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <groupId>org.example</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>commonApi</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencyManagement>
         <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>
    </dependencyManagement>


</project>

二子model根据实际需要引入parent中预定义的依赖

<?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>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>provider</artifactId>

    <dependencies>
        <dependency>
            <artifactId>commonApi</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

三parent模块DependencyManagement好处

1、依赖统一管理(parent中定义,需要变动dependency版本,只要修改一处即可);

2、代码简洁(子model只需要指定groupId、artifactId即可)

3、dependencyManagement只会影响现有依赖的配置,但不会引入依赖,即子model不会继承parent中dependencyManagement所有预定义的depandency,只引入需要的依赖即可,简单说就是“按需引入依赖”或者“按需继承”;

4 因此,在parent中严禁直接使用depandencys预定义依赖,坏处是子model会自动继承depandencys中所有预定义依赖;

四问题

1 问题的来源和产生

单继承:maven的继承跟java一样,单继承,也就是说子model中只能出现一个parent标签;

parent模块中,dependencyManagement中预定义太多的依赖,造成pom文件过长,而且很乱;

如何让这些依赖可以分类并清晰的管理?

2问题解决

使用pom,import的依赖方式

如何使用:

1、maven2.9以上版本

2、将dependency分类,每一类建立单独的pom文件

3、在需要使用到这些依赖的子model中,使用dependencyManagement管理依赖,并import scope依赖

3、注意:scope=import只能用在dependencyManagement里面,且仅用于type=pom的dependency

示例:
父项目:

<?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>org.example</groupId>
    <artifactId>springcloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>commonApi</module>
        <module>provider</module>
        <module>consumer</module>
    </modules>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.0.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
    </dependencyManagement>


</project>

子项目:

<?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>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>provider</artifactId>

    <dependencies>
           <!-- 引入其他子项目的中类的依赖 -->
        <dependency>
            <artifactId>commonApi</artifactId>
            <groupId>org.example</groupId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

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

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

</project>

好处分析:

1、单一职责原则,根据依赖的分类,细化每一个单一职责的pom文件

2、解决单继承问题,通过import pom文件达到依赖的目的(典型的非继承模式),从而不用从父类中引用依赖

3、父模块的pom就会非常干净,容易维护

五关于spring-boot-starter-parent 与 spring-boot-dependencies

Spring Boot的每个发布版本都会规划它所支持的依赖项。实际上,你不用指定这些依赖项的版本号,因为Spring Boot都为你管理好了。当更新Spring Boot时,会相应的更新依赖。

1Maven管理依赖

Maven用户可以继承spring-boot-starter-parent项目,来获取最佳依赖。这个父项目提供了以下几个功能:

默认Java 1.6编译
UTF-8编码格式
依赖管理部分,可让你对公共依赖省略version标签。继承自spring-boot-dependencies POM。
良好的资源过滤
良好的插件配置
对于application.properties和application.yml包括profile-specific文件,良好的资源过滤
最后一点:因为默认配置文件接受Spring风格的占位符(${}),Maven过滤器换成了@...@占位符。(可以通过Maven属性resource.delimiter替换)

2继承starter parent

配置继承spring-boot-starter-parent:

1 <parent>
2         <groupId>org.springframework.boot</groupId>
3         <artifactId>spring-boot-starter-parent</artifactId>
4         <version>2.1.3.RELEASE</version>
5         <relativePath/> <!-- lookup parent from repository -->
6 </parent>

只需要在这里指定Spring Boot的版本号。如果导入其他的starters,你可以完全省略版本号。

使用这个配置,你还可以通过property覆盖内部的依赖。例如,在pom.xml中升级Spring Data release train

<properties><spring-data-releasetrain.version>Fowler-SR2</spring-data-releasetrain.version></properties>

可以通过spring-boot-dependencies pom,查看支持的属性列表。

不使用parent POM,配置Spring Boot
可能有人不喜欢继承spring-boot-starter-parent POM。你可能有自己的企业标准parent,或者你可能只是比较喜欢明确声明所有的Maven配置。

如果你不想使用spring-boot-starter-parent,你依然可以通过使用scope=import利用依赖管理的便利:

 <dependencyManagement>
       <dependencies>
         <dependency>
              <!-- Import dependency management from Spring Boot -->
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-dependencies</artifactId>
              <version>2.1.3.RELEASE</version>
              <type>pom</type>
              <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>

这种方式不能使用property的形式覆盖原始的依赖项。要达到同样的效果,需要在dependencyManagement里面的spring-boot-dependencies之前添加依赖的东西。例如,要升级Spring Data release train,pom.xml应该是这样的:

 <dependencyManagement>
      <dependencies>
          <!-- Override Spring Data release train provided by Spring Boot -->
          <dependency>
              <groupId>org.springframework.data</groupId>
              <artifactId>spring-data-releasetrain</artifactId>
              <version>Fowler-SR2</version>
              <scope>import</scope>
              <type>pom</type>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-dependencies</artifactId>
             <version>2.1.3.RELEASE</version>
             <type>pom</type>
             <scope>import</scope>
         </dependency>
     </dependencies>
 </dependencyManagement>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵牛刘先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值