Hey, Nice to meet You. 

必有过人之节.人情有所不能忍者,匹夫见辱,拔剑而起,挺身而斗,此不足为勇也,天下有大勇者,猝然临之而不惊,无故加之而不怒.此其所挟持者甚大,而其志甚远也.          ☆☆☆所谓豪杰之士,

Maven工具学习(六)----Maven依赖的版本锁定与版本常量

1、版本的管理

在Maven中对依赖版本的管理暂且有两种方式:

  • 版本锁定
  • 版本常量

注意:这两种方式在实际的开发中会经常使用

2、版本锁定

版本锁定:指的是锁定项目中依赖的版本。这种是目前实际项目中使用的最多的。版本锁定需要使用到dependencyManagement元素。需要说明的是dependencyManagement仅仅起到指锁定依赖版本的作用,其它的作用它什么都没有。而真正依赖包的下载任然要定义在dependencie元素中。

<!--锁定依赖的版本,它仅仅是起到锁定作用,不下载依赖包-->
<dependencyManagement>
  <dependencies>
	<!-- Junit单元测试依赖 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
      
    <!-- Spring的相关依赖 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
 
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>
  </dependencies>
</dependencyManagement>
 
<!--依赖包的下载仍然由dependencies管理-->
<!--只有dependencies中定义了依赖才会下载jar包-->
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <!--<version>4.11</version>-->
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <!--<version>4.1.6.RELEASE</version>-->
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.6.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <!--<version>4.1.6.RELEASE</version>-->
  </dependency>
</dependencies>

注意:当我们使用dependencyManagement元素来锁定依赖版本后,dependencie元素中的依赖版本(version)可写,也可不写,这样依赖版本引入则就有两种不同的方式了:

  1. 在dependencies中的依赖中如果没有声明依赖的版本,就到dependenciesManage中去找,找到就使用锁定的版本号,没有就报错。
  2. 在dependencies中声明了依赖的版本,则使用该依赖的版本,不管在dependenciesManage中有没有声明依赖的版本,都以dependencies中声明的版本为主。

dependencies和dependencyManagement的区别:

  • dependencies是真正引入依赖的元素。而且在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)。
  • dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的在dependencies中声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的依赖版本。

3、版本常量

版本常量是在pom.xml文件中提取出各自依赖的版本常量。封装至properties元素中,然后在依赖的version元素中用OGNL表达式获取即可,版本常量方式用的也很多。

这种方式方便了项目版本依赖管理的统一和后面的升级,而且它还具有继承性,在父项目pom.xml文件中定义的版本常量,在子模块中的pom.xml文件也能使用。

<!--定义版本常量-->
<properties>
  <spring.version>5.2.6.RELEASE</spring.version>
  <junit.version>4.11</junit.version>
</properties>
 
<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <!--使用版本常量-->
    <version>${junit.version}</version>
    <scope>test</scope>
  </dependency>
 
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
  </dependency>
</dependencies>
posted @ 2020-06-17 15:22  唐浩荣  阅读(969)  评论(0编辑  收藏  举报