首页 > 编程技术 > java

maven 指定version不生效的问题

发布时间:2022-1-18 16:54 作者:zx1323

maven 指定version不生效

在项目中依赖一个 spring-security 版本时, 发现未生效, 调用报错

由于项目是依赖了srpingBoot

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

只要加入spring-security的依赖, 哪怕指定了版本, 也会使用springBoot中的版本

找了半天, 发现把security的版本依赖放在maven的 dependencyManagement标签中即可

有关maven依赖中的version

构件依赖解析机制

1、首先判断依赖的范围是否为system,如果是system,直接从本地文件系统解析构件;

2、非system,需要根据依赖坐标计算仓库路径,然后先从本地仓库寻找构件,找到构件解析成功;

3、如果本地仓库找不到,再判断版本号是否为明确版本号,如果版本号明确,会从远程仓库下载相应版本的构件;

4、如果版本号不明确,如 RELEASE、LATEST 和 SNAPSHOT,Maven 就需要根据远程仓库更新策略来检查更新

通过配置或命令行参数去设置远程仓库检查更新的策略

配置方式:

<repository> 
    <id>xxx-snapshots</id>  
    <url>https://xxx/repository/maven-xxx-snapshots/</url>  
    <releases> 
        <enabled>true</enabled>
    </releases>  
    <snapshots> 
        <enabled>true</enabled>  
        <updatePolicy>always</updatePolicy> 
    </snapshots> 
</repository> 

最新快照版本:

即使本地仓库已缓存,仍然会根据更新策略去远程仓库检查构件是否更新,然后下载最新时间戳的构件。

当maven 检测到需要依赖一个Snashot版本时,就会尝试从所有的远程仓库下载对应的meta文件,并在与本地的meta文件进行merge,得到一个与{0.1.0-SNAPSHOT}并带有时间戳的最新的版本号{0.1.0-20210910.091638-3},然后再尝试下载该版本。

<dependency>
     <groupId>com.etoak</groupId>
     <artifactId>test</artifactId>
     <version>SNAPSHOT</version>
 </dependency>

最新发布版本:

如果本地仓库没有缓存,会去远程仓库获取;如果本地仓库已缓存,即使远程仓库同一版本号有更新,也不再去远程仓库获取。

<dependency>
     <groupId>com.etoak</groupId>
     <artifactId>test</artifactId>
     <version>RELEASE</version>
 </dependency>

最新版本:

下载最新的SNAPSHOT或者最新的RELEASE,不推荐使用。

<dependency>
     <groupId>com.etoak</groupId>
     <artifactId>test</artifactId>
     <version>LATEST</version>
 </dependency>

指定依赖的版本范围:

<dependency>
     <groupId>com.etoak</groupId>
     <artifactId>test</artifactId>
     <version>[1.0.1,)</version>
 </dependency>

范围说明:

RangeMeaning
1.0x >= 1.0 * The default Maven meaning for 1.0 is everything (,) but with 1.0 recommended. Obviously this doesn't work for enforcing versions here, so it has been redefined as a minimum version.
(,1.0]x <= 1.0
(,1.0)x < 1.0
[1.0]x == 1.0
[1.0,)x >= 1.0
(1.0,)x > 1.0
(1.0,2.0)1.0 < x < 2.0
[1.0,2.0]1.0 <= x <= 2.0
(,1.0],[1.2,)x <= 1.0 or x >= 1.2. Multiple sets are comma-separated
(,1.1),(1.1,)x != 1.1

以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。

原文出处:https://blog.csdn.net/zx1323/article/details/112571698

标签:[!--infotagslink--]

您可能感兴趣的文章: