如何通过Maven display-dependency-updates获取Jar包的真实最新版本?
解决versions-maven-plugin误识别老旧时间戳版本的问题
这个问题确实挺恼人的——老旧依赖的时间戳版本号干扰了versions插件的版本判断逻辑,不过有两种可行的方案能解决这个问题,让插件识别到真实的最新稳定版本:
方案一:直接在插件配置中排除特定版本模式
你可以通过excludes配置项,用正则表达式匹配并排除那些时间戳格式的版本号。比如针对commons-beanutils的情况,在项目的pom.xml中添加如下插件配置:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <!-- 建议使用最新版本的插件,功能更完善 --> <configuration> <!-- 排除所有类似20030211.134440的时间戳版本 --> <excludes> <exclude>commons-beanutils:commons-beanutils:*[0-9]{8}\.[0-9]{6}</exclude> </excludes> </configuration> </plugin> </plugins> </build>
这个正则表达式会精准匹配8位数字.6位数字格式的版本号,把这类老旧的时间戳版本从插件的更新候选列表中剔除。
方案二:使用版本规则文件定义自定义判断逻辑
如果需要更灵活的版本过滤规则(比如同时处理多个依赖的异常版本),可以创建一个版本规则文件(比如version-rules.xml),放在项目根目录下:
<ruleset xmlns="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://mojo.codehaus.org/versions-maven-plugin/rule/2.0.0 http://mojo.codehaus.org/versions-maven-plugin/xsd/rule-2.0.0.xsd"> <!-- 针对commons-beanutils的规则 --> <rule groupId="commons-beanutils" artifactId="commons-beanutils"> <ignoreVersions> <ignoreVersion type="regex">^[0-9]{8}\.[0-9]{6}$</ignoreVersion> </ignoreVersions> </rule> <!-- 可以添加更多依赖的规则 --> </ruleset>
然后在pom.xml的插件配置中引用这个规则文件:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <configuration> <rulesUri>file://${project.basedir}/version-rules.xml</rulesUri> </configuration> </plugin>
额外建议
- 优先升级
versions-maven-plugin到最新稳定版本:新版本的插件在版本比较逻辑上做了更多优化,对这类异常版本号的兼容性更好。 - 如果是团队共享的规则,可以把规则文件放在内部仓库,通过HTTP URI引用,方便团队统一维护。
内容的提问来源于stack exchange,提问作者Christophe Tela
相关产品推荐
相关产品推荐

