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

ruarlubt  于 2023-10-15  发布在  Spring
关注(0)|答案(3)|浏览(164)

我使用下面的maven pom.xml文件,但无法生成查询文件。
我发现了一个问题:Kotlin-Kapt Annotation Processor not working with maven
我想从Kotlin实体类生成jpa查询文件。
在线上有一个很好的例子,说明如何使用gradle https://github.com/JetBrains/kotlin-examples/blob/master/gradle/kotlin-querydsl/build.gradle生成JavaScript文件。
然而,我曾试图在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>

谢谢

pb3skfrl

pb3skfrl1#

我今天碰到了这个。有几个问题在起作用:
1.“kapt”插件挂接了“compile”阶段,所以你在那里的任何编译器插件(no-arg/JPA/Spring)都会覆盖它。您可以注解掉<compilerPlugins>块中的<jpa>行,它应该会吐出您的Q类,但这样您就丢失了实体类的无参数构造函数。
1.即使你能做到这一点,在Kotlin1.2.20版本之前,有一个bug会阻止后续的编译运行,因为当kapt遇到已经存在的生成的输出文件时,它会出错。这在1.2.20中已修复。
1.可以将<compilerPlugins>块从<plugin>顶层移到“编译”执行层,但这实际上导致插件被忽略。我的项目将编译,但在运行时失败,因为Spring config类被视为final,而我的实体类缺少no-arg默认构造函数。
我找到的解决方案是将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-Data-JPA和Spring-Data-REST的Sping Boot 项目。

ds97pgxw

ds97pgxw2#

AFAIK这是不可能的,因为QueryDSL插件分析源代码,而不是字节码。当然,它不能理解Kotlin类的源代码。
所以我认为你唯一的方法就是把实体类移到单独的模块中,然后用java重写它们。

a64a0gku

a64a0gku3#

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>

相关问题