2

I made a small zip example to illustrate the issue.

I have in the project the Animal class from AutoValue example and a maven pom file to do the code generation and compilation. See below.

Running mvn clean install works great and generates and compiles everything into target/classes:

enter image description here

The problem is that IntelliJ doesn't recognize AutoValue_Animal:

enter image description here

So how to make IntelliJ IDEA recognize build-time generated sources?

* Preferably, without needing to change IDEA specific setting, in order to keep the dev env consolidated around Maven.

Class:

package kilaka;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class Animal {
    static Animal create(String name, int numberOfLegs) {
        // See "How do I...?" below for nested classes.
        return new AutoValue_Animal(name, numberOfLegs);
    }

    abstract String name();

    abstract int numberOfLegs();
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>kilaka</groupId>
    <artifactId>auto-test</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.google.auto.value</groupId>
            <artifactId>auto-value</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Maven conventions (recognized by IntelliJ) would place generated sources in `target/generated-sources/[plugin-name]`. – Tome Apr 27 '17 at 08:10
  • @Tome So who's placing the generated java file into `target/classes`? – AlikElzin-kilaka Apr 27 '17 at 08:11
  • Usually it is the process/plugin actually doing the generation. Here, as the `com.google.auto.value:auto-value` is rather an APT than an external plugin, you can have a look here: https://immutables.github.io/apt.html – Tome Apr 27 '17 at 08:30
  • @Tome `auto-value` isn't a plugin. It's a dependency. Anyhow, It's maven that is putting the generated content in `target/classes`. Not IntelliJ. – AlikElzin-kilaka Apr 27 '17 at 08:46
  • 1
    Yes, it is rather an annotation processor, and the compiler plugin should place the generated sources in ` ${project.build.directory}/generated-sources/annotations` (see https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html) – Tome Apr 27 '17 at 11:28
  • @Tome using `generatedSourcesDirectory` solved the issue. Thank you. Do you want to add an answer or will I? – AlikElzin-kilaka Apr 27 '17 at 11:38
  • Go ahead with the answer, glad it helped. – Tome Apr 27 '17 at 11:55
  • It seems that the fix eventually was to use the `source` tag in `maven-compiler-plugin`. Didn't matter whether the source is `5` or `8`. – AlikElzin-kilaka Apr 27 '17 at 12:06
  • Enable annotation processing in Intellij Preferences so that while building code will compile and run test. – swapnil Jul 13 '17 at 06:24

2 Answers2

0

Found a solution that worked for me but don't like it. I unmarked the target folder from Excluded and marked the classes as Sources.

In order to continue excluding all other target folders, I had to mark all other folders as excluded.

enter image description here

This is a bad solution because:

  1. If you want to keep your code clean from dev-env, you aren't committing IDEA'a configuration files. Thus need to repeat this step on every clone.
  2. If additional folders are starting to get into the target folder, you need to manually exclude them.
  3. The solution is not maven centric, causing dev-env configuration to be split into several files.
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
0

It seems that the following fixes the issue:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5</version>
    <configuration>
        <source>6</source>
        <target>6</target>
    </configuration>
</plugin>

This caused the file AutoValue_Animal.java to be generated in target/generated-sources/annotations, which IntelliJ IDEA read without any additional tweaking.

I read that annotation processing wasn't supported in Java 5 (yet the class was generated). See more info here.

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • 2
    See explanation in https://github.com/google/auto/issues/482#issuecomment-297711192 – Thomas Broyer Apr 27 '17 at 13:21
  • 1
    _“I read that annotation processing wasn't supported in Java 5 (yet the class was generated)”_ Because you're not using Java 5, you're (probably) using Java 7 or 8 and telling it to cross-compile for a Java 5-compatible runtime (https://docs.oracle.com/javase/8/docs/technotes/tools/unix/javac.html#BHCIJIEG); this is not the same! And beware that this could break at runtime given you're not passing a `-bootclasspath`: https://www.cloudbees.com/blog/beware-sirens-target-call – Thomas Broyer Apr 27 '17 at 13:44