eclipse-m2e / m2e-core

Eclipse Public License 2.0
110 stars 112 forks source link

Annotation processing does not occur in one project if annotation processor definition project is open in another #1550

Open garretwilson opened 1 year ago

garretwilson commented 1 year ago

Summary: Projects imported into Eclipse using m2e that use an annotation processor, which is itself defined in other project open in the same workspace, do not invoke the annotation processor when compiling unless the project defining the annotation processor is first closed in the workspace.

I'm using a standard Eclipse EE 2023-09 installation. I have a Maven project that defines an annotation processor, which I have imported into Eclipse using m2e. The processor processes the @Foo annotation. The project looks like this:

The processor so far is pretty bare-bones:

@SupportedAnnotationTypes("com.example.Foo")
public class FooProcessor extends AbstractProcessor {

  @Override
  public SourceVersion getSupportedSourceVersion() {
    return SourceVersion.latestSupported();
  }

  @Override
  public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
    java.awt.Toolkit.getDefaultToolkit().beep();
    System.out.println("***** Annotation processing!");
    processingEnv.getMessager().printMessage(NOTE, "Processing round; processing %d annotations ...".formatted(annotations.size()));
    return true;
  }

}

Now if I run mvn clean compile on foo-aggregate, when maven compiles the test-project subproject I hear a beep and Maven prints:

***** Annotation processing!
***** Annotation processing!
[INFO] Processing round; processing 1 annotations ...
[INFO] Processing round; processing 0 annotations ...

This is as I would expect.

In Eclipse, I have found that there is a Preferences > Maven > Annotation Processing > Select Annotation Processing Mode setting which by default is set to "Do not automatically configure/execute annotation processing from pom.xml". I have changed this to "Automatically configure JDT API …". So we should be all set.

However when I import the foo-aggregate project into Eclipse using m2e and compile the test-project subproject, I see no annotation processing output in the "Error Log" view tab; nor do I hear a beep.

In fact someone noted this six years ago in an answer on Stack Overflow:

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

So it seems impossible to have a subproject that uses an annotation processing defined in the same multimodule aggregate project that defines the annotation processor in a separate subproject.

But it's worse than that—it's not confined to the same aggregate project. If I create a completely separate bar-project that uses the @Foo annotation and lists foo-processor-provider as a dependency in the POM, the same bug appears. I imported bar-project into the same workspace in Eclipse, and there are still no messages or beep when compiling bar-project.

So I closed the foo-aggregate project (making sure I had used mvn install from the command line so that the annotation processor would still appear in Maven's local repository), and sure enough—I get two lines of in the Error Log view tab, and I hear a beep! (Note that the lines in the Error Log appear in reverse order in the tab because that's how the tab lists messages.)

Processing round; processing 0 annotations ... Processing round; processing 1 annotations ...

Why must I close the project in which the annotation processor is defined in order to use it in another project in the same workspace in Eclipse? (This will significantly impede my ability to develop an annotation processor using Eclipse.)

I don't have enough information to even guess if they are related, but I note that this bug and #1501 have similar behavior in that the bug appears or disappears based upon whether a dependency is open in the workspace.

garretwilson commented 1 year ago

Actually it's even more complicated than that:

fbricon commented 1 year ago

Upstream JDT APT ignores annotation processors from workspace projects. It's a design decision we have no control over. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=259230 for context.

Please take it up to https://github.com/eclipse-jdt/eclipse.jdt.core/issues

garretwilson commented 1 year ago

@fbricon, just to clarify the current behavior, if I'm developing an annotation processor in foo-aggregate in the workspace, and I want to test it in bar-project, also in the workspace, can I simply close the foo-aggregate project and then tell Eclipse to recompile bar-project?

Or will I have to 1) close the foo-aggregate project, 2) go to the command line and run mvn install for foo-aggregate, 3) come back and refresh bar-project in Eclipse, and then finally 4) tell Eclipse to recompile bar-project?

fbricon commented 1 year ago

IIRC, you need to mvn install foo-aggregate from CLI, then close it in the workspace, bar-project should automatically pick up the jar in the Maven classpath container. If the JDT APT config was not updated automatically, just run Maven > Update Project