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

定义多个激活Rewrite Recipe时的执行顺序及强制指定方法问询

OpenRewrite Recipe执行顺序问题解答

执行顺序是否随机?

不是随机的。OpenRewrite的Recipe执行顺序由以下规则决定:

  • 优先处理存在依赖关系的Recipe:若Recipe A声明依赖Recipe B,则B会先于A执行。
  • 无依赖关系的Recipe,默认按类名的字典序排序执行。你的自定义Recipe类名以com开头,官方Recipe以org开头,字典序中com早于org,所以无论你在activeRecipe列表中怎么调整顺序,自定义Recipe都会先执行。

如何强制让自定义Recipe先执行?

如果你需要确保自定义Recipe始终优先执行(避免后续因其他Recipe引入导致顺序变化),有两种可靠方式:

方式1:使用Gradle插件的recipeOrder配置

直接在init.gradle中显式指定执行顺序,覆盖默认排序逻辑:

rewrite {
  activeRecipe("org.openrewrite.java.testing.mockito.Mockito1to4Migration", "com.myorg.openrewrite.Mockito1to4Migration")
  // 强制指定顺序,自定义Recipe在前
  recipeOrder = ["com.myorg.openrewrite.Mockito1to4Migration", "org.openrewrite.java.testing.mockito.Mockito1to4Migration"]
}

方式2:创建组合Recipe

编写一个自定义的组合Recipe,在其中明确指定执行顺序,然后仅激活这个组合Recipe:

  1. 编写组合Recipe类:
package com.myorg.openrewrite;

import org.openrewrite.Recipe;
import org.openrewrite.java.testing.mockito.Mockito1to4Migration;

public class OrderedMockitoMigration extends Recipe {
    @Override
    public String getDisplayName() {
        return "有序的Mockito 1到4迁移";
    }

    @Override
    public String getDescription() {
        return "先执行自定义Mockito迁移逻辑,再执行官方迁移逻辑";
    }

    @Override
    public void configure(Context context) {
        // 按顺序添加要执行的Recipe
        doNext(new com.myorg.openrewrite.Mockito1to4Migration());
        doNext(new Mockito1to4Migration());
    }
}
  1. 修改init.gradle配置:
rewrite {
  activeRecipe("com.myorg.openrewrite.OrderedMockitoMigration")
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 16:10:28