Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query DSL & Maven: Classes not generated, but only on command line (in eclipse it is working fine)

We are using a setup with Spring Boot, Hibernate, Query DSL and Maven with Java 1.8

Recently, I've added Query DSL to the project with the configuration listed below. To make it work, I had to configure the Java Compiler in the eclipse project settings to allow Annotation Processing and also add the Query DSL .jar file to the eclipse Annotation Factory Path.

This setup worked as expected. It generated the custom Q classes and I could use them in my code. When now running the mvn clean install on the command line, every class in my code throws the error cannot find symbol, because the class is missing. Is there anything else I need to configure - similar to the .jar file in the eclipse settings - to make the build process work?

EDIT: This question is not a duplicate of this question because I did not ask why this error (cannot find a symbol) occurs but rather how to configure QueryDSL to also work on the command line.

EDIT2: I have now tried to integrate the build-helper-maven-plugin to use multiple source paths as an input. This did not help either. I also tried to generate the files into a src folder. It did not help either.

When I first compile the library in eclipse, the mvn compile goes through on the command line, but mvn clean compile still fails, because it just uses the compiled files of eclipse again. The apt-maven-plugin is executed, which can be seen just before the build process fails:

[INFO] --- apt-maven-plugin:1.1.3:process (default) @ project1 ---
[INFO]
[INFO] --- build-helper-maven-plugin:1.9.1:add-source (add-source) @ project1 ---
[INFO] Source directory: C:\Users\user1\git\project1\src\main\generated added.
[INFO]
[INFO] --- maven-processor-plugin:2.2.4:process (process) @ project1 ---
[ERROR] diagnostic: [...]

EDIT3: When I remove every import statement which is referring to the Q classes, the build process goes through (obviously). It is, however, remarkable, that the Q classes get compiled correctly in that case. They appear in the target folder as .class files as they should. Could it be, that the Q classes are compiled too late?


Here is an excerpt of the 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    [...]
    <prerequisites>
        <maven>3.0.0</maven>
    </prerequisites>

    <dependencies>
        [...]
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>4.1.3</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>4.1.3</version>
        </dependency>
    </dependencies>

    <build>
        <defaultGoal>spring-boot:run</defaultGoal>
        <plugins>
            [...]
            <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-sources/java</outputDirectory>
                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        [...]
    </build>
</project>

This is the configuration of the eclipse project settings:

enter image description here

This is the error message which is displayed in the console:

[INFO] --- maven-processor-plugin:2.2.4:process (process) @ project1 ---
[ERROR] diagnostic: C:\Users\user1\git\project1\src\main\java\com\project1\repository\UserRepositoryImpl.java:3: error: cannot find symbol
import static com.project1.domain.QUser.user;
                                     ^
  symbol:   class QUser
  location: package com.project1.domain

[ERROR] diagnostic: C:\Users\user1\git\project1\src\main\java\com\project1\repository\UserRepositoryImpl.java:3: error: static import only from classes and interfaces
import static com.project.domain.QUser.user;
^
like image 621
ssc-hrep3 Avatar asked May 30 '18 15:05

ssc-hrep3


People also ask

What is a query DSL?

Querydsl is an extensive Java framework, which allows for the generation of type-safe queries in a syntax similar to SQL. It currently has a wide range of support for various backends through the use of separate modules including JPA, JDO, SQL, Java collections, RDF, Lucene, Hibernate Search, and MongoDB.

What is a query in Elasticsearch?

A query is made up of two clauses − Leaf Query Clauses − These clauses are match, term or range, which look for a specific value in specific field. Compound Query Clauses − These queries are a combination of leaf query clauses and other compound queries to extract the desired information.


2 Answers

This is old question but this is how i find my solution, added classifier for jpa dependency:

    <!-- BEGIN: 'querydsl-jpa' -->
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl-jpa.version}</version>
        <classifier>apt</classifier>
    </dependency>
    <!-- END: 'querydsl-jpa' -->

My complete pom:

<!-- BEGIN: BUILD -->
<build>

    <!-- BEGIN: PLUGINS -->
    <plugins>

        <!-- BEGIN: apt-maven-plugin -->
        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>${apt.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/apt</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- END: apt-maven-plugin -->

    </plugins>
    <!-- END: PLUGINS -->

</build>
<!-- END: BUILD -->

<!-- BEGIN: DEPENDENCIES -->
<dependencies>

    <!-- *********************************************** -->
    <!-- BEGIN: 'QUERYDSL DEPENDENCIES'                  -->

    <!-- BEGIN: 'querydsl-apt' -->
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>${querydsl-apt.version}</version>
    </dependency>
    <!-- END: 'querydsl-apt' -->

    <!-- BEGIN: 'querydsl-jpa' -->
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl-jpa.version}</version>
        <classifier>apt</classifier>
    </dependency>
    <!-- END: 'querydsl-jpa' -->

    <!-- *********************************************** -->
    <!-- END: 'QUERYDSL DEPENDENCIES'                    -->


</dependencies>
like image 79
cmlonder Avatar answered Sep 30 '22 09:09

cmlonder


I would rather use profile to generate these Qclasses only when db change occurs.

cons:

-your diff in pull requests is clean when you don't change db schema because for each generation these files tend to generate differently for some reason (atleast in my case).

-you can manage witch of tables present in your db will have Qclasses (sometimes it is a pain when you forget to regenerate them after changing db schema)

-well not that it is lots of time . but builds are faster if You don't change schema and profile is turned off.

Try something like this and turn on profile when You want to generate changed schema Qclasses :

<properties>
    <whitelisted.tables>
        user_accunt,
        other
    </whitelisted.tables>
</properties>
<profiles>
    <profile>
        <id>generate</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.querydsl</groupId>
                    <artifactId>querydsl-maven-plugin</artifactId>
                    <version>${querydsl.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>export</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <jdbcDriver>org.postgresql.Driver</jdbcDriver>
                        <jdbcUrl>jdbc:postgresql://localhost:port/dbname</jdbcUrl>
                        <packageName>your.package.name.for.q</packageName>
                        <jdbcUser>dbusername</jdbcUser>
                        <jdbcPassword>dbpassword</jdbcPassword>
                        <targetFolder>${project.basedir}/src/main/java/</targetFolder>
                        <spatial>true</spatial>
                        <tableNamePattern>${whitelisted.tables}</tableNamePattern>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.postgresql</groupId>
                            <artifactId>postgresql</artifactId>
                            <version>42.1.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
like image 22
Szychan Avatar answered Sep 30 '22 10:09

Szychan