【问题标题】:Bad service configuration file, or exception thrown while constructing Processor object错误的服务配置文件,或构造处理器对象时抛出异常
【发布时间】:2016-07-14 22:08:19
【问题描述】:

我正在用 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

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 时,我遇到了以下问题

[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

我一定错过了一个简单的技巧。有什么帮助吗?

【问题讨论】:

  • 你能发布你的整个 POM 吗?

标签: java maven annotations


【解决方案1】:

尝试使用标志编译它:-proc:none

【讨论】:

    【解决方案2】:

    我已经解决了以下问题,只需删除注释处理器的目标文件夹并再次编译框架。它就像魔术一样工作。

    问题:

    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

    【讨论】:

    • 您说“您已解决并且有效”,但您正在发布另一个问题。这是您遇到的新问题?
    【解决方案3】:

    我遇到过这个错误,特别是

    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
    

    将 maven 项目从 JDK 1.8 (1.8.0_201) 迁移到 OpenJDK 11(11.0.2) 时。

    通过添加对(2.3.1 是最新的稳定版本)的依赖来修复它

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

    【讨论】:

      【解决方案4】:

      按照以下步骤解决此问题:

      • 编辑nbproject/project.properties文件
      • 搜索javac.processorpath,并将其更改为:

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

      【讨论】:

        【解决方案5】:

        默认的 maven 生命周期运行 javac,并将 javax.annotation.processing.Processor 文件作为类路径的一部分。这导致编译器期望文件中列出的注释处理器的编译实例。但是LogMeCustomAnnotationProcessor 那时没有编译,所以编译器会引发“Bad service configuration file ...”错误。见bug report

        为了解决这个问题,可以将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>
        

        default-compile 执行编译 LogMeCustomAnnotationProcessor 并禁用注释处理,以便成功编译。
        compile-project 编译整个项目并进行注释处理。

        【讨论】:

        • 如何运行特定类型的执行?
        • @EpicPandaForce execution-id 规范自 Maven 3.3.1 MNG-5768 起可用。 mvn compiler:compile@compile-project
        • &lt;compilerArgument&gt;-proc:none&lt;/compilerArgument&gt; 表示禁用注释处理,这是个坏主意。您正在远离实际问题来编译LogMeCustomAnnotationProcessor
        • @RohitGaikwad - 请仔细阅读答案。编译分为两步。第一步LogMeCustomAnnotationProcessor 被编译,显然这里我们不需要启用注释处理。第二步使用启用的注释处理编译整个项目。
        • 很抱歉把它从坟墓里带回来,但是如果我使用另一个注释处理器,比如 Lambok,该怎么办
        【解决方案6】:

        好的。发现问题。早些时候,我的 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>
        

        【讨论】:

        • &lt;compilerArgument&gt;-proc:none&lt;/compilerArgument&gt; 禁用注释处理。所以LogMeCustomAnnotation 永远不会被处理。可能不是您在原始问题中所期望的。
        猜你喜欢
        • 1970-01-01
        • 2011-06-25
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        相关资源
        最近更新 更多