使用OpenRewrite执行dryRun时POM解析失败:未知仓库访问异常
问题描述
配置OpenRewrite工具后执行dryRun,出现POM解析错误,提示无法访问http://snapshots.repository.codehaus.org,抛出java.net.UnknownHostException异常。未使用Nexus仓库,已尝试配置官方推荐的HTTPS镜像https://oss.sonatype.org/content/repositories/snapshots,但IDE内和外部执行dryRun/运行recipes均无效,仅测试项目可正常运行。当前插件配置如下:
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.29.0</version> <configuration> <activeRecipes> <recipe>org.openrewrite.java.spring.boot3.UpgradeSpringBoot_3_2</recipe> <recipe>org.openrewrite.java.migrate.UpgradeToJava21</recipe> </activeRecipes> </configuration> <dependencies> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-java-dependencies</artifactId> <version>1.7.0</version> </dependency> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>5.8.0</version> </dependency> </dependencies> </plugin>
解决步骤
替换项目内的HTTP仓库配置
当前项目或父POM中可能仍引用了http://snapshots.repository.codehaus.org这个已废弃的HTTP仓库,需全局替换所有POM中的该地址为HTTPS镜像:<repositories> <repository> <id>codehaus-snapshots</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>codehaus-snapshots</id> <url>https://oss.sonatype.org/content/repositories/snapshots</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories>配置Maven全局镜像规则
在Maven的settings.xml(路径通常为~/.m2/settings.xml或Maven安装目录下的conf/settings.xml)中添加镜像,强制将对codehaus快照仓库的请求转发到HTTPS地址:<mirrors> <mirror> <id>codehaus-snapshots-mirror</id> <mirrorOf>codehaus-snapshots</mirrorOf> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </mirror> </mirrors>清理本地仓库缓存
本地仓库可能缓存了旧的HTTP仓库元数据,执行以下命令强制更新依赖:mvn clean install -U升级OpenRewrite相关依赖版本
旧版本插件可能存在仓库解析的兼容性问题,尝试升级到最新稳定版:- 将
rewrite-maven-plugin版本调整为5.x最新稳定版(如5.40.0) - 将
rewrite-spring版本调整为对应兼容版本(如5.15.0)
- 将
排查项目有效配置
执行命令查看项目的有效POM配置,定位引入无效仓库的来源:mvn help:effective-pom在输出中搜索
http://snapshots.repository.codehaus.org,找到具体的依赖或父POM后针对性修改。
内容的提问来源于stack exchange,提问作者Mentee

