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

是否有Open-Rewrite中移动同名配置文件的Recipe?求迁移方案

OpenRewrite 配置文件迁移 Recipe 实现方案

核心思路

针对固定名称的配置文件迁移需求,你可以通过自定义OpenRewrite Recipe实现:匹配目标文件名,修改其存储路径,同时可扩展处理代码/配置中对该文件的引用。

自定义迁移Recipe代码示例

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.SourceFile;
import org.openrewrite.TreeVisitor;
import org.openrewrite.maven.MavenIsoVisitor;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MoveTestConfigRecipe extends Recipe {
    // 固定配置文件名
    private static final String TARGET_FILE = "test.conf";
    // 新路径:相对于原文件所在目录的子路径
    private static final Path NEW_RELATIVE_PATH = Paths.get("v2", TARGET_FILE);

    @Override
    public String getDisplayName() {
        return "迁移test.conf至v2目录";
    }

    @Override
    public String getDescription() {
        return "将项目中的test.conf配置文件从原目录移动到v2子目录";
    }

    @Override
    public TreeVisitor<?, ExecutionContext> getVisitor() {
        return new MavenIsoVisitor<ExecutionContext>() {
            // 处理文件路径迁移
            @Override
            public SourceFile visitSourceFile(SourceFile sourceFile, ExecutionContext ctx) {
                Path currentPath = sourceFile.getSourcePath();
                // 匹配目标配置文件
                if (TARGET_FILE.equals(currentPath.getFileName().toString())) {
                    // 构建新的文件路径
                    Path newPath = currentPath.getParent().resolve(NEW_RELATIVE_PATH);
                    // 更新文件存储路径
                    return sourceFile.withSourcePath(newPath);
                }
                return super.visitSourceFile(sourceFile, ctx);
            }

            // 可选:更新代码/配置中对该文件的引用路径
            @Override
            public String visitString(String s, ExecutionContext ctx) {
                String oldRef = "test.conf";
                String newRef = "v2/test.conf";
                if (s.contains(oldRef)) {
                    return s.replace(oldRef, newRef);
                }
                return super.visitString(s, ctx);
            }
        };
    }
}

使用方式

  1. 将上述Recipe打包为JAR,添加到项目构建插件的依赖中(以Maven为例):
<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>5.40.0</version>
    <configuration>
        <recipes>
            <!-- 引用自定义Recipe -->
            <recipe>com.yourpackage.MoveTestConfigRecipe</recipe>
        </recipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>com.yourpackage</groupId>
            <artifactId>your-rewrite-recipes</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>
</plugin>
  1. 执行OpenRewrite迁移命令:
mvn rewrite:run

扩展说明

  • 如果你的项目使用Gradle,只需对应调整插件配置,逻辑完全一致
  • 若存在其他类型的引用(如XML配置中的文件路径),可在Visitor中添加对应节点的处理逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 23:10:11