简体   繁体   English

为什么maven不会从测试类生成源代码?

[英]Why does maven not generate sources from test-classes?

I have some test-classes using Querydsl for my unit tests. 我有一些使用Querydsl进行单元测试的测试类。 Querydsl normally generates a query type class but if the classes are in the test directory the class will not be generated. Querydsl通常会生成一个查询类型类,但如果这些类位于测试目录中,则不会生成该类。 As soon as I put the files in the src/main and compile with maven it works fine. 只要我将文件放在src / main中并使用maven编译就可以了。 Because I use the classes only in my tests I don't want to put these files in this directory. 因为我只在我的测试中使用这些类,所以我不想将这些文件放在这个目录中。 Can someone help me with that ? 有人可以帮助我吗?
My directory structure 我的目录结构
src/main/java 的src /主/ JAVA
src/main/resources 的src / main /资源
src/test/java/ 的src /测试/ JAVA /
src/test/resources SRC /测试/资源

As I said putting the files in this directory "src/main/java" the query type class will be generated, but putting the files in this directory src/test/java/ the type class will not be generated. 正如我所说的那样将文件放在这个目录“src / main / java”中,将生成查询类型类,但是不会生成将文件放在src / test / java /类型类的目录中。

Maven APT plugin which generates the query types used by Querydsl Maven APT插件,用于生成Querydsl使用的查询类型

            <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-test-sources/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>4.1.0</version>
                </dependency>
            </dependencies>
        </plugin>

Can across this same issue. 可以跨越同样的问题。 I found this solution to also generate the 'Q' entities for test sources. 我发现这个解决方案也为测试源生成'Q'实体。 The trick was to use the test-process goal of the plugin. 诀窍是使用插件的测试过程目标。 Adding it to the generate-test-sources phase generates them at the correct time (test-compile). 将其添加到generate-test-sources阶段会在正确的时间生成它们(测试编译)。

        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>${apt.maven.plugin.version}</version>
            <executions>
                <execution>
                    <id>generate-source-entities</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
                <execution>
                    <id>generate-test-entities</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>test-process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/generated</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Note this generates the entities for both the main and test sources. 请注意,这会为主要源和测试源生成实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM