使用OpenRewrite升级Spring Boot2.5到2.7时遇NoSuchMethodError求助
问题描述
尝试使用OpenRewrite recipe将Spring Boot 2.5项目升级到2.7版本,多次执行均触发相同错误。已尝试排除报错模块,但错误会转移至下一个模块(项目共3个模块);删除本地.m2仓库后问题依旧。
现有Rewrite插件配置
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.2.4</version> <configuration> <activeRecipes> <recipe>org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7</recipe> </activeRecipes> <exclusions> <exclude>*/node/**</exclude> <exclude>*/node_modules/**</exclude> <exclude>*/target/**</exclude> </exclusions> </configuration> <dependencies> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>5.0.1</version> </dependency> </dependencies> </plugin>
执行mvn rewrite:run后的错误日志
[ERROR] The recipe produced an error. Please report this to the recipe author. [ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.2.4:run (default-cli) on project my-project: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.2.4:run failed: Error while visiting my-module/src/main/java/nl/commons/util/someClass.java: java.lang.NoSuchMethodError: 'boolean org.openrewrite.java.MethodMatcher.matches(org.openrewrite.java.tree.J$MethodInvocation)' [ERROR] org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito$PowerMockitoToMockitoVisitor.visitMethodInvocation(PowerMockitoMockStaticToMockito.java:164) [ERROR] org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito$PowerMockitoToMockitoVisitor.visitMethodInvocation(PowerMockitoMockStaticToMockito.java:48) [ERROR] org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3706) [ERROR] org.openrewrite.java.tree.J.accept(J.java:59) [ERROR] org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278) [ERROR] org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361) [ERROR] org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1284) [ERROR] org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:367)
解决方法
这个NoSuchMethodError是由于rewrite-maven-plugin和rewrite-spring依赖版本不兼容导致的。rewrite-maven-plugin:5.2.4与rewrite-spring:5.0.1之间存在API差异,触发了方法找不到的异常。
1. 调整为兼容的版本组合
将插件和依赖版本更新为以下兼容组合:
<plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.4.0</version> <configuration> <activeRecipes> <recipe>org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7</recipe> </activeRecipes> <exclusions> <exclude>*/node/**</exclude> <exclude>*/node_modules/**</exclude> <exclude>*/target/**</exclude> </exclusions> </configuration> <dependencies> <dependency> <groupId>org.openrewrite.recipe</groupId> <artifactId>rewrite-spring</artifactId> <version>5.1.1</version> </dependency> </dependencies> </plugin>
2. 执行后续操作
- 运行
mvn clean清理项目构建缓存 - 重新执行
mvn rewrite:run
备选方案
如果调整版本后仍有问题,可以尝试:
- 检查项目中是否存在其他rewrite相关依赖,确保所有依赖版本统一
- 临时排除PowerMockito转换的recipe,避免其干扰:在
<activeRecipes>中添加<exclude>org.openrewrite.java.testing.mockito.PowerMockitoMockStaticToMockito</exclude>
内容的提问来源于stack exchange,提问作者Niek7301
相关产品推荐
相关产品推荐

