Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Messages with level Diagnostic.Kind.NOTE (and others) on Annotation Processors

I have two Maven projects:

  • The first defines an annotation, an annotation processor and a provider-configuration file to trigger the annotation processor through the ServiceLoader API.
  • The other depends on the first and defines some classes and interfaces, and one of them is annotated with the annotation defined in the first project.

I invoke the build of the second project (after having built and installed the first) with mvn clean && mvn compile, just to be sure that compilation will happen and that annotation processing will run. It works as expected: the simple annontation processor just generates a resource file in the target/classes/ directory containing some dummy data.

When I tried to output some information using processingEnv.getMessager().printMessage(...) I started having some trouble. If I use Diagnostic.Kind.ERROR, the build stops and it prints the message, as I would expect. However, with any other Kind (such as NOTE or WARNING), the message is not written on the screen!

Some information on my environment:

Kubuntu 12.10
Linux 3.5.0-17-generic
Apache Maven 3.0.4 (r1232337; 2012-01-17 06:44:56-0200)
Java version: 1.7.0_09, vendor: Oracle Corporation

I didn't explicitly declare the version of maven-compiler-plugin anywhere, so it must be using the default version (2.3.2? I'm not sure).

How does one enable the output of all the Kinds of messages on this setup? (ie, launching the annontation processor through maven)

like image 632
Bruno Reis Avatar asked Oct 31 '12 05:10

Bruno Reis


People also ask

What is the annotation process?

The process of generating code at compile time to handle the annotations is called Annotation Processing. The annotation processor can validate, generate, and modify your code based on the annotations, which help you significantly reduce the amount of code you need to write.

How do annotation processors work?

Annotations provide information to a program at compile time or at runtime based on which the program can take further action. An annotation processor processes these annotations at compile time or runtime to provide functionality such as code generation, error checking, etc. The java.

What is annotation processing tool?

Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.

What is Eclipse annotation processing?

A Java annotation processor is a compiler plug-in that can gather information about source code as it is being compiled, generate additional Java types or other resource files, and post warnings and errors.


2 Answers

Apparently, this is a bug in maven-compiler-plugin that has not been fixed: MCOMPILER-66 - Compiler swallows messages from annotation processors.

like image 169
Bruno Reis Avatar answered Sep 22 '22 03:09

Bruno Reis


Try to add following lines:

<configuration>
  <showWarnings>true</showWarnings>
</configuration>

into the maven compiler-plugin definition in the pom.xml of the project. This solution works for me.

See: https://gist.github.com/esamson/0777b97adde4c2f9bc31

like image 41
daniel Avatar answered Sep 19 '22 03:09

daniel