小编sn4*_*n42的帖子

使用注释处理器时,IntelliJ 中的 Maven 项目构建失败(google/auto-value)

我使用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>
Run Code Online (Sandbox Code Playgroud)

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

Error:java: java.lang.NoClassDefFoundError: com/google/auto/service/AutoService
com.google.auto.service.AutoService
Run Code Online (Sandbox Code Playgroud)

值得注意的是:正确的源代码生成到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>
Run Code Online (Sandbox Code Playgroud)

此修复程序的缺点是auto-service在 …

intellij-idea maven annotation-processing auto-value

7
推荐指数
2
解决办法
1871
查看次数