24

I am writing a simple custom annotation in Java and running into a problem with it. Here is the main parts of my code.

LogMeCustomAnnotation.java

package fun.n.learn.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// We need this annotation only till before compilation.
@Retention(RetentionPolicy.SOURCE)
// This is a simple custom annotation.
public @interface LogMeCustomAnnotation {

}

LogMeCustomAnnotationProcessor.java

package fun.n.learn.annotation;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;

// List the custom annotations that are supported.
@SupportedAnnotationTypes({ "fun.n.learn.annotation.LogMeCustomAnnotation" })
// Extend AbstractProcessor. This will let you process.
public class LogMeCustomAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {

        Messager messager = processingEnv.getMessager();
        messager.printMessage(Diagnostic.Kind.NOTE, "I was here.");

        // TODO: Put some meaningful code here. Right now just get it to work.

        // return false;
        // We have already handled these annotations. No more. So return true.
        return true;
    }

}

/src/main/resources/META-INF/services/javax.annotation.processing.Processor

fun.n.learn.annotation.LogMeCustomAnnotationProcessor

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fun.n.learn</groupId>
    <artifactId>javaCustomAnnotation</artifactId>
    <version>0.1.0</version>

    <build>
        <plugins>
            <plugin>
                <!-- Configure the project to use java 8 version. -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <!-- Disable annotation processing for ourselves. -->
                    <!-- <compilerArgument>-proc:none</compilerArgument> -->
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

Now when I run mvn -e clean install I get the following problem

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider fun.n.learn.annotation.LogMeCustomAnnotationProcessor not found
[INFO] 1 error

I must be missing a simple trick here. Any help?

mishtusorous
  • 335
  • 1
  • 2
  • 8

6 Answers6

32

The default maven lifecycle runs javac with javax.annotation.processing.Processor file as a part of classpath. This cause compiler to expect a compiled instance of annotation processors listed in the files. But LogMeCustomAnnotationProcessor is not compiled at that moment so compiler raises "Bad service configuration file ..." error. See bug report.

To solve this issue maven compilation phase can be separated to compile annotation processor at the first place and then compile whole project.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <configuration>
                        <compilerArgument>-proc:none</compilerArgument>
                        <includes>
                            <include>fun/n/learn/annotation/LogMeCustomAnnotationProcessor.java</include>
                            <!--include dependencies required for LogMeCustomAnnotationProcessor -->
                        </includes>
                    </configuration>
                </execution>
                <execution>
                    <id>compile-project</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

default-compile execution compiles LogMeCustomAnnotationProcessor with disabled annotation processing in order to have successful compilation.
compile-project compiles whole project with annotaton processing.

Evgeny
  • 2,483
  • 1
  • 17
  • 24
  • How do I run a particular type of execution? – EpicPandaForce Apr 10 '17 at 12:25
  • 1
    @EpicPandaForce execution-id specification is available since Maven 3.3.1 [MNG-5768](https://issues.apache.org/jira/browse/MNG-5768). `mvn compiler:compile@compile-project` – Evgeny Apr 13 '17 at 17:02
  • `-proc:none` means disabling annotation processing, its bad idea. You are moving away from the actual problem to compile the `LogMeCustomAnnotationProcessor `. – Rohit Gaikwad Jul 06 '18 at 06:21
  • @RohitGaikwad - please read answer carefully. Compilation is split in two steps. At first step `LogMeCustomAnnotationProcessor` is compiled and obviously here we don't need annotation processing to be enabled. Second step compiles whole project with enabled annotation processing. – Evgeny Jul 06 '18 at 16:05
  • Sorry to bring this back from the grave but What to do if I am using another annotation processor like Lambok – Jibin Mathews Nov 26 '20 at 12:15
5

Ok. Found the issue. Earlier my pom.xml had the proc:none line commented out. Now that I have got it back in action it is compiling fine. I need to find out exactly what this line does, but the answer to my question is just put the proc:none back in game. This is how the build section of my pom.xml looks now.

<build>
    <plugins>
        <plugin>
            <!-- Configure the project to use java 8 version. -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <!-- Disable annotation processing for ourselves. -->
                <compilerArgument>-proc:none</compilerArgument>
            </configuration>
        </plugin>
    </plugins>
</build>
mishtusorous
  • 335
  • 1
  • 2
  • 8
  • 10
    `-proc:none` disables annotation processing. So `LogMeCustomAnnotation ` will never be processd. Probably it is not what you expected in your original question. – Evgeny Mar 27 '16 at 17:30
  • Fixed mine too! – Zach May 19 '23 at 19:04
3

Follow the following steps to resolve this:

  • Edit nbproject/project.properties file
  • Search for javac.processorpath, and change it to:

javac.processorpath=\ ${javac.classpath}:\ ${libs.eclipselink.classpath}

Babatunde Adeyemi
  • 14,360
  • 4
  • 34
  • 26
0

I've encountered this error, in particular

Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider com.iviz.schemarestriction.processor.SchemaRestrictionCompilationProcessor could not be instantiated

when migrating maven project from JDK 1.8 (1.8.0_201) to OpenJDK 11(11.0.2).

It was fixed by adding dependency on (2.3.1 was the latest stable version)

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
0

I have resolved the following issue just by deleting the Target folder of annotation-processor and compiled the framework again. It worked like a magic.

Issue:

java: Bad service configuration file, or exception thrown while constructing Processor object: javax.annotation.processing.Processor: Provider com.tracelink.automation.framework.processors.FeedFactoryProcessor not found

-1

try compiling it with the flag: -proc:none