java 找不到Maven注释处理处理器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38926255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:53:45  来源:igfitidea点击:

Maven annotation processing processor not found

javamavenannotationsannotation-processing

提问by Aurasphere

I'm new to annotation processing and I'm trying to automating it with Maven. I've put this in my pom.xml:

我是注释处理的新手,我正在尝试使用 Maven 对其进行自动化。我把它放在我的 pom.xml 中:

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <annotationProcessors>
                    <annotationProcessor>
                        co.aurasphere.revolver.annotation.processor.InjectAnnotationProcessor</annotationProcessor>
                    <annotationProcessor>
                        co.aurasphere.revolver.annotation.processor.RevolverContextAnnotationProcessor</annotationProcessor>
                </annotationProcessors>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>

The problem is that when I try to build the project I get a CompilationFailureException because Maven can't find the processors.

问题是,当我尝试构建项目时,我收到 CompilationFailureException,因为 Maven 找不到处理器。

I've found other questions like this, solved by putting the dependency outside the plugin. I tried that, but nothing changed for me.

我发现了其他类似的问题,通过将依赖项放在插件之外来解决。我试过了,但对我来说没有任何改变。

Am I missing something?

我错过了什么吗?

Thank you.

谢谢你。

EDIT

编辑

Here is my dependency on another project which contains both the processor and the annotations:

这是我对另一个包含处理器和注释的项目的依赖:

    <dependencies>
    <dependency>
        <groupId>co.aurasphere</groupId>
        <artifactId>revolver-annotation-processor</artifactId>
        <version>0.0.3-SNAPSHOT</version>
    </dependency>
</dependencies>

EDIT 2:

编辑 2

After further investigation, I decided to decompile the processor JAR (built with Maven) and it happens that... my classes are not there. For some reasons, Maven is not compiling my classes into the JAR and that's why the classes are not found. I've tried figuring out what's wrong on that build (this never happened to me before and I've used Maven for a while...).

经过进一步调查,我决定反编译处理器 JAR(用 Maven 构建),结果……我的类不在那里。由于某些原因,Maven 没有将我的类编译到 JAR 中,这就是找不到这些类的原因。我试图找出该构建的问题(这在我之前从未发生过,而且我已经使用了 Maven 一段时间......)。

First of all, the packaging on that project is jar. The classes are all under src/main/java. I've checked in my pom.xml that the classpath and source path is the same.

首先,那个项目的包装是jar。这些类都在 src/main/java 下。我已经在我的 pom.xml 中检查了类路径和源路径是相同的。

Here's the processor pom:

这是处理器 pom:

<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>co.aurasphere</groupId>
<artifactId>revolver-annotation-processor</artifactId>
<version>0.0.3-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins> 
</build>
<dependencies>
    <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.velocity/velocity -->
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.7</version>
    </dependency>


</dependencies>

EDIT 3

编辑 3

Here's the output of a maven clean install on the processor project. Unfortunately the output is too long and I had to post an external link even if I know it's not good.

是处理器项目上的 maven 全新安装的输出。不幸的是,输出太长,即使我知道它不好,我也不得不发布一个外部链接。

EDIT 4

编辑 4

Here are some screenshots of my dependency hierarchy: Eclipseand File System.

以下是我的依赖层次结构的一些屏幕截图:蚀文件系统.

Since the project was originally created as an Eclipse simple Java project and then converted to a Maven one, I tried to create a new Maven project and move everything to the new one in the hope that the problem was the Eclipse plugin that messed something up, but the error was still there.

由于该项目最初创建为 Eclipse 简单 Java 项目,然后转换为 Maven 项目,因此我尝试创建一个新的 Maven 项目并将所有内容移至新项目,希望问题是 Eclipse 插件搞砸了,但错误仍然存​​在。

采纳答案by Aurasphere

I've found the answer myself. I've figured out that the problem was the file javax.annotation.processing.Processor in META-INF/services/ with the configuration of the annotation processor's class. In order to fix the problem I had to add the following to the pom.xml configuration of my processor project:

我自己找到了答案。我发现问题出在 META-INF/services/ 中的文件 javax.annotation.processing.Processor 和注释处理器类的配置中。为了解决这个问题,我必须将以下内容添加到我的处理器项目的 pom.xml 配置中:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <compilerArgument>
                    -proc:none
                </compilerArgument>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

