使用maven为eclipse编译器设置Java 6注解处理配置 [英] Setup Java 6 annotation processing configuration for eclipse compiler with maven

查看:30
本文介绍了使用maven为eclipse编译器设置Java 6注解处理配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What's the best way to setup the eclipse project compiler configuration for Java 6 annotation processors?

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

My solution is to setup the org.eclipse.jdt.apt.core.prefs and factorypath files manually. This is a bit cumbersome:

  • 在 factorypath 文件中引用处理器 jar
  • 配置eclipse注解处理器输出目录(org.eclipse.jdt.apt.core.prefs中的org.eclipse.jdt.apt.genSrcDir属性)
  • 将eclipse注解处理器输出目录添加为源文件夹
  • Reference the processor jar in the factorypath file
  • Configure the eclipse annotation processor output directory (org.eclipse.jdt.apt.genSrcDir property in org.eclipse.jdt.apt.core.prefs)
  • Add the eclipse annotation processor output directory as source folder

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

One problem is that eclipse generated sources will be compiled with maven. Only maven clean compile is reliable as it removes the eclipse generated source files. (Eclipse and javac generated source files could be out of sync.)

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

Is there are better solution to configure maven without eclipse generated source files at the maven source path?

<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>

推荐答案

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

Update: You could try using the apt-maven-plugin. It currently provides three goals:

  • apt-process Executes apt on project sources.
  • apt:test-process Executes apt on project test sources.
  • apt:eclipse Generates Eclipse files for apt integration.

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

You can configure the goals to run as part of your build as follows:

<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>

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

By default the output directory is set to ${project.build.directory}/generated-sources/apt,

有一个 open Jira 对抗编译器插件来添加对 Java 的 APT 支持6、如果你想在以后的版本中看到它,你可以去投票.

There is an open Jira against the compiler plugin to add APT support for Java 6, you can go and vote for it if this is something you want to to see in future versions.

这篇关于使用maven为eclipse编译器设置Java 6注解处理配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