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

OpenRewrite Gradle.ChangeDependency测试无变更问题求助

问题

我定义了如下Recipe:

- org.openrewrite.gradle.ChangeDependency:
      oldGroupId: org.x
      oldArtifactId: x-test
      newGroupId: org.y
      newArtifactId: y-test
      newVersion: 1.0.0

测试代码如下:

@Override
override fun defaults(spec: RecipeSpec) {
    val inputStream: InputStream = this::class.java.getResourceAsStream(RECIPE_FILE_NAME) as InputStream
    spec.typeValidationOptions(TypeValidation.none())
    spec.recipe(inputStream, RECIPE_NAME)
}

@Test
fun `should change dependencies`() {
    // given
    val before = """     
                dependencies {
                    testImplementation 'org.x:x-test:1.1.1'
                }
                """.trimIndent()
    val after = """
                dependencies {
                    testImplementation 'org.y:y-test:1.0.0'
                }
                """.trimIndent()
    // when/then
    rewriteRun(buildGradle(before, after))
}

但测试始终因未产生任何变更而失败,其他测试(如文本查找替换)可正常运行。

我已尝试的操作:

  • 为Recipe参数添加引号,如oldGroupId: 'org.x'
  • 尝试不同格式的before/after字符串适配Gradle语法
  • 指定源路径,如rewriteRun(buildGradle(before, after) { s -> s.path("build.gradle")})
解决思路
  1. 检查Recipe加载准确性
    确认RECIPE_FILE_NAME对应的YAML文件路径无误,且RECIPE_NAME与YAML中定义的Recipe名称完全一致。如果YAML未显式命名Recipe,直接使用spec.recipe(inputStream)即可,无需传入第二个参数。

  2. 显式指定依赖配置
    ChangeDependency默认匹配所有配置,但测试用例中明确使用了testImplementation,可在Recipe中添加configuration: testImplementation参数,精准锁定目标配置:

    - org.openrewrite.gradle.ChangeDependency:
        oldGroupId: org.x
        oldArtifactId: x-test
        newGroupId: org.y
        newArtifactId: y-test
        newVersion: 1.0.0
        configuration: testImplementation
    
  3. 验证Gradle语法解析结果
    可以在测试中临时添加日志或打印,输出解析后的AST结构,确认testImplementation 'org.x:x-test:1.1.1'被识别为Gradle依赖节点,而非普通文本节点。

  4. 精确匹配旧版本
    若未指定oldVersion,ChangeDependency默认匹配所有版本,但部分场景下需显式设置oldVersion: 1.1.1来精确匹配测试用例中的依赖,避免模糊匹配失效。

  5. 升级Rewrite版本
    旧版本的Rewrite Gradle插件可能存在ChangeDependency匹配bug,尝试升级到最新稳定版,确保插件与测试依赖的版本统一。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:35:08