10

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

M-Razavi
  • 3,327
  • 2
  • 34
  • 46
AndiDev
  • 1,272
  • 14
  • 26
  • 2
    Check your project settings under `Java Compiler > Annotation Processing` and make sure that everything is enabled there. – gk5885 Sep 04 '13 at 17:51
  • Hi! Did you solve your problem? – LppEdd Aug 27 '17 at 09:13
  • Yes I switched to netbeans – AndiDev Aug 27 '17 at 10:08
  • Hahaha, well thanks anyway! I'll tell you if I find something! – LppEdd Aug 27 '17 at 13:34
  • @AndiDev what library/framework where you using? – LppEdd Aug 27 '17 at 18:49
  • I was developing a my own custom annotation processor for code generation – AndiDev Aug 28 '17 at 12:10
  • @LppEdd What does not work for you as you want more attention to this? Eclipse is a bit unstable getting this up and running, so you may want to open your own question? – Thorbjørn Ravn Andersen Aug 29 '17 at 13:48
  • @AndiDev, I've tried to provide an answer, but then I re-read your question and I might have misunderstood; do you mean that you want to have BOTH projects in your workspace (annotation processor + client project), and upon changing a file in your annotation processor, it should get invoked on your client project, or do you mean that you have a project in Eclipse and an external jar which contains an annotation processor, which doesn't get invoked upon save? If first option, I don't think it's possible. – Andrei Aug 30 '17 at 20:35

3 Answers3

3

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.

Andrei
  • 1,613
  • 3
  • 16
  • 36
2

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

Mickael
  • 3,506
  • 1
  • 21
  • 33
1

I think you need a trigger to run Maven goal, So:

You have to add a valid maven lifecycle action

Example for a jar which is automatically deployed locally by maven install plugin:

<build>
    <!-- ... -->

    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>

                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-jar-plugin</artifactId>
                                    <versionRange>[2.0,)</versionRange>
                                    <goals>
                                        <goal>jar</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute>
                                        <runOnConfiguration>true</runOnConfiguration>
                                        <runOnIncremental>true</runOnIncremental>
                                    </execute>
                                </action>
                            </pluginExecution>

                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-install-plugin</artifactId>
                                    <versionRange>[2.5.0,)</versionRange>
                                    <goals>
                                        <goal>install</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <execute>
                                        <runOnConfiguration>true</runOnConfiguration>
                                        <runOnIncremental>true</runOnIncremental>
                                    </execute>
                                </action>
                            </pluginExecution>

                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Hint: relates to Maven Project Builder is invoked every time I change a source file (GWT) and as a warning: install typically includes tests if you have included them in your normal maven build cycle

M-Razavi
  • 3,327
  • 2
  • 34
  • 46