使用maven为eclipse编译器设置Java 6注释处理配置

Tho*_*ung 16 java eclipse maven-2 annotations javac

为Java 6注释处理器设置eclipse项目编译器配置的最佳方法是什么?

我的解决方案是手动设置org.eclipse.jdt.apt.core.prefsfactorypath文件.这有点麻烦:

  • 引用factorypath文件中的处理器jar
  • 配置eclipse注释处理器输出目录(org.eclipse.jdt.apt.genSrcDir属性org.eclipse.jdt.apt.core.prefs)
  • 添加eclipse注释处理器输出目录作为源文件夹

一个问题是eclipse生成的源将使用maven编译.只有maven clean compile可靠,因为它删除了eclipse生成的源文件.(Eclipse和javac生成的源文件可能不同步.)

有没有更好的解决方案来配置maven没有eclipse生成的源文件在maven源路径?

<project>
  <properties>
    <eclipse.generated.src>${project.build.directory}/eclipse</eclipse.generated.src>
  </properties>
  <build>
      <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.4</version>
            <executions>
                <execution>
                  <id>add-source</id>
                  <phase>generate-sources</phase>
                  <goals> <goal>add-source</goal> </goals>
                  <configuration>
                      <sources>
                        <source>${eclipse.generated.src}</source>
                      </sources>
                    </configuration>
              </execution>
            </executions>
          </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <configuration>
          <additionalConfig>
            <file> <name>.factorypath</name>
        <content><![CDATA[<factorypath>
  <factorypathentry kind="VARJAR" id="M2_REPO/processor/processor.jar" enabled="true" runInBatchMode="false"/>
  </factorypath>
  ]]>      </content>
            </file>
            <file>
              <name>.settings/org.eclipse.jdt.apt.core.prefs</name>
        <content><![CDATA[
  eclipse.preferences.version=1
  org.eclipse.jdt.apt.aptEnabled=true
  org.eclipse.jdt.apt.genSrcDir=${eclipse.generated.src}
  org.eclipse.jdt.apt.reconcileEnabled=true
   ]]>     </content>
            </file>
          </additionalConfig>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
Run Code Online (Sandbox Code Playgroud)

Ric*_*ler 6

更新:您可以尝试使用apt-maven-plugin.它目前提供三个目标:

您可以将目标配置为作为构建的一部分运行,如下所示:

<build>
  ...
  <plugins>
    ...
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>apt-maven-plugin</artifactId>
      <version>1.0-alpha-2</version>
      <executions>
        <execution>
          <goals>
            <goal>process</goal>
            <goal>test-process</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    ...
  </plugins>
  ...
</build>
Run Code Online (Sandbox Code Playgroud)

默认情况下,输出目录设置为${project.build.directory}/generated-sources/apt,

对于编译器插件有一个开放的Jira来添加对Java 6的APT支持,如果你希望在将来的版本中看到它,你可以去投票.

  • 它不再开放,它是在2.2中实现的. (2认同)