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.

sn42
  • 2,353
  • 1
  • 15
  • 27
  • 1
    Is there a project you can share to check? See https://youtrack.jetbrains.com/issue/IDEA-212384#focus=streamItem-27-3484036.0-0 about what might be causing this. – Andrey Nov 04 '19 at 06:54
  • 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. – phreed Dec 15 '20 at 00:42

2 Answers2

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.
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
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.

emcmanus
  • 353
  • 2
  • 6
  • 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! – Stella Feb 11 '23 at 01:20