简体   繁体   English

未找到 Maven 注释处理处理器

[英]Maven annotation processing processor not found

I'm new to annotation processing and I'm trying to automating it with Maven.我是注释处理的新手,我正在尝试使用 Maven 将其自动化。 I've put this in my pom.xml:我把它放在我的 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.经过进一步调查,我决定反编译处理器 JAR(使用 Maven 构建),碰巧......我的课程不在那里。 For some reasons, Maven is not compiling my classes into the JAR and that's why the classes are not found.由于某些原因,Maven 没有将我的类编译到 JAR 中,这就是找不到这些类的原因。 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...).我已经尝试弄清楚该版本出了什么问题(这在我之前从未发生过,我已经使用了 Maven 一段时间......)。

First of all, the packaging on that project is jar.首先,该项目的包装是 jar。 The classes are all under src/main/java.这些类都在 src/main/java 下。 I've checked in my pom.xml that the classpath and source path is the same.我检查了我的 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.是 maven 干净安装在处理器项目上的 output。 Unfortunately the output is too long and I had to post an external link even if I know it's not good.不幸的是,output 太长了,即使我知道它不好,我也不得不发布一个外部链接。

EDIT 4编辑 4

Here are some screenshots of my dependency hierarchy:以下是我的依赖层次结构的一些屏幕截图: 蚀 and文件系统 . .

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. 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,但错误仍然存在。

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. 我已经发现问题是META-INF / services /中的文件javax.annotation.processing.Processor以及注释处理器类的配置。 In order to fix the problem I had to add the following to the pom.xml configuration of my processor project: 为了解决这个问题,我必须将以下内容添加到我的处理器项目的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. 这让Maven将类构建到实际的jar中并修复了问题。 I don't know if this is a bug or not but it surely looks strange to me. 我不知道这是不是一个错误,但它肯定对我来说很奇怪。 Thank you everybody for the help! 谢谢大家的帮助!

This is an extended version of the accepted answer above provided by @Aurasphere. 这是@Aurasphere提供的上述接受答案的扩展版本。 Hopefully this will give some explanation to how the proposed solution works. 希望这将对所提出的解决方案如何工作提供一些解释。

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. 我们实现它并将其作为Maven artefact放入JAR中,以便它可以被其他项目使用。 When such projects are being compiled, we want our annotation processor to be recognised by Java compiler and used appropriately. 在编译这样的项目时,我们希望Java编译器能够识别我们的注释处理器并进行适当的使用。 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.Processor file. 编译器查看资源并检查META-INF/services/javax.annotation.processing.Processor文件中列出的类的FQN。 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 . 它尝试在类路径中找到这些类并加载它们以运行对当前正在编译的类使用的注释的处理。

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. 问题是,如果我们简单地将此文件与自定义处理器的FQN放在其中,它将在编译我们的人工制品时触发编译器,并且由于处理器本身尚未编译,编译器将显示有关它的错误。 So we need to skip annotation processing to avoid this. 所以我们需要跳过注释处理以避免这种情况。 This can be done using -proc:none , or with Maven: 这可以使用-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. 在Maven中,在构建主源之后执行测试编译,并且所有类都已经可用,包括我们的处理器。 We just need to add special step during processing of test sources which would use our annotation processor. 我们只需要在处理使用我们的注释处理器的测试源时添加特殊步骤。 This can be done using: 这可以使用:

<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>

The easiest way is to register the annotation processor in the META-INF/services directory of the revolver-annotation-processor artifact. 最简单的方法是在revolver-annotation-processor工件的META-INF / services目录中注册注释处理器。 No Maven compiler configuration is needed. 不需要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. 像这样,无论何时使用其中一个注释,编译器都会选择注释处理器。

The accepted answer here works by disabling all annotation processing, which may not be suitable if other annotation processors need to run during the compilation.此处接受的答案通过禁用所有注释处理来工作,如果其他注释处理器需要在编译期间运行,这可能不适合。 Instead, the SPI configuration file listing the newly compiled annotation processor can be added in a post-processing step.相反,可以在后处理步骤中添加列出新编译的注释处理器的 SPI 配置文件。 I added a directory src/main/post-resources to my project and this plugin configuration:我在我的项目和这个插件配置中添加了一个目录src/main/post-resources

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.3.0</version>
  <executions>
    <execution>
      <id>annotation-processor-spi</id>
      <phase>process-classes</phase>
      <goals>
        <goal>copy-resources</goal>
      </goals>
      <configuration>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <resources>
          <resource>
            <directory>src/main/post-resources</directory>
          </resource>
        </resources>
      </configuration>
    </execution>
  </executions>
</plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM