Maven project build fails in IntelliJ when annotation processors are used (google/auto-value)
Asked Answered
E

2

8

I use google/auto-value to create immutable value classes in a maven project.

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

This works like a charm using the CLI (e.g. mvn clean test) but creates an error during IntelliJ project builds:

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

Noteworthy: The correct sources are generated into generated-sources/annotations/... but the IntelliJ build fails after this step and does not create the generated test sources directory generated-test-sources/....

While the issue can be easily fixed by adding another annotation processor path to the maven-compiler-plugin

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

this fix has the downside of looking up and manually changing the auto-service version whenever the auto-value-dependency version changes. Is there an obvious mistake i made in my pom file or a setting in IntelliJ i don't know? As far as i can see a correct annotation processing profile is created when i import the project into IntelliJ.

Evocator answered 2/11, 2019 at 12:50 Comment(2)
Is there a project you can share to check? See youtrack.jetbrains.com/issue/… about what might be causing this.Halting
I have the same problem but it builds correctly and the tests run but when I try to run outside of the test framework with Intellij I get the error you report.Alisealisen
C
9

I faced the same issue, and I fixed it without touching to the code. Here's what I did:

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Build, Execution, Deployment | Compiler | Annotation Processors.
  2. Select the default, or select your own application profile or create a new one (click "+" on the bottom of the page).
  3. Make sure Enable annotation processing is selected
  4. Change the radio button from Processor path to Obtain processors from the project classpath.
Cupule answered 11/2, 2021 at 14:8 Comment(1)
***** (5 Star) for " . . . without touching the code"Nonlinearity
T
4

This looks like a bug in IntelliJ, if it builds with mvn but not from within IntelliJ. I see the same thing. There is an alternative way of configuring AutoValue which avoids the problem:

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

You don't need the <annotationProcessorPaths> stuff in this case. On the downside, there's apparently some risk of the AutoValue annotation processor (the auto-value artifact) or its dependencies finding their way into your built project.

Tudela answered 29/11, 2019 at 19:1 Comment(1)
Thanks! After trying all sorts of recommendations like 1.configuring project source to include generated and 2.configuring Annotation Processors, your solution helped me solve my issue!Meekins

© 2022 - 2024 — McMap. All rights reserved.