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

Maven为何不从test-classes生成源码?Querydsl测试类生成问题求助

这个问题我之前踩过坑!默认情况下Querydsl的Maven插件只会扫描src/main/java下的实体类来生成查询类型,所以test目录下的实体自然不会被处理。要解决这个问题,只需要调整插件配置,让它也关照到test目录的内容就行,给你两个实用的方案:

方案一:让Querydsl插件同时处理main和test目录

修改你的Maven pom.xml,给Querydsl插件添加test目录的扫描路径,同时指定生成类输出到test专属的目录,避免和main的代码混在一起:

<build>
    <plugins>
        <!-- Querydsl代码生成插件 -->
        <plugin>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-maven-plugin</artifactId>
            <version>你的Querydsl版本</version>
            <executions>
                <execution>
                    <goals>
                        <goal>export</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- 同时扫描main和test目录下的实体类 -->
                <sourcePaths>
                    <sourcePath>${project.basedir}/src/main/java</sourcePath>
                    <sourcePath>${project.basedir}/src/test/java</sourcePath>
                </sourcePaths>
                <!-- 生成的查询类输出到test的源码生成目录 -->
                <targetFolder>${project.build.directory}/generated-test-sources/java</targetFolder>
                <!-- 根据你的持久化框架调整,比如JPA填true,MongoDB填mongodb -->
                <jpa>true</jpa>
                <querydslVersion>你的Querydsl版本</querydslVersion>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>com.querydsl</groupId>
                    <artifactId>querydsl-jpa</artifactId>
                    <version>你的Querydsl版本</version>
                </dependency>
                <dependency>
                    <groupId>org.hibernate.javax.persistence</groupId>
                    <artifactId>hibernate-jpa-2.1-api</artifactId>
                    <version>1.0.2.Final</version>
                </dependency>
            </dependencies>
        </plugin>
        <!-- 让Maven识别test目录下的生成源码 -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.3.0</version>
            <executions>
                <execution>
                    <id>add-test-source</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>add-test-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.build.directory}/generated-test-sources/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

方案二:单独为test目录配置生成任务

如果你的test实体和main实体完全独立,也可以给插件单独加一个针对test目录的执行任务,逻辑更清晰:

<plugin>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-maven-plugin</artifactId>
    <version>你的Querydsl版本</version>
    <executions>
        <!-- 处理main目录的默认任务 -->
        <execution>
            <id>export-main</id>
            <goals>
                <goal>export</goal>
            </goals>
            <configuration>
                <sourcePaths>
                    <sourcePath>${project.basedir}/src/main/java</sourcePath>
                </sourcePaths>
                <targetFolder>${project.build.directory}/generated-sources/java</targetFolder>
                <jpa>true</jpa>
            </configuration>
        </execution>
        <!-- 新增处理test目录的任务 -->
        <execution>
            <id>export-test</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>export</goal>
            </goals>
            <configuration>
                <sourcePaths>
                    <sourcePath>${project.basedir}/src/test/java</sourcePath>
                </sourcePaths>
                <targetFolder>${project.build.directory}/generated-test-sources/java</targetFolder>
                <jpa>true</jpa>
            </configuration>
        </execution>
    </executions>
    <!-- 依赖配置和方案一一致 -->
</plugin>

小提醒

test目录下的实体类一定要加上Querydsl能识别的注解:比如JPA实体加@Entity,普通测试DTO加@QueryEntity,不然插件会直接跳过这些类哦。

配置完成后,执行mvn clean test或者单独跑mvn generate-test-sources,就能在target/generated-test-sources/java下看到生成的查询类了,测试代码直接引用就行,完全不用把测试相关的实体移到main目录里~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:44:46