Can't find class in generated sources produced by custom annotation processor when running java program

108 Views Asked by At

I have written an annotation processor to generate classes to the generated sources directly during compilation. The class generates with valid syntax, and to the correct, matching (same as interface that is annotated) package. My IDE (IntelliJ) detects the generated class and itself does not produce compilation errors. When I also generate a spring type bean with my library (using @Component) My IDE also correctly detects that it is a injectable bean.

When I try to execute a normal java program I get this error:

java: cannot find symbol
  symbol:   class DuhStudentMapper

When I try to start a spring boot server, I get this error:

Field wordMapper2 in com.oworms.word.controller.WordController required a bean of type 'com.oworms.WordMapper2' that could not be found.

Parent 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.noydb.duhmap</groupId>
    <artifactId>duhmapper</artifactId>
    <name>duhmapper</name>
    <version>0.5.1</version>
    <packaging>pom</packaging>

    <modules>
        <module>processor</module>
        <module>runner</module>
    </modules>

    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>

        <duhmap.processor.version>0.6.0</duhmap.processor.version>
    </properties>

</project>

Processor 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>
    <parent>
        <groupId>com.noydb.duhmap</groupId>
        <artifactId>duhmapper</artifactId>
        <version>0.5.1</version>
    </parent>

    <artifactId>processor</artifactId>
    <version>${duhmap.processor.version}</version>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <configuration>
                            <source>${java.version}</source>
                            <target>${java.version}</target>
                            <compilerArgument>-proc:none</compilerArgument>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.3.0</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

I’m not certain I need the maven-source-plugin, but when I went through other popular libraries doing annotation processor work, they had that plugin configured.

Runner (purely for testing purposes) 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>
    <parent>
        <groupId>com.noydb.duhmap</groupId>
        <artifactId>duhmapper</artifactId>
        <version>0.5.1</version>
    </parent>

    <artifactId>runner</artifactId>
    <version>0.0.0-final</version>
    <description>
        A simple test module to practice and test the processor. Zero importance in regard to this library
    </description>

    <dependencies>
        <dependency>
            <groupId>com.noydb.duhmap</groupId>
            <artifactId>processor</artifactId>
            <version>${duhmap.processor.version}</version>
        </dependency>
    </dependencies>

</project>

The processor writes the file to the directory like so:

final Filer filer = processingEnv.getFiler();
final FileObject fileObject;

try {
    fileObject = filer.createResource(StandardLocation.SOURCE_OUTPUT, packageName, className + ".java");
} catch (final IOException e) {
    throw new DuhMapException(
            String.format(
                    "Error during writing of DuhMap file to source output for class: %s.%s",
                    packageName,
                    className
            ),
            e
    );
}

try (final Writer writer = fileObject.openWriter()) {
    writer.write(content);
} catch (final IOException e) {
    throw new DuhMapException(
            String.format(
                    "Error during writing of DuhMap file to source output for class: %s.%s",
                    packageName,
                    className
            ),
            e
    );
}

I feel as if I am missing some type of maven configuration or compiler option. I have gone through the source code of other popular libraries doing similar things to this library I’m trying to build, but I cannot seem to spot anything I might be missing. I also struggle to know what to Google in order to find articles, advice, etc. on this specific problem.

Any help would be greatly appreciated

1

There are 1 best solutions below

0
On

The reason why it isn't working in your runner is most likely due to the use of an older maven-compiler-plugin

I would suggest adding

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
        </plugin>
    </plugins>
</build>

Another alternative would also be to just add the maven-compiler-plugin in your parent pluginManagement

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.11.0</version>
        </plugin>
    </plugins>
</pluginManagement>

In this case you do not need to do anything in the runner and in the processor pom.xml you'll only need to do

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <configuration>
                        <compilerArgument>-proc:none</compilerArgument>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>