运行java程序时在自定义注释处理器生成的生成源中找不到类

问题描述 投票:0回答:1

我编写了一个注释处理器,可以在编译期间直接为生成的源生成类。该类使用有效的语法生成,并生成正确的匹配(与带注释的接口相同)的包。我的 IDE (IntelliJ) 检测生成的类,它本身不会产生编译错误。当我还用我的库生成一个 spring 类型的 bean 时(使用

@Component
),我的 IDE 也正确检测到它是一个可注入的 bean。

当我尝试执行普通的 java 程序时,出现此错误:

java: cannot find symbol
  symbol:   class DuhStudentMapper

当我尝试启动 Spring Boot 服务器时,出现此错误:

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

家长

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>

处理器

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>

我不确定我是否需要 maven-source-plugin,但是当我浏览其他流行的库进行注释处理器工作时,他们配置了该插件。

Runner(纯粹用于测试目的)

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>

处理器将文件写入目录,如下所示:

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
    );
}

我感觉好像缺少某种类型的 Maven 配置或编译器选项。我已经浏览了其他流行库的源代码,它们与我正在尝试构建的这个库做了类似的事情,但我似乎找不到任何我可能丢失的东西。我也很难知道要谷歌什么才能找到关于这个特定问题的文章、建议等。

任何帮助将不胜感激

java maven code-generation annotation-processing cannot-find-symbol
1个回答
0
投票

它在您的运行器中不起作用的原因很可能是由于使用了较旧的 maven-compiler-plugin

我建议添加

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

另一种选择也是在您的父项中添加

maven-compiler-plugin
pluginManagement

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

在这种情况下,您不需要在运行器中执行任何操作,而在处理器 pom.xml 中您只需要执行

<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>
© www.soinside.com 2019 - 2024. All rights reserved.