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

OpenRewrite API问题:项目根目录下YAML recipe无法生效

OpenRewrite根目录YAML Recipe失效问题

问题概述

使用OpenRewrite API处理YAML文件时,项目根目录下的.yaml文件无法被YAML Recipe(如DeleteKey、MergeYaml)修改,但将这些文件移到子目录后就能正常生效。当前需要处理的是多个.yaml文件位于根目录的项目。

目录结构示例:

project-root-directory
│
├── .yaml files (YAML recipes do not work)
│
├── subdirectory
│   ├── .yaml files (YAML recipes work)
│
├── src
│   ├── main
│   │   ├── java
│   │   ├── resources
│   ├── test
│
├── pom.xml

排查与解决方法

1. 调整Maven插件的文件包含规则

OpenRewrite Maven插件默认不会扫描根目录下的非标准位置文件,需在pom.xml的插件配置中显式添加根目录.yaml文件的包含规则:

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>你的插件版本</version>
    <configuration>
        <includes>
            <include>*.yaml</include>
            <include>*.yml</include>
            <!-- 保留子目录规则(若需同时处理) -->
            <include>subdirectory/*.yaml</include>
        </includes>
        <!-- 其他配置如指定recipes等 -->
    </configuration>
</plugin>

2. 自定义Recipe的文件匹配范围

如果是自定义Recipe,需要确保文件选择器覆盖根目录的YAML文件,示例如下:

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
    // 匹配所有yaml/yml文件,包含根目录
    return Preconditions.check(
        new FileMatcher("**/*.yaml", "**/*.yml"),
        // 替换为你实际使用的Recipe,比如DeleteKey
        new DeleteKey<>("目标key路径").getVisitor()
    );
}

3. 检查Rewrite配置文件的路径覆盖

若使用rewrite.yml作为配置源,确认其中未限制文件扫描范围,或添加显式匹配规则:

---
type: specs.openrewrite.org/v1beta/recipe
name: com.yourorg.YourRecipe
displayName: YAML文件修改Recipe
recipeList:
  - org.openrewrite.yaml.DeleteKey:
      key: target.key.path
    # 指定匹配所有yaml文件
    fileMatcher: "**/*.yaml"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 22:08:24