You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lombok+Xtext+QueryDSL项目Delombok配置报错:无法找到符号

问题

我正在开发一个使用Lombok、Xtext和QueryDSL的项目,需求是让Xtext使用delombok处理后的文件进行后续代码生成,而非带有Lombok注解的原始文件。但执行mvn clean package时,控制台出现大量「无法找到符号」错误,涉及Java类中引用的QueryDSL Q类及Xtext生成的类。问题根源在于这些类是在delombok执行完成后才生成的,导致delombok步骤报错。

具体报错信息如下:

[INFO] --- lombok:1.18.20.0:delombok (default) @ ART-Impl ---
/ART-Impl/src/main/java/com/core/domain/model/customer/CustomersRepository.java:3: error: package com.core.domain.model.customer.QCustomer does not exist
import static com.core.domain.model.customer.QCustomer.Constants.customer;
                                                                             ^
ART-Impl/src/main/java/com/core/domain/model/customer/CustomersRepository.java:18: error: cannot find symbol
import com.core.domain.composite.QCustomerComposite;

当前POM配置如下:

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.20.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <addOutputDirectory>false</addOutputDirectory>
                <outputDirectory>${delombok.dir}</outputDirectory>
                <sourceDirectory>${src.dir}</sourceDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.eclipse.xtext</groupId>
    <artifactId>xtext-maven-plugin</artifactId>
    <configuration>
        <compilerSourceLevel>11</compilerSourceLevel>
        <compilerTargetLevel>11</compilerTargetLevel>
    </configuration>
    <executions>
        <execution>
            <id>xtext.generate.sources</id>
            <configuration>
                <javaSourceRoots>
                    <sourceRoot>${delombok.dir}</sourceRoot>
                </javaSourceRoots>
                <sourceRoots>
                    <sourceRoot>${delombok.dir}</sourceRoot>
                    <sourceRoot>
                        ${project.basedir}/src/main/resources</sourceRoot>
                    <sourceRoot>
                        ${project.build.directory}/generated-sources/java</sourceRoot>
                </sourceRoots>
            </configuration>
        </execution>
        <execution>
            <id>xtext.generate.test.sources</id>
            <configuration>
                <javaSourceRoots>
                    <sourceRoot>${delombok.dir}</sourceRoot>
                </javaSourceRoots>
                <sourceRoots>
                    <sourceRoot>
                        ${project.basedir}/src/main/resources</sourceRoot>
                    <sourceRoot>
                        ${project.build.directory}/generated-sources/java</sourceRoot>
                    <sourceRoot>
                        ${project.build.directory}/generated-test-sources/java</sourceRoot>
                </sourceRoots>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    target/generated-sources/annotations</outputDirectory>
                <processor>
                    com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>
解决方案

核心是调整插件执行顺序并配置delombok的classpath,确保生成类在delombok执行前存在且可被访问。

  • 调整插件顺序:在POM中把QueryDSL的apt-maven-plugin和Xtext的xtext-maven-plugin移到Lombok插件前面。Maven在generate-sources阶段会按插件声明顺序执行,这样QueryDSL的Q类和Xtext生成类会先被创建,delombok执行时就能找到这些类。

  • 配置delombok的classpath:给lombok-maven-plugin添加生成类目录到classpath元素,让delombok能访问到提前生成的类。修改后的Lombok插件配置如下:

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.20.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
            <configuration>
                <addOutputDirectory>false</addOutputDirectory>
                <outputDirectory>${delombok.dir}</outputDirectory>
                <sourceDirectory>${src.dir}</sourceDirectory>
                <!-- 加入生成类目录,让delombok能找到Q类和Xtext生成类 -->
                <classpathElements>
                    <classpathElement>${project.build.directory}/generated-sources/annotations</classpathElement>
                    <classpathElement>${project.build.directory}/generated-sources/java</classpathElement>
                </classpathElements>
            </configuration>
        </execution>
    </executions>
</plugin>
  • 验证Xtext配置:确认Xtext插件的<sourceRoots>已包含generated-sources/java目录,确保Xtext生成的类目录被正确识别,这样delombok执行时能找到这些类。

内容的提问来源于stack exchange,提问作者Sash HEY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:15:10