maven强制更新release

1,509 阅读1分钟

背景

大家都知道,maven的snapshot是快照版本,根据updatepolicy能够一直更新,而release是发布版本,会优先从本地仓库查找,如果本地仓库找到了就不会从远程仓库下载,如果有需求强制更新本地仓库的release怎么实现呢?(虽然这个需求不合理)是不是只能删除本地仓库?

查资料

在中文资料搜索maven强制更新,只会得到-U选项能强制更新snapshot,不能更新release
直到打开Google,在baeldung.com搜到了mvn dependency:purge-local-repository -Dinclude=com.yyy.projectA:projectB -Dexclude=com.yyy.projectA:projectC命令,能够强制更新com.yyy.projectA:projectB的依赖,包括release

试验

以自己的项目测试mvn dependency:purge-local-repository clean package,在本地仓库已经缓存依赖的状态下打印了如下日志, 可见purge-local-repository删除了本地仓库的依赖,并从远程下载,优雅地实现了我们的需求.

--------------------< org.ypq:early-instantiation >---------------------
[INFO] Building early-instantiation 0.0.1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.2:purge-local-repository (default-cli) @ early-instantiation ---
[INFO] Deleting 35 transitive dependencies for project early-instantiation from C:\Users\Administrator\.m2\repository with artifact version resolution fuzziness
[INFO] Re-resolving dependencies
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-web/2.5.2/spring-boot-starter-web-2.5.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-web/2.5.2/spring-boot-starter-web-2.5.2.pom (3.0 kB at 1.4 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-web/2.5.2/spring-boot-starter-web-2.5.2.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter-web/2.5.2/spring-boot-starter-web-2.5.2.jar (4.8 kB at 10 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter/2.5.2/spring-boot-starter-2.5.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter/2.5.2/spring-boot-starter-2.5.2.pom (3.1 kB at 7.5 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter/2.5.2/spring-boot-starter-2.5.2.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/springframework/boot/spring-boot-starter/2.5.2/spring-boot-starter-2.5.2.jar (4.8 kB at 10 kB/s)

参考资料

www.baeldung.com/maven-force…
stackoverflow.com/questions/7…
maven.apache.org/plugins/mav…