问题描述
我正在用Java编写一个简单的自定义批注,并遇到了问题.这是我代码的主要部分.
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>
现在,当我运行mvn -e clean install
时,出现以下问题
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?
推荐答案
默认的maven生命周期使用 javax.annotation.processing.Processor 文件作为类路径的一部分运行javac.这导致编译器期望文件中列出的注释处理器的已编译实例.但是此时尚未编译LogMeCustomAnnotationProcessor
,因此编译器会引发错误的服务配置文件..."错误.请参阅错误报告.
为了解决这个问题,可以将maven的编译阶段分开,首先编译注释处理器,然后再编译整个项目.
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
执行将使用禁用注释处理的方式编译LogMeCustomAnnotationProcessor
,以便成功进行编译.compile-project
使用注释处理编译整个项目.
default-compile
execution compiles LogMeCustomAnnotationProcessor
with disabled annotation processing in order to have successful compilation.compile-project
compiles whole project with annotaton processing.
这篇关于服务配置文件错误,或在构造Processor对象时引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!