Java Querydsl maven 编译错误:QClass.class 不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23040620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:15:33  来源:igfitidea点击:

Querydsl maven compilation error: QClass.class does not exist

javaspringmavenquerydsl

提问by Frey Zheng

I am new to Querydsl and trying to use it in a simple test project. I followed the official tutorial to configure my pom.xml, then mvn clean install was able to generated the Q Classes under target/generated-sources/java. But I got the error below:

我是 Querydsl 的新手,并试图在一个简单的测试项目中使用它。我按照官方教程配置了我的 pom.xml,然后 mvn clean install 能够在 target/generated-sources/java 下生成 Q Classes。但我收到以下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-jqgrid-tutorial: Compilation failure

[ERROR] /D:/Project/spring-jqgrid-tutorial/src/main/java/org/krams/controller/UserController.java:[92,62] package QUser.user does not exist

I think the root cause is the generated Q class source files were not compiled automatically into binary class files. I did verify there are no QUser.class under my project directory. I also tried to use build-helper-maven-plugin to add target/generated-sources/java as a source folder and specify target/generated-sources/java as an additional source root in apt-maven-plugin configuration. But I got no luck.

我认为根本原因是生成的Q类源文件没有自动编译成二进制类文件。我确实验证了我的项目目录下没有 QUser.class 。我还尝试使用 build-helper-maven-plugin 添加 target/generated-sources/java 作为源文件夹,并在 apt-maven-plugin 配置中指定 target/generated-sources/java 作为额外的源根。但我没有运气。

Here is my pom.xml

这是我的 pom.xml

<properties>
    <querydsl.version>3.3.2</querydsl.version>
    <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version>
    <maven.apt.plugin.version>1.1.1</maven.apt.plugin.version
    <maven.build.helper.plugin.version>1.8</maven.build.helper.plugin.version>
<properties>

<dependencies>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.version}</version>
        </dependency>
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>${querydsl.version}</version>
        <scope>provided</scope>
    </dependency>
 <dependencies>

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
                <version>${maven.build.helper.plugin.version}</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>target/generated-sources/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>${maven.apt.plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/java</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        <additionalSourceRoots>
                            <additionalSourceRoot>target/generated-sources/java</additionalSourceRoot>
                        </additionalSourceRoots>
                    </configuration>
                </execution>
            </executions>
        </plugin>
<plugins>

And here is the generated QUser.java

这是生成的 QUser.java

package org.krams.domain;

import static com.mysema.query.types.PathMetadataFactory.*;

import com.mysema.query.types.path.*;

import com.mysema.query.types.PathMetadata;
import javax.annotation.Generated;
import com.mysema.query.types.Path;
import com.mysema.query.types.path.PathInits;


/**
 * QUser is a Querydsl query type for User
 */
@Generated("com.mysema.query.codegen.EntitySerializer")
public class QUser extends EntityPathBase<User> {

    private static final long serialVersionUID = -1712499619L;

    private static final PathInits INITS = PathInits.DIRECT2;

    public static final QUser user = new QUser("user");

    public final NumberPath<Integer> age = createNumber("age", Integer.class);

    public final StringPath firstName = createString("firstName");

    public final NumberPath<Long> id = createNumber("id", Long.class);

    public final StringPath lastName = createString("lastName");

    public final StringPath password = createString("password");

    public final QRole role;

    public final StringPath username = createString("username");

    public QUser(String variable) {
        this(User.class, forVariable(variable), INITS);
    }

    public QUser(Path<? extends User> path) {
        this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QUser(PathMetadata<?> metadata) {
        this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT);
    }

    public QUser(PathMetadata<?> metadata, PathInits inits) {
        this(User.class, metadata, inits);
    }

    public QUser(Class<? extends User> type, PathMetadata<?> metadata, PathInits inits) {
        super(type, metadata, inits);
        this.role = inits.isInitialized("role") ? new QRole(forProperty("role"), inits.get("role")) : null;
    }

}

Below is the only reference to QUser in UserController.java. repository is an instance of UserRepository repository, which extends QueryDslPredicateExecutor.

下面是 UserController.java 中对 QUser 的唯一引用。存储库是 UserRepository 存储库的一个实例,它扩展了 QueryDslPredicateExecutor。

package org.krams.controller;

import java.util.List;

import org.krams.domain.QUser;
import org.krams.domain.Role;
import org.krams.domain.User;
import org.krams.repository.UserRepository;
import org.krams.response.JqgridResponse;
import org.krams.response.StatusResponse;
import org.krams.response.UserDto;
import org.krams.service.UserService;
import org.krams.util.JqgridFilter;
import org.krams.util.JqgridObjectMapper;
import org.krams.util.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

...

if (qUsername != null) 
            users = repository.findAll(QUser.user.username.like(qUsername), pageRequest);

Appreciate your help and comments. I can provide more information if required.

感谢您的帮助和评论。如果需要,我可以提供更多信息。

采纳答案by Frey Zheng

I built everything from scratch again and finally Querydsl is working. I think this problem was caused by incompatible versions of spring data jpa and querydsl. Now I am using spring data jpa 1.3.2.RELEASE and querydsl 2.8.0., everything works perfectly. P.S. I removed the additionalSourceRoots and the build-helper-maven-plugin config. They are unnecessary.

我再次从头开始构建所有内容,最终 Querydsl 开始工作。我认为这个问题是由 spring data jpa 和 querydsl 的版本不兼容引起的。现在我正在使用 spring data jpa 1.3.2.RELEASE 和 querydsl 2.8.0.,一切正常。PS 我删除了 additionalSourceRoots 和 build-helper-maven-plugin 配置。它们是不必要的。

回答by Niels Bech Nielsen

Not sure, but in my current project (set up by someone else) I have two extra folders, and they are tied in in the generate-sources phase..

不确定,但在我当前的项目(由其他人设置)中,我有两个额外的文件夹,它们在生成源阶段绑定..

Hope this helps.

希望这可以帮助。

  <!-- MYSEMA generates a lot of query classes for the model in a pre-process step -->
  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>${version.org.codehaus.mojo.build-helper-maven-plugin}</version>
    <executions>
      <execution>
        <id>add-source</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>add-source</goal>
        </goals>
        <configuration>
          <sources>
            <source>${project.build.directory}/generated-sources/apt/</source>
            <source>${project.build.directory}/generated-sources/annotations/</source>
          </sources>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>${version.com.mysema.maven.apt-maven-plugin}</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>

回答by Jeremy Fang

It work for me like this

它像这样对我有用

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/main/sources</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <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>src/main/sources</outputDirectory>
                        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.mysema.querydsl</groupId>
                    <artifactId>querydsl-apt</artifactId>
                    <version>3.4.3</version>
                </dependency>
            </dependencies>
        </plugin>