如何在 Kotlin 中使用 QueryDSL 并生成文件

How to user QueryDSL and generate file in Kotlin

我使用下面的 maven pom.xml 文件,但无法生成 querydsl 文件。

我找到了一个问题:

我想从 kotlin 实体生成 jpa querydsl 文件 类。

在线有一个很好的例子说明如何使用 gradle https://github.com/JetBrains/kotlin-examples/blob/master/gradle/kotlin-querydsl/build.gradle.

生成 dsl 文件

但是我曾尝试在 Maven 中实现它,但没有成功。我当前的 pom 在下面。有谁知道问题可能是什么?提前致谢。

它用querydsl3,我用4

<?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/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>cn.techcave.chat</groupId>
 <artifactId>jpa</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>jpa</name>
 <description>Kotlin Demo project for Spring Boot JPA</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <kotlin.version>1.2.10</kotlin.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-rest</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-rest-hal-browser</artifactId>
  </dependency>
  <dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-stdlib-jdk8</artifactId>
   <version>${kotlin.version}</version>
  </dependency>
  <dependency>
   <groupId>org.jetbrains.kotlin</groupId>
   <artifactId>kotlin-reflect</artifactId>
   <version>${kotlin.version}</version>
  </dependency>

  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <scope>runtime</scope>
  </dependency>

  <!--swagger 2-->
  <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.8.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.8.0</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/io.springfox/springfox-data-rest -->
  <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-data-rest</artifactId>
   <version>2.8.0</version>
  </dependency>

  <dependency>
   <groupId>com.querydsl</groupId>
   <artifactId>querydsl-apt</artifactId>
   <version>4.1.4</version>
  </dependency>
  <dependency>
   <groupId>com.querydsl</groupId>
   <artifactId>querydsl-jpa</artifactId>
   <version>4.1.4</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>javax.xml.bind</groupId>
   <artifactId>jaxb-api</artifactId>
   <version>2.3.0</version>
   <!--<scope>test</scope>-->
  </dependency>
 </dependencies>

 <build>
  <sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
  <testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <artifactId>kotlin-maven-plugin</artifactId>
    <groupId>org.jetbrains.kotlin</groupId>
    <version>${kotlin.version}</version>
    <configuration>
     <compilerPlugins>
      <plugin>spring</plugin>
     </compilerPlugins>
     <jvmTarget>1.8</jvmTarget>
    </configuration>
    <executions>
                    <execution>
                        <id>kapt</id>
                        <goals>
                            <goal>kapt</goal>
                        </goals>
                        <configuration>
                            <sourceDirs>
                                <sourceDir>/src/main/kotlin</sourceDir>
                                <!--<sourceDir>${project.basedir}/src/main/java</sourceDir>-->
                            </sourceDirs>
                            <annotationProcessorPaths>
                                <annotationProcessorPath>
                                    <groupId>com.querydsl</groupId>
                                    <artifactId>querydsl-apt</artifactId>
                                    <version>4.1.4</version>
                                    <classifier>jpa</classifier>
                                </annotationProcessorPath>
                            </annotationProcessorPaths>
                            <!--<annotationProcessors>-->
                                <!--<outputDirectory>target/generated-sources/java</outputDirectory>-->
                                <!--<processor>com.querydsl.apt.QuerydslAnnotationProcessor</processor>-->
                            <!--</annotationProcessors>-->
                        </configuration>
                    </execution>
     <execution>
      <id>compile</id>
      <phase>compile</phase>
      <goals>
       <goal>compile</goal>
      </goals>
     </execution>
     <execution>
      <id>test-compile</id>
      <phase>test-compile</phase>
      <goals>
       <goal>test-compile</goal>
      </goals>
     </execution>
    </executions>
    <dependencies>
     <dependency>
      <groupId>org.jetbrains.kotlin</groupId>
      <artifactId>kotlin-maven-allopen</artifactId>
      <version>${kotlin.version}</version>
     </dependency>
    </dependencies>
   </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>target/generated-sources/java</outputDirectory>-->
       <!--<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>-->
      <!--</configuration>-->
     <!--</execution>-->
    <!--</executions>-->
   <!--</plugin>-->
  </plugins>
 </build>


</project>

谢谢

据我所知,这是不可能的,因为 QueryDSL 插件分析的是源代码,而不是字节码。当然,它无法理解 kotlin 类.

的来源

所以我看到的唯一方法是将实体 类 移动到单独的模块并在 java 中重写它们。

我今天 运行 进入这个。游戏中存在一些问题:

  1. "kapt" 插件挂钩 "compile" 阶段,因此您在那里的任何编译器插件 (no-arg/JPA/Spring) 都会覆盖它。您可以注释掉 <compilerPlugins> 块中的 <jpa> 行,它应该吐出您的 Q 类,但是您会丢失实体 类' 无参数构造函数。
  2. 即使你让它工作,在 Kotlin 1.2.20 版本之前,由于 kapt 在遇到已经存在的生成的输出文件时会出错,所以存在一个阻止后续编译 运行 的错误。这在 1.2.20 中已修复。
  3. 可以将 <compilerPlugins> 块从顶部 <plugin> 级别向下移动到 "compile" 执行,但这实际上导致插件被忽略。我的项目可以编译,但由于 Spring 配置 类 被视为最终版本而我的实体 类 缺少无参数默认构造函数,因此在运行时失败。

我找到的解决方案是将 kotlin-maven-plugin 的 <compile> 执行从 compile 阶段移动到 process-sources 阶段,例如:

<build>       
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>


    <plugins>
        <plugin>
            <artifactId>kotlin-maven-plugin</artifactId>
            <groupId>org.jetbrains.kotlin</groupId>
            <version>${kotlin.version}</version>
            <configuration>
                <jvmTarget>1.8</jvmTarget>
                <compilerPlugins>
                    <plugin>spring</plugin>
                    <plugin>jpa</plugin>
                </compilerPlugins>
            </configuration>

            <executions>

                <execution>
                    <id>compile</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>

                <execution>
                    <id>kapt</id>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/main/kotlin</sourceDir>
                        </sourceDirs>
                        <annotationProcessorPaths>
                            <annotationProcessorPath>
                                <groupId>com.querydsl</groupId>
                                <artifactId>querydsl-apt</artifactId>
                                <version>${querydsl.version}</version>
                                <classifier>jpa</classifier>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/test/kotlin</sourceDir>
                            <sourceDir>target/generated-sources/kapt/test</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-noarg</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

这对我在 Spring 使用 Spring-Data-JPA 和 Spring-Data-REST 的启动项目中有效。

N. DaSilva 的示例对我不起作用,因为在 kapt 执行中缺少 "generate-sources" 阶段。下面的配置解决了这个问题。 (抱歉,信誉不足,无法在评论中报告)。

             <executions>
                <execution>
                    <id>compile</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>kapt</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>kapt</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>src/main/kotlin</sourceDir>
                        </sourceDirs>
                        <annotationProcessorPaths>
                            <annotationProcessorPath>
                                <groupId>com.querydsl</groupId>
                                <artifactId>querydsl-apt</artifactId>
                                <version>${querydsl.version}</version>
                                <classifier>jpa</classifier>
                            </annotationProcessorPath>
                        </annotationProcessorPaths>
                    </configuration>
                </execution>
            </executions>