问题描述
为 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.prefs
和 factorypath
文件.这有点麻烦:
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 inorg.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 在项目源上执行 apt.
- apt:test-process 执行 apt在项目测试源上.
- apt:eclipse 为 apt 集成生成 Eclipse 文件.
- 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注解处理配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!