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

使用rewrite-maven-plugin改写第三方项目的问题咨询

解决Maven多模块下Rewrite插件的两个问题

针对你遇到的两个问题,给你具体的解决方案:

1. 限制Rewrite插件仅作用于当前模块

不需要找专门的goal,直接在当前模块的pom.xml里单独配置Rewrite插件,同时设置<inherited>false</inherited>避免配置被其他子模块继承。另外不要在父pom中绑定runNoFork,只在需要修改第三方源码的子模块里将其绑定到generate-sources阶段,插件就只会处理当前模块:

<plugin>
  <groupId>org.openrewrite.maven</groupId>
  <artifactId>rewrite-maven-plugin</artifactId>
  <version>你的插件版本</version>
  <inherited>false</inherited> <!-- 关键:不继承给其他模块 -->
  <executions>
    <execution>
      <id>rewrite-3rdparty-code</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>runNoFork</goal>
      </goals>
      <configuration>
        <!-- 你的recipe配置 -->
      </configuration>
    </execution>
  </executions>
</plugin>

2. 让Rewrite插件扫描添加的源码目录

这不是插件bug,只是默认行为需要调整。Rewrite插件默认不会自动包含所有Maven注册的源码目录,你可以通过显式配置<sourceDirectories>,引用Maven内置的${project.sources}变量,就能获取所有已注册的源码目录(包括通过build-helper-plugin添加的target/generated-sources/3rdparty/):

<plugin>
  <!-- 插件基本信息 -->
  <configuration>
    <sourceDirectories>${project.sources}</sourceDirectories> <!-- 关键:扫描所有项目源码目录 -->
    <activeRecipes>
      <!-- 你的recipe全类名 -->
    </activeRecipes>
  </configuration>
</plugin>

另外要确保build-helper-plugin的add-source goal绑定在generate-sources阶段的早于Rewrite插件执行的时机,比如绑定到process-sources阶段,保证源码目录在Rewrite执行前就已经被Maven注册:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>你的插件版本</version>
  <executions>
    <execution>
      <id>add-3rdparty-source</id>
      <phase>process-sources</phase>
      <goals>
        <goal>add-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>target/generated-sources/3rdparty/</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 02:33:15