简体   繁体   English

Javac没有运行注释处理器

[英]Javac not running annotation processors

Problem问题

I'm revisiting a maven project that I haven't touched in at least a year.我正在重新审视我至少一年没有接触过的 maven 项目。 I'm pretty sure it was compiling successfully when I left it (there was still a working jar in the target directory), but now compilation fails because generated classes are missing.我很确定当我离开它时它编译成功( target目录中仍然有一个工作 jar),但现在编译失败,因为缺少生成的类。

What I tried我试过的

  • Verified there are indeed no sources in target/generated-sources/annotations ( none present )已验证target/generated-sources/annotations中确实没有源(不存在
  • Ensured javac wasn't invoked with -proc:none ( it wasn't )确保没有使用-proc:none调用 javac(不是
  • Ran mvn clean dependency:unpack-dependencies -Dmdep.useSubDirectoryPerArtifact=true to ensure the expected dependencies are on the classpath and that they contain a valid META-INF/services/javax.annotation.processing.Processor entry ( yes there were multiple, including org.immutables.processor.ProxyProcessor ) Ran mvn clean dependency:unpack-dependencies -Dmdep.useSubDirectoryPerArtifact=true以确保预期的依赖项位于类路径上,并且它们包含有效的META-INF/services/javax.annotation.processing.Processor条目(的,有多个,包括org.immutables.processor.ProxyProcessor
  • Followed these steps from How to make sure javac is using an annotation processor and troubleshoot when it is not :遵循如何确保 javac 正在使用注释处理器并在未使用时进行故障排除中的这些步骤:
    1. Take the javac options from the maven debug log从 maven 调试日志中获取 javac 选项
    2. Remove -nowarn option and add -verbose -XprintRounds -XprintProcessorInfo -Xlint -J-verbose .删除-nowarn选项并添加-verbose -XprintRounds -XprintProcessorInfo -Xlint -J-verbose NOTE: I also had to add the relative path to the main class, or javac would complain there are no sources.注意:我还必须添加主 class 的相对路径,否则 javac 会抱怨没有来源。
    3. Verify if class of processor is loaded ( not present )验证处理器的 class 是否已加载(不存在
    4. Look for at least one processing round log ( none present )查找至少一个处理轮次日志(不存在
    5. Make sure annotations are present in the list of the round log ( no round log )确保注释存在于循环日志列表中(无循环日志
  • Added -proc:only option to javac command为 javac 命令添加了-proc:only选项
  • Also added -processor org.immutables.processor.ProxyProcessor to javac command ( processor now loaded , but still no rounds printed and no classes generated) -processor org.immutables.processor.ProxyProcessor添加到 javac 命令(处理器现在已加载,但仍然没有打印轮次,也没有生成类)
  • Changed the processor option to -processor org.immutables.processor.ProxyProcessorXXX to see if it would make a difference ( it did , it now printed a warning stating that processing was requested with proc:only , but no processors were found)将处理器选项更改为-processor org.immutables.processor.ProxyProcessorXXX以查看它是否会有所作为(确实如此,它现在打印一条警告,指出使用proc:only请求处理,但未找到处理器)
  • Checked to see if that warning appeared if I didn't use the -processor option, which should make javac detect processors from class path ( it didn't show the warning, which suggests it's detecting processors yet the log doesn't show any sign of it)检查如果我不使用-processor选项是否出现该警告,这应该使 javac 从 class 路径检测处理器(它没有显示警告,这表明它正在检测处理器但日志没有显示任何迹象其中)
  • Tried above after switching from JDK1.8 to JDK9 (using jenv)从JDK1.8切换到JDK9后在上面尝试过(使用jenv)
  • Installed Zulu 1.7 and tried above after switching to it (using jenv)安装 Zulu 1.7 并在切换到它后尝试上面(使用 jenv)

Possible factors可能的因素

  • I did an entirely clean install of macOS我做了一个完全干净的 macOS 安装
  • I ran macOS Sierra before, currently High Sierra我之前运行过 macOS Sierra,目前是 High Sierra
  • Before I didn't have JDK9 installed在我没有安装 JDK9 之前
  • I switched to using Gradle a long time ago, so I might have forgotten an important detail about maven我很久以前改用 Gradle,所以我可能忘记了关于 maven 的一个重要细节

Command options命令选项

This is the javac command I'm currently using:这是我目前使用的 javac 命令:

javac -d./target/classes -classpath./target/classes:$HOME/.m2/repository/com/google/dagger/dagger/2.4/dagger-2.4.jar:$HOME/.m2/repository/com/google/dagger/dagger-compiler/2.4/dagger-compiler-2.4.jar:$HOME/.m2/repository/com/google/auto/factory/auto-factory/1.0-beta3/auto-factory-1.0-beta3.jar:$HOME/.m2/repository/org/immutables/builder/2.3.9/builder-2.3.9.jar:$HOME/.m2/repository/org/immutables/value/2.3.9/value-2.3.9.jar:<LONG LIST OF DIRECT AND TRANSITIVE DEPENDENCIES HERE> -sourcepath./src/main/java: -s./target/generated-sources/annotations -verbose -XprintRounds -XprintProcessorInfo -Xlint -J-verbose -processor org.immutables.processor.ProxyProcessor -proc:only./src/main/java/com/mycompany/myproject/Main.java

NOTE : for better readability, I put each argument on a new line, replaced absolute paths with $HOME/ and ./ and I omitted most dependencies.注意:为了更好的可读性,我将每个参数放在一个新行上,用$HOME/./替换了绝对路径,并且我省略了大多数依赖项。

Question问题

What am I missing here?我在这里想念什么? Any suggestions or pointers would be greatly appreciated.任何建议或指示将不胜感激。

As far as I can remember maven generates a marker file or marker directory (starting with a dot) for generated sources. 据我所知,maven为生成的源生成一个标记文件或标记目录(以点开头)。 But it's also a long time ago... 但也是很久以前......

We had the same issue on a git branch, while the main branch built fine.我们在 git 分支上遇到了同样的问题,而主分支构建良好。 The root cause turned out to be a.java source with a duplicate definition of a private void function.根本原因原来是 a.java 源具有重复定义的私有 void function。 This duplicate was caused by a merge from the main branch.这个重复是由主分支的合并引起的。 At first we did not notice it, because the merge did not cause conflicts.起初我们没有注意到它,因为合并没有引起冲突。 The problem occurred with Amazon Corretto 8 and 11 compilers. Amazon Corretto 8 和 11 编译器出现此问题。 After removing the duplicate private void function, the java compiler started generating sources as before.删除重复的私有 void function 后,java 编译器像以前一样开始生成源。

javac appears to completely and silently skip annotation processing for some parse failures.对于某些解析失败, javac似乎完全且静默地跳过注释处理。

For me a duplicated annotation caused by a merge mistake caused thousands of failures because my annotation processors didn't run - so the real error got completely lost.对我来说,由于我的注释处理器没有运行,由合并错误引起的重复注释导致了数千次失败 - 所以真正的错误完全丢失了。

@Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(callSuper = false)

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

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