如何在保存时在Eclipse中运行注释处理器 [英] How to run annotation processor in eclipse on save

查看:183
本文介绍了如何在保存时在Eclipse中运行注释处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我通过

Right click on project > Run As > Maven Clean
Right click on project > Run As > Maven install

这是非常耗时的.如何设置eclipse以使其在保存时运行注释处理器?

This is quite time consuming. How do I set up eclipse to make it run the annotation processor on save?

我设置了自动构建"功能,但似乎忽略了注释处理器.顺便说一句,我在使用m2e apt插件并带有自动配置激活的JDT APT".

I have the "Build Automatically" feature set but it seems to ignore the annotation processors. BTW I am using m2e apt plugin with "Automatically configure JDT APT activated".

推荐答案

我在Eclipse中为我的一些项目进行了批注处理.对我而言,它可以保存,并且不必mvn install(它与Maven的工作方式不同,因为Eclipse运行自己的编译器).

I have annotation processing working in Eclipse for some of my projects; for me, it IS working on save, and I don't have to mvn install (and it works differently than Maven, as Eclipse runs its own compiler).

我也在为此使用m2e-apt插件. 如上所述,Eclipse运行自己的编译器.这意味着它的输出可能与Maven的输出略有不同(当您右键单击项目>运行方式> Maven清理/安装"时,您是在调用Maven,而不是Eclipse).我之所以提及这一点,是因为您的处理器很可能在Maven中有问题并且可以在Eclipse中工作(尽管在大多数情况下它们确实产生相同的输出;我看到了一些区别,但很小).如果您是我,我将密切注意Eclipse的错误日志(因为这就是编写注释处理错误的地方).

I'm also using m2e-apt plugin for this. As noted above, Eclipse runs its own compiler; that means that its output can differ slightly than Maven's (when you "Right click on project > Run As > Maven Clean / Install" you're invoking Maven, not Eclipse). I'm mentioning this because it is entirely possible that your processors have a problem and work in Maven but not in Eclipse (although most of the time they do produce the same output; I've seen some differences, but very small). I'd keep an eye on Eclipse's error log if I were you (because that's where annotation processing errors are written).

这就是我的建议:

  • 使用Eclipse中的Maven/注释处理设置发布图片(即使您似乎确实激活了正确的选项).
  • 发布具有Java/Compiler设置的图片(其中有一个选中标记需要激活;没有它就无法使用).
  • 奇怪的是,发布您的pom.xml会有所帮助.特别是如果您具有maven-compiler-plugin的自定义配置.某些配置由m2e-apt解释,例如编译器参数.
  • 查找名为.factorypath的文件.这是m2e-apt保留扫描的jar列表以进行批注处理的地方(即使它们实际上没有包含处理器,也可以在其中找到项目的所有jar;也就是说,除非配置了maven-compiler-plugin因此仅考虑特定的处理器列表).如果包含处理器的jar不在.factorypath中,它将无法正常工作.
  • 最后但并非最不重要的是,还有另一件事会引起问题.如果包含实际注释处理器的项目(而不是客户端")与客户机"项目位于同一工作空间中,则m2e-apt将仅忽略注释处理器;我不知道为什么在这种情况下,关闭注释处理器项目就足够了(您不必从工作空间中删除它).
  • Post A picture with your Maven / Annotation Processing settings in Eclipse (even though you do seem to have the correct option activated).
  • Post a picture with Java/Compiler settings (there is a checkmark in there that needs to be activated; it doesn't work without).
  • Posting your pom.xml would, strangely, be helpful. Especially if you have custom configuration for maven-compiler-plugin. Some of that config is interpreted by m2e-apt, such as compiler arguments.
  • Look for a file called .factorypath. That's where m2e-apt keeps the list of jars that it scans for annotation processing (you'll find all the jars of your project in there, even though they don't actually contain processors; that is, unless your maven-compiler-plugin is configured as such to only consider a specific list of processors). If the jar containing your processor is not in .factorypath, it won't work.
  • Last but not least, there is another thing that can cause problems. If the project containing the actual annotation processor (so NOT the "client") is in the same workspace as the "client" project, then m2e-apt will simply ignore your annotation processor; I don't know why. Closing your annotation processor project would be enough in this case (you don't have to delete it from workspace).

忘了说,如果您通过Maven do 运行注释处理(并且您只是为了处理注释而调用Maven),那么mvn compile就足够了.另外,您不需要单独运行它(首先运行mvn clean然后运行mvn compile).您可以使用mvn clean compile一键运行它.它应该具有完全相同的效果.

Forgot to say that if you do run your annotation processing via Maven (and you're invoking Maven just to process annotations), then mvn compile should be enough. Also, you don't need to run it separately (first mvn clean then mvn compile). You can run it in one shot with mvn clean compile; it is supposed to have the exact same effect.

这篇关于如何在保存时在Eclipse中运行注释处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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