错误的服务配置文件,或在构造处理器对象时引发的异常
2022-09-02 20:47:52
我正在用Java编写一个简单的自定义注释,并遇到了一个问题。这是我的代码的主要部分。
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
啪.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>
现在当我运行时,我遇到以下问题mvn -e clean install
[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
我一定在这里错过了一个简单的技巧。有什么帮助吗?