当指定 fork=true 时,maven-compiler-plugin 不进行注释处理?

Joh*_*zen 5 annotations javac maven maven-compiler-plugin

maven-compiler-plugin在 Maven 项目中使用它对我的代码执行注释处理。在我添加配置选项之前它一直有效<fork>true</fork>

pom.xml文件具有以下内容:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.5.1</version>
  <dependencies>
    <!-- Add dependency on the annotation processor -->
    <dependency>
      <groupId>x.y.z</groupId>
      <artifactId>my-processor</artifactId>
      <version>1.0</version>
    </dependency>
  </dependencies>
  <configuration>
    <source>1.7</source>
    <target>1.7</target>
  </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

my -processor-1.0.jar文件包含META-INF/services/javax.annotation.processing.Processor文件,以便编译javac器在运行时可以发现它。

当我使用此配置运行mvn cleancompile时,我看到注释处理器运行并且生成的代码按预期放入target\ generated-sources\annotations目录中。

但是,如果我<fork>true</fork>向插件配置添加一个选项,那么我会观察到注释处理器不会运行,并且target\ generated-sources\annotations目录中不存在任何代码。

我在maven-compiler-plugin2.5.1、3.0 和 3.1 版本中尝试过此操作(对于 3.x 版本,我必须<forceJavaCompilerUser>true</forceJavaCompilerUser>在配置中添加一个选项,以便能够发现注释处理器 jar)。

我还尝试显式指定注释处理器:

<configuration>
  ...
  <annotationProcessors>
    <annotationProcessor>x.y.z.MyProcessor</annotationProcessor>
  </annotationProcessors>
  ...
</configuration>
Run Code Online (Sandbox Code Playgroud)

同样,对于版本 2.5.1、3.0 和 3.1,如果配置选项未指定分叉,则将调用注释处理器。当<fork>true</fork>指定选项时,注释处理器将不会运行。

我还在依赖项x.y.z:my_processor之外添加了依赖项maven-compiler-plugin,以确保加载注释处理器依赖项。

maven-compiler-plugin当配置了时,注释处理是否仍然可以工作<fork>true</fork>?或者我错误地配置了插件?

请注意,我不想将编译拆分为单独的执行(例如,一个执行在不进行注释处理的情况下进行编译,使用<fork>true</fork>,另一个执行仅进行注释处理,使用<fork>false</fork>,因为第二次执行再次重新编译整个源代码,这在以下情况下很糟糕处理数千个源文件,除非有办法解决这个问题)。

我使用的是 JDK 1.7.0_45。

编辑#1

实际上,解决方案是将处理器依赖项从插件依赖项移至正常依赖项:

<dependencies>
  <dependency>
    <groupId>x.y.z</groupId>
    <artifactId>my-processor</artifactId>
 </dependency>
 ...
</dependencies>

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
      <source>1.7</source>
      <target>1.7</target>
      <fork>true</fork>
    </configuration>
  </plugin>
</plugin>
Run Code Online (Sandbox Code Playgroud)

我以为我已经对此进行了测试,但我可能一直在寻找控制台输出(当进程分叉时不会出现),而不是寻找生成的代码是否存在。

Joh*_*zen 1

实际上,解决方案是将处理器依赖项从插件依赖项移至正常依赖项。请参阅原始帖子中的编辑#1。

尽管奇怪的是,当添加依赖项时与未添加依赖项时我会收到编译错误。如果没有依赖性,我会看到有关使用内部专有 API 的编译器警告。添加注释处理器依赖项后,该警告将被视为错误。我在编译器选项中看不到任何-Werror将警告视为错误的选项。当注释处理器依赖项被删除时,编译会通过,但会出现警告。为这个问题挠头……