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

OpenRewrite Refaster模板Recipe无法被发现及使用的问题求助

OpenRewrite Refaster Recipe 无法被mvn rewrite:discover/run识别的问题

问题概述

  • 使用OpenRewrite的Refaster式开发了一个调整运算顺序的模板类,目的是让除法在乘法之后执行以提升计算精度
  • mvn compile执行成功,生成的Recipe类已存在于target/generated-sources/annotations目录
  • 该Recipe在OpenRewrite测试用例中可正常使用,但执行mvn rewrite:discover或mvn rewrite:run时,报错提示找不到对应Recipe类

相关信息

典型错误日志

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.40.0:discover (default-cli) on project my-project: Unable to find recipe with name 'com.example.MyAdjustOrderRecipe'

Refaster模板类代码示例

package com.example;

import org.openrewrite.java.refaster.RefasterTemplate;
import org.openrewrite.java.refaster.annotation.AfterTemplate;
import org.openrewrite.java.refaster.annotation.BeforeTemplate;

public class AdjustOperationOrderTemplate {
    @BeforeTemplate
    public double divideBeforeMultiply(double a, double b, double c) {
        return (a / b) * c;
    }

    @AfterTemplate
    public double multiplyBeforeDivide(double a, double b, double c) {
        return (a * c) / b;
    }
}

pom.xml关键配置片段

<build>
    <plugins>
        <!-- OpenRewrite Maven Plugin -->
        <plugin>
            <groupId>org.openrewrite.maven</groupId>
            <artifactId>rewrite-maven-plugin</artifactId>
            <version>5.40.0</version>
            <configuration>
                <activeRecipes>
                    <recipe>com.example.MyAdjustOrderRecipe</recipe>
                </activeRecipes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.openrewrite.recipe</groupId>
                    <artifactId>rewrite-java</artifactId>
                    <version>2.4.0</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- Compiler with Refaster Annotation Processor -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
            <configuration>
                <source>17</source>
                <target>17</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.openrewrite</groupId>
                        <artifactId>rewrite-refaster</artifactId>
                        <version>7.40.0</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

排查与解决步骤

  1. 校验生成类的全限定名
    检查target/generated-sources/annotations下生成的Recipe类的包路径和类名,确保与activeRecipes中配置的完全一致(注意大小写、包名拼写)

  2. 确保生成目录被识别为源码目录
    Maven默认可能不会将target/generated-sources/annotations标记为源码根目录,导致rewrite插件无法加载生成的类。添加build-helper-maven-plugin显式声明该目录:

    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.4.0</version>
        <executions>
            <execution>
                <id>add-generated-sources</id>
                <phase>generate-sources</phase>
                <goals>
                    <goal>add-source</goal>
                </goals>
                <configuration>
                    <sources>
                        <source>target/generated-sources/annotations</source>
                    </sources>
                </configuration>
            </execution>
        </executions>
    </plugin>
    
  3. 清理缓存后重新构建
    执行mvn clean compile彻底清理旧构建产物,再运行mvn rewrite:discover,避免缓存中的无效类或路径干扰

  4. 检查版本兼容性
    确认rewrite-refaster、rewrite-maven-plugin和rewrite-java的版本是否兼容,建议使用OpenRewrite官方推荐的配套版本,避免因版本不匹配导致生成的Recipe无法被插件识别

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 02:47:17