mis*_*ous 5 java annotations maven
我在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 {
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
/src/main/resources/META-INF/services/javax.annotation.processing.Processor
fun.n.learn.annotation.LogMeCustomAnnotationProcessor
Run Code Online (Sandbox Code Playgroud)
的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>
Run Code Online (Sandbox Code Playgroud)
现在,当我跑步时,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
Run Code Online (Sandbox Code Playgroud)
我必须在这里错过一个简单的伎俩.有帮助吗?
Evg*_*eny 12
默认的maven生命周期使用javax.annotation.processing.Processor文件作为classpath的一部分运行javac .这会导致编译器期望文件中列出的注释处理器的已编译实例.但是LogMeCustomAnnotationProcessor当时没有编译,因此编译器引发"Bad service configuration file ..."错误.查看错误报告.
要解决这个问题,可以将maven编译阶段分开编译注释处理器,然后编译整个项目.
<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>
Run Code Online (Sandbox Code Playgroud)
default-compile执行LogMeCustomAnnotationProcessor使用禁用的注释处理进行编译,以便成功编译.
compile-project使用注释处理编译整个项目.
好的。发现问题了。早些时候,我的 pom.xml 已proc:none注释掉该行。现在我已经让它恢复正常了,它编译得很好。我需要弄清楚这条线到底是做什么的,但我问题的答案就是把它放回proc:none游戏中。这就是我的 pom.xml 的构建部分现在的样子。
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12802 次 |
| 最近记录: |