You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用OpenRewrite执行JUnit4to5Migration时依赖库缺失问题

问题:OpenRewrite JUnit4转JUnit5迁移报错,找不到指定依赖

环境

  • 多模块Maven项目
  • Maven 3.99、Java 21

配置的OpenRewrite插件

根pom中配置如下:

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>6.21.0</version>
    <configuration>
        <exportDatatables>true</exportDatatables>
        <activeRecipes>
            <recipe>org.openrewrite.java.testing.junit5.JUnit4to5Migration</recipe>
        </activeRecipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-testing-frameworks</artifactId>
            <version>3.19.0</version>
        </dependency>
    </dependencies>
</plugin>

报错信息

项目编译成功后,OpenRewrite执行终止,抛出如下堆栈:

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.16.0:run (default-cli) on project locs-rest-api-client: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.16.0:run failed: Error while visiting model\src\test\java\de\mvbonline\locs\adb\IsbnRangeIntegrationTest.java: java.lang.IllegalArgumentException: Unable to find classpath resource dependencies beginning with: 'apiguardian-api-1.1', 'junit-jupiter-api-5'
[ERROR]   org.openrewrite.java.JavaParser.dependenciesFromResources(JavaParser.java:139)
[ERROR]   org.openrewrite.java.JavaParser$Builder.classpathFromResources(JavaParser.java:316)
[ERROR]   org.openrewrite.java.testing.junit5.UpdateTestAnnotation$UpdateTestAnnotationVisitor.visitMethodDeclaration(UpdateTestAnnotation.java:114)
[ERROR]   org.openrewrite.java.testing.junit5.UpdateTestAnnotation$UpdateTestAnnotationVisitor.visitMethodDeclaration(UpdateTestAnnotation.java:57)
[ERROR]   org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:4036)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:60)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1310)
[ERROR]   org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:392)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
[ERROR]   org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:391)
[ERROR]   org.openrewrite.java.JavaIsoVisitor.visitBlock(JavaIsoVisitor.java:88)
[ERROR]   org.openrewrite.java.JavaIsoVisitor.visitBlock(JavaIsoVisitor.java:30)
[ERROR]   org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
[ERROR]   ...

尝试过的无效操作

将apiguardian-api和junit-jupiter-api添加到项目或插件依赖中,无论是否设置test作用域,报错信息均无变化。项目本身并未使用apiguardian,疑问该迁移Recipe是否强制依赖它,以及操作中是否有遗漏或错误。

解决方法(编辑补充)

执行以下操作后问题解决:

  • 升级OpenRewrite插件版本
  • 配置IntelliJ使用@argfile缩短类路径
  • 多次刷新、同步和重新加载项目

不确定具体哪项操作直接解决了问题,但推测是IntelliJ Maven插件相关的类路径问题,无需额外添加依赖。


内容的提问来源于stack exchange,提问作者Uwe Allner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 04:03:14