Intellij Idea IntelliJ无法识别KotlinnoArg插件

8yoxcaq7  于 2023-04-11  发布在  Kotlin
关注(0)|答案(4)|浏览(252)

我不确定我是否错过了一个配置或偶然发现了一个bug。我正在使用IntelliJ构建一个带有JPA的KotlinSping Boot 应用程序,并希望使用Kotlin noArg插件来减少实体中的样板文件。
使用下面的build.gradle.kts,我的应用程序编译正常,但IntellJ用错误Class 'User' should have [public, protected] no-arg constructor强调了我的实体。
我可以在IntelliJ或build.gradle中设置一些东西来消 debugging 误吗?
系统规格:

  • Windows 11 + WSL
  • IntelliJ IDEA 2021.3.1(终极版)
  • Kotlin插件213-1.6.10-release-944-IJ6461.79
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.6.4"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    id("org.jetbrains.kotlin.plugin.noarg") version "1.6.10"
    kotlin("jvm") version "1.6.10"
    kotlin("plugin.spring") version "1.6.10"
    kotlin("plugin.jpa") version "1.6.10"
}

group = "XXXXXX"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_16

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-security")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.h2database:h2")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    testImplementation("org.springframework.security:spring-security-test")
}

noArg {
    annotation("javax.persistence.Entity")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "16"
    }
}

ttygqcqt

ttygqcqt1#

我也遇到了同样的问题,但是在我的Maven项目中。这个东西在<executions>块中。可能IDEA还不理解这么复杂的配置。我删除了它,保留了这个简单的配置。

<plugin>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-maven-plugin</artifactId>
    <version>${kotlin.version}</version>
    <configuration>
        <sourceDirs>
            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
            <sourceDir>${project.basedir}/src/main/java</sourceDir>
        </sourceDirs>
        <compilerPlugins>
            <plugin>jpa</plugin>
            <plugin>all-open</plugin>
            <plugin>spring</plugin>
        </compilerPlugins>
        <pluginOptions>
            <option>all-open:annotation=javax.persistence.Entity</option>
            <option>all-open:annotation=javax.persistence.Embeddable</option>
            <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
        </pluginOptions>
        <args>
            <!-- https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-kotlin.html#boot-features-kotlin-null-safety -->
            <arg>-Xjsr305=strict</arg>
        </args>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-noarg</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-allopen</artifactId>
            <version>${kotlin.version}</version>
        </dependency>
    </dependencies>
</plugin>
ymzxtsji

ymzxtsji2#

原来这是运行在基于WSL的项目上的IntelliJ中的一个错误。
我在这里跟踪错误https://youtrack.jetbrains.com/issue/KTIJ-21314/,一旦解决,我会更新答案。

9q78igpj

9q78igpj3#

你只需要重新加载项目,对我来说,它在一个maven项目中工作。

83qze16e

83qze16e4#

如果您使用新版本java,javax将更改为jakarta
Java Persistence API(JPA),2019年更名为Jakarta Persistence
因此您可能需要像这样配置pom.xml文件

<configuration>
                <compilerPlugins>
                    <plugin>spring</plugin>
                    <plugin>jpa</plugin>
                    <plugin>no-arg</plugin>
                </compilerPlugins>
                <pluginOptions>
                    <option>no-arg:annotation=jakarta.persistence.Entity</option>
                </pluginOptions>
                <jvmTarget>17</jvmTarget>
            </configuration>

和插件依赖关系

<dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-noarg</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>

相关问题