如何在 Eclipse 中配置 Java 注释处理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43404891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to configure Java Annotation Processor(s) in Eclipse?
提问by Animesh
I have successfully run annotation processor for CLASS level retention annotations via command prompt compiling using Java 8.
我已经通过使用 Java 8 的命令提示符编译成功地运行了 CLASS 级别保留注释的注释处理器。
However, when I tried to configure the annotation processor in eclipse and tried to run it with "-proc:only"
option, it didn't get effective.
但是,当我尝试在 Eclipse 中配置注释处理器并尝试使用"-proc:only"
选项运行它时,它没有生效。
I have included the Jar file containing the custom annotation processor class file into the Project Properties -> Annotation Processing -> Factory Path
. I have also provided the -proc:only
option in Project Properties -> Annotation Processing -> Processor Options
, still the annotation processor isn't getting called when a class containing my annotation is executed.
我已将包含自定义注释处理器类文件的 Jar 文件包含到Project Properties -> Annotation Processing -> Factory Path
. 我还在 中提供了-proc:only
选项Project Properties -> Annotation Processing -> Processor Options
,当包含我的注释的类被执行时,注释处理器仍然没有被调用。
Please help me identifying the required setting or mistake or additional step for running the annotation processor via eclipse.
请帮助我确定通过 eclipse 运行注释处理器所需的设置或错误或附加步骤。
回答by sudhir shakya
I was finally able to enable my own annotation processor by adding my jar file.
我终于能够通过添加我的 jar 文件来启用我自己的注释处理器。
- Right click on the project and select Properties.
- Open Java Compiler -> Annotation Processing. Check "Enable annotation processing".
- Open Java Compiler -> Annotation Processing -> Factory Path. Check "Enable project specific settings". Add your JAR file to the list.
- Clean and build the project.
- 右键单击项目并选择属性。
- 打开 Java Compiler -> Annotation Processing。选中“启用注释处理”。
- 打开Java Compiler -> Annotation Processing -> Factory Path。选中“启用项目特定设置”。将您的 JAR 文件添加到列表中。
- 清理并构建项目。
And it finally worked.
它终于奏效了。
回答by Avinash Kadam
It is very much straightforward provided that whatever APT plug in you are trying to use is correctly configured.
只要您尝试使用的任何 APT 插件都已正确配置,这将非常简单。
Follow the steps mentioned in above post and the reference image to visualize it. Image showing QueryDsl APT configuration for maven project.
按照上面帖子中提到的步骤和参考图像对其进行可视化。显示 Maven 项目的 QueryDsl APT 配置的图像。
回答by Prabhu
While compiling via command prompt or terminal, you can see all the logs in same screen after the compilation command. In eclipse, these logs can be seen at
通过命令提示符或终端编译时,您可以在编译命令后在同一屏幕上看到所有日志。在eclipse中,这些日志可以在
Window->show view->General->Error Log
窗口->显示视图->常规->错误日志
If you want the IDE(Eclipse) to point out the warning, error or other diagnostic message exactly at the particular element in the code editor, we have to call the printMessagemethod of javax.annotation.processing.Messagerwith 3arguments as shown below.
如果您希望 IDE(Eclipse) 在代码编辑器中的特定元素准确指出警告、错误或其他诊断消息,我们必须使用3 个参数调用javax.annotation.processing.Messager的printMessage方法,如下所示.
messager.printMessage(Kind.ERROR, "Error Message", element);
messager.printMessage(Kind.ERROR, "错误信息", element);