eclipse-jdt / eclipse-jdt/eclipse.jdt.core

Eclipse impedes developing annotation processors by refusing to use them in the workspace

Open
#1,384 7 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
237
Forks
195
Avg merge
1d 12h
Merged PRs (30d)
47

Description

This ticket was originally opened as eclipse-m2e/m2e-core#1550, but I was told it belongs here.

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:

* `foo-aggregate`: Aggregate project.
* `foo-processor`: Implements the `FooProcessor` annotation processor.
* `foo-processor-provider`: Includes the `META-INF/services/javax.annotation.processing.Processor` files so that the annotation processor can be automatically detected. (If this were in the same project as the processor, `javac` would issues a `Bad service configuration file …` error, because it would try to process the same project with the same processor when the processor has not yet been compiled.) Has `foo-processor` as a dependency.
* `test-project`: Simply adds the `@Foo` processor to one of the classes, and indicates `foo-processor-provider` as a dependency.

The processor so far is pretty bare-bones:

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

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

@Override
public boolean process(final Set 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](https://stackoverflow.com/a/45968938):

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

The behavior gets even more complicated than that:

* If I delete the locally-installed versions of `foo-aggregate` and `bar-project` from the local repository in `~/.m2` and then open Eclipse, neither project runs annotation processing, and the `bar-project` says it can't even find the annotation-processing dependency _even though `foo-aggregate` is open in the workspace_`.
* I then go to the command line and issue `mvn clean install` for `foo-aggregate` to install the annotation processor to the local repository. But even then the annotation processing does not occur in either project, even after refreshing the projects in Eclipse using m2e.
* Finally I have to close `foo-aggregate` and refresh `bar-project`, after which `bar-project` will start using the annotation processor (apparently from the local Maven repository in `~/.m2`).

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.