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

OpenRewrite:recipeList中前置Recipe执行依赖问题排查

解决OpenRewrite Recipe中变量更新后未生效的问题

问题原因

你遇到的问题源于OpenRewrite对Maven项目有效模型的解析时机:

  • 初始执行Recipe时,Maven项目的有效模型基于pom.xml原始状态构建,此时my-libs.version仍是旧值4.3.5
  • 虽然ChangePropertyValue修改了pom.xml中的属性,但OpenRewrite不会自动重新计算有效模型,后续AddManagedDependency仍会使用初始解析的旧变量值,导致尝试下载不存在的4.3.5版本BOM

解决方案

方案1:直接指定BOM版本(最可靠)

放弃依赖变量,在AddManagedDependency中直接硬编码BOM的目标版本5.0.0,既确保下载正确版本的BOM,也完成了版本变量的更新,后续其他依赖引用my-libs.version时会使用新值。

修改后的Recipe:

type: specs.openrewrite.org/v1beta/recipe
name: my.company.recipe.UpgradeInternalComponents
displayName: UpgradeInternalComponents
description: UpgradeInternalComponents
recipeList:
  - org.openrewrite.maven.ChangePropertyValue:
      key: my-libs.version
      newValue: 5.0.0 # 原版本为4.3.5
  - org.openrewrite.maven.AddManagedDependency:
      groupId: my.company
      artifactId: my-libs-bom # 5.0.0版本新增的BOM
      version: 5.0.0
      type: pom
      scope: import
      addToRootPom: true

方案2:强制重新解析属性(可选)

如果坚持使用变量,可在修改属性后添加ResolveProperties步骤,强制OpenRewrite重新解析Maven属性,确保后续步骤获取更新后的变量值:

type: specs.openrewrite.org/v1beta/recipe
name: my.company.recipe.UpgradeInternalComponents
displayName: UpgradeInternalComponents
description: UpgradeInternalComponents
recipeList:
  - org.openrewrite.maven.ChangePropertyValue:
      key: my-libs.version
      newValue: 5.0.0 # 原版本为4.3.5
  - org.openrewrite.maven.ResolveProperties:
  - org.openrewrite.maven.AddManagedDependency:
      groupId: my.company
      artifactId: my-libs-bom # 5.0.0版本新增的BOM
      version: ${my-libs.version}
      type: pom
      scope: import
      addToRootPom: true

注意:该方法依赖ResolveProperties的执行效果,部分场景下可能仍存在模型未及时更新的问题,因此方案1的硬编码方式更稳定。

内容的提问来源于stack exchange,提问作者Rodrigo Carvalho Silva

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 13:51:17