java-How to solve Compilation failure: Provider AnnotationProcessor not found

1. The purpose of this post

This post would demo how to solve this exception when we use annotation processing with java:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project java-custom-annotation: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] Error: avax.annotation.processing.Processor: Provider com.bswen.processor.MethodStatusProcessor not found throws exception
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

2. Environments

  • java 1.8+

3. How to reproduce this exception

By default ,you use this maven compiler plugin:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

If we run mvn clean compile, then we got the above error.

4. How to solve this exception

Change your maven pom.xml like this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
        </plugin>
    </plugins>
</build>

rerun the build command mvn clean compile, then we get this:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-custom-annotation ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ java-custom-annotation ---
[INFO] Compiling 2 source files to /bswen-project/java-custom-annotation/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.977 s
[INFO] Finished at: 2019-06-13T13:55:42+08:00
[INFO] Final Memory: 13M/309M
[INFO] ------------------------------------------------------------------------

5. Why

The problem is that the maven-compiler-plugin will trigger compiler during compilation of our annotation-processor, and since the processor itself is not yet compiled, the compiler will show the error about it. So we need to skip annotation processing to avoid this. This can be done using -proc:none.

According to maven documents:

maven compiler plugin’s proc settings: Sets whether annotation processing is performed or not. Only applies to JDK 1.6+ If not set, both compilation and annotation processing are performed at the same time.

6. The code

The code can be found at github bswen project.