Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run annotation processor in eclipse on save

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

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

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

like image 953
AndiDev Avatar asked Feb 14 '13 07:02

AndiDev


2 Answers

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

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

So here is what I suggest:

  • 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).

Edit: 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.

like image 87
Andrei Avatar answered Sep 30 '22 14:09

Andrei


Make sure your Java project settings (accessible with right-click on project > Java compiler > Annotation processors) do enable annotation processing and that the settings match your expections.

For Maven project, m2e is supposed to configure those settings properly according to the pom.xml content. However, this is not working smoothly for all Maven plugins (some will be supported "out-of-the-box", some others will require a specific plugin...).

like image 27
Mickael Avatar answered Sep 30 '22 12:09

Mickael