如何在保存时在 Eclipse 中运行注释处理器

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

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

问题描述

目前我在eclipse中为一个项目生成带有注释处理器的文件

Currently I generate files with an annotation processor in eclipse for a project by

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?

我有自动构建"功能集,但它似乎忽略了注释处理器.顺便说一句,我正在使用带有自动配置 JDT APT 激活"的 m2e 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 运行您的注释处理(并且您调用 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 中运行注释处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在保存时在 Eclipse 中运行注释处理器

基础教程推荐