我按照这个示例 https://felixzett.com/articles/minimal-maven-kotlin-querydsl-example/ 在 Springboot Kotlin Maven 项目中实现 querydsl。使用提到的 pom,我在运行
mvn compile
后成功生成了 q 类,并且也可以成功运行 mvn clean install
,但是当我尝试在本地运行该项目时,所有 q 类中都出现错误。
java: cannot find symbol
symbol: class Generated
location: package javax.annotation.processing
我尝试将 javax.annotation-api 添加到
<annotationProcessPaths>
元素,但它不起作用。
<annotationProcessorPath>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</annotationProcessorPath>
我还尝试将其添加为依赖项,在该
<plugin>
元素或主 <dependencies>
元素中,但这些效果不佳。
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
请问有人知道如何解决这个问题吗?
更新:
我的 pom 中有 kotlin-maven-plugin 和 maven-compiler-plugin。我意识到如果我注释掉 maven-compiler-plugin 那么错误就会消失。但我不确定如果没有这个插件,应用程序的其他部分是否会受到影响。
有谁知道只使用 kotlin-maven-plugin 是否可以,或者我应该如何修改 maven-compiler-plugin 以使这两个插件一起工作?
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<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>
<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>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=javax.persistence.Entity</option>
<option>all-open:annotation=javax.persistence.MappedSuperclass</option>
<option>all-open:annotation=javax.persistence.Embeddable</option>
</pluginOptions>
<jvmTarget>1.8</jvmTarget>
</configuration>
<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>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<groupId>org.apache.maven.plugins</groupId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
似乎是由于maven-compiler-plugin中的Java版本所致。当我从 1.8 更改为 11 后,即使我同时拥有 kotlin-maven-plugin 和 maven-compiler-plugin,我也不会收到错误。
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
还要在
<properties>
元素中将 java 版本更改为 11。
<properties>
<java.version>11</java.version>
</properties>
将以下代码添加到 apt-maven-plugin 中:
<options>
<querydsl.generatedAnnotationClass>javax.annotation.Generated</querydsl.generatedAnnotationClass>
</options>
类似:
<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>
<options>
<querydsl.generatedAnnotationClass>javax.annotation.Generated</querydsl.generatedAnnotationClass>
</options>
</configuration>
</execution>
参考:https://github.com/redhat-developer/vscode-java/issues/2799