This let Maven build the classes into the actual jar and fixed the problem. I don't know if this is a bug or not but it surely looks strange to me. Thank you everybody for the help!

这让 Maven 将类构建到实际的 jar 中并修复了问题。我不知道这是否是一个错误,但对我来说肯定看起来很奇怪。谢谢大家的帮助!

回答by scrutari

This is an extended version of the accepted answer above provided by @Aurasphere. Hopefully this will give some explanation to how the proposed solution works.

这是@Aurasphere 提供的上述已接受答案的扩展版本。希望这将对所提出的解决方案的工作原理给出一些解释。

First, some background to what is happening here. Say, we want a custom annotation processor. We implement it and put it into a JAR as Maven artefact, so that it could be consumed by other projects. When such projects are being compiled, we want our annotation processor to be recognised by Java compiler and used appropriately. To make this happen, one needs to tell the compiler about a new custom processor. Compiler looks in the resources and checks FQN of classes listed in META-INF/services/javax.annotation.processing.Processorfile. It tries to find these classes in classpath and load them to run the processing of annotations used upon classes that are currently being compiled.

首先,这里发生的事情的一些背景。比如说,我们想要一个自定义的注释处理器。我们实现它并将其作为 Maven 人工制品放入 JAR 中,以便其他项目可以使用它。在编译此类项目时,我们希望我们的注解处理器能够被 Java 编译器识别并正确使用。要实现这一点,需要告诉编译器有关新的自定义处理器的信息。编译器查看资源并检查META-INF/services/javax.annotation.processing.Processor文件中列出的类的 FQN 。它尝试在类路径中找到这些类并加载它们以运行对当前正在编译的类使用的注释的处理。

So, we want our custom class to be mentioned in this file. We can ask a user of our library to put this file manually, but this is not intuitive and users could be frustrated why the promised processing of annotation doesn't work. That's why we might want to prepare this file in advance and deliver it together with the processor inside JAR of our Maven artefact.

因此,我们希望在此文件中提及我们的自定义类。我们可以要求我们库的用户手动放置这个文件,但这并不直观,用户可能会因为承诺的注释处理不起作用而感到沮丧。这就是为什么我们可能想要提前准备这个文件,并将它与我们的 Maven 工件的 JAR 内的处理器一起交付。

The problem is that if we simply put this file with FQN of the custom processor in it, it will trigger compiler during compilation of our artefact, and since the processor itself is not yet compiled, the compiler will show the error about it. So we need to skip annotation processing to avoid this. This can be done using -proc:none, or with Maven:

问题是,如果我们简单地将自定义处理器的 FQN 放在这个文件中,它会在我们的工件编译期间触发编译器,并且由于处理器本身尚未编译,编译器会显示有关它的错误。所以我们需要跳过注释处理来避免这种情况。这可以使用-proc:none, 或使用 Maven来完成:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <proc>none</proc>
    </configuration>
</plugin>

We might have unit tests that will need our annotation processor. In Maven, test compilation is carried out after main sources are built, and all classes are already available including our processor. We just need to add special step during processing of test sources which would use our annotation processor. This can be done using:

我们可能有需要注释处理器的单元测试。在Maven中,主要源代码构建完成后进行测试编译,包括我们的处理器在内的所有类都已经可用。我们只需要在处理测试源期间添加特殊步骤,这将使用我们的注释处理器。这可以使用:

<plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
        <execution>
            <id>process-test-annotations</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>testCompile</goal>
            </goals>
            <configuration>
                <proc>only</proc>
                <annotationProcessors>
                    <annotationProcessor>fully.qualified.Name</annotationProcessor>
                </annotationProcessors>
            </configuration>
        </execution>
    </executions>
</plugin>

回答by Puce

The easiest way is to register the annotation processor in the META-INF/services directory of the revolver-annotation-processor artifact. No Maven compiler configuration is needed.

最简单的方法是在 revolver-annotation-processor 工件的 META-INF/services 目录中注册注释处理器。不需要 Maven 编译器配置。

Check if it's already registered, if not, register it yourself if you control the source code.

检查它是否已经注册,如果没有,如果您控制源代码,请自行注册。

https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html

https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html

If you control the source code I also recommend to package the processor in the same artifact as the annotations. Like this, whenever you're using one of the annotations, the annotation processor is also picked-up by the compiler.

如果您控制源代码,我还建议将处理器打包在与注释相同的工件中。像这样,每当您使用其中一个注释时,编译器也会选取注释处理器。