3

I have some test-classes using Querydsl for my unit tests. Querydsl normally generates a query type class but if the classes are in the test directory the class will not be generated. As soon as I put the files in the src/main and compile with maven it works fine. 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/main/resources
src/test/java/
src/test/resources

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.

Maven APT plugin which generates the query types used by 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>
subsauber
  • 31
  • 3
  • Please share your directory structure along with your maven logs – Afgan Apr 26 '18 at 13:03
  • I edited my post, but I'm not sure what exactly do you mean by "your maven logs". Sorry working with maven is really new to me. – subsauber Apr 26 '18 at 13:27
  • what is your command to start the Maven build? – Alexis Dufrenoy Apr 26 '18 at 15:18
  • In order to start the maven build, I simply click on compile in the ''maven Lifecycle'' (I think the corresponding command is mvn compile). I hope that helps. So I need another command to compile if the files are in the test folder? – subsauber Apr 26 '18 at 16:11
  • try to compile files from cmd, with -> mvn clean install – Afgan Apr 27 '18 at 06:48
  • Unfortunately, it doesn't work. I get a compilation error: cannot find symbol QTestEntity (that is the generated class). – subsauber Apr 27 '18 at 08:57
  • Maven distinguishes between test and non-test [phases](https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html). By default `mvn compile` will not compile test-code or generate sources for test code. Use `mvn test-compile` to trigger generation of source and compile the genrated test sources. Also make sure that your DSL2Java generation is triggered in the right phase – Roman Vottner Apr 27 '18 at 13:34
  • 1
    I tried mvn test-compile but I get the same error: cannot find symbol QTestEntit. How can I check if my DSL2Java generation is triggered in the right phase ? In my "Question Post" I have added the plugin which should generate the query type class. Can I modify this in order to "trigger the generation in the right phase" ? – subsauber Apr 27 '18 at 15:41

1 Answers1

1

Can across this same issue. I found this solution to also generate the 'Q' entities for test sources. 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).

        <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.

Joost den Boer
  • 4,556
  • 4
  • 25
  • 39