【问题标题】:Maven project build fails in IntelliJ when annotation processors are used (google/auto-value)使用注释处理器时,IntelliJ 中的 Maven 项目构建失败(google/auto-value)
【发布时间】:2019-11-02 12:50:07
【问题描述】:

我使用google/auto-value 在 maven 项目中创建不可变值类。

<?xml version="1.0" encoding="UTF-8"?>
<project 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"
         xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    [...]
    <packaging>war</packaging>

    <properties>
        <auto-value.version>1.7</auto-value.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>com.google.auto.value</groupId>
                            <artifactId>auto-value</artifactId>
                            <version>${auto-value.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>com.google.auto.value</groupId>
            <artifactId>auto-value-annotations</artifactId>
            <version>${auto-value.version}</version>
        </dependency>

        [...]

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

这就像使用 CLI(例如 mvn clean test)的魅力,但在 IntelliJ 项目构建期间会产生错误:

Error:java: java.lang.NoClassDefFoundError: com/google/auto/service/AutoService
com.google.auto.service.AutoService

值得注意的是:在generated-sources/annotations/... 中生成了正确的源代码,但是在此步骤之后IntelliJ 构建失败并且不会创建生成的测试源目录generated-test-sources/...

虽然可以通过向maven-compiler-plugin 添加另一个注释处理器路径来轻松解决此问题

<path>
    <groupId>com.google.auto.service</groupId>
    <artifactId>auto-service</artifactId>
    <version>1.0-rc6</version>
</path>

此修复的缺点是每当auto-value-dependency 版本更改时查找并手动更改auto-service 版本。我的 pom 文件中是否有明显的错误或 IntelliJ 中的设置我不知道?据我所知,当我将项目导入 IntelliJ 时,会创建正确的注释处理配置文件。

【问题讨论】:

  • 有什么项目可以分享看看吗?请参阅 youtrack.jetbrains.com/issue/… 了解可能导致此问题的原因。
  • 我有同样的问题,但它构建正确并且测试运行但是当我尝试使用 Intellij 在测试框架之外运行时,我收到了您报告的错误。

标签: maven intellij-idea annotation-processing auto-value


【解决方案1】:

我遇到了同样的问题,我在没有触及代码的情况下修复了它。这是我所做的:

  1. Settings/Preferences对话框中Ctrl+Alt+S,转到Build,执行、部署 | 编译器 | 注释处理器
  2. 选择默认值,或选择您自己的应用程序配置文件或创建一个新配置文件(单击页面底部的“+”)。
  3. 确保选择了启用注释处理
  4. 将单选按钮从处理器路径更改为从项目类路径获取处理器

【讨论】:

  • ***** (5 Star) for " . . . without touch the code"
【解决方案2】:

这看起来像是 IntelliJ 中的一个错误,如果它是使用 mvn 构建的,但不是来自 IntelliJ 中的。我看到同样的事情。有另一种配置 AutoValue 的方法可以避免该问题:

  <dependencies>
    <dependency>
      <groupId>com.google.auto.value</groupId>
      <artifactId>auto-value-annotations</artifactId>
      <version>1.7</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>com.google.auto.value</groupId>
      <artifactId>auto-value</artifactId>
      <version>1.7</version>
      <optional>true</optional>
    </dependency>
  </dependencies>

在这种情况下,您不需要 &lt;annotationProcessorPaths&gt; 的东西。不利的一面是,AutoValue 注释处理器(auto-value 工件)或其依赖项显然存在一些风险进入您构建的项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2015-09-29
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多