Intellij IDEA complains cannot resolve spring boot properties but they work fine

后端 未结 5 545
囚心锁ツ
囚心锁ツ 2020-12-24 00:31

Can not resolve configuration property \'...

I have no problem accessing my properties through the @Value annotation or through

相关标签:
5条回答
  • 2020-12-24 00:31

    A Gradle-based workaround is this:

    afterEvaluate {
        val kaptKotlinTasks = tasks.named("kaptKotlin") {
            doLast {
                val kaptKotlin = this
                tasks.named<ProcessResources>("processResources") {
                    from(kaptKotlin.outputs) {
                        include("META-INF/spring-configuration-metadata.json")
                    }
                }
            }
        }
        tasks.named("processResources") {
            this.dependsOn(kaptKotlinTasks)
        }
    }
    

    After running a build (or just the processResources task) from the Intellij Gradle panel, the warnings about the properties should disappear.

    Not ideal, but IntelliJ not supporting kapt is not ideal either :-/

    0 讨论(0)
  • 2020-12-24 00:33

    I had the same problem plus not showing auto completion found out that it works with IntelliJ Ultimate edition and not community version. link

    couple of useful steps to take would be:

    • adding Maven dependency:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    
    • refresh Maven to download dependencies.
    • coping exact name of property from target/classes/META-INF/spring-configuration-metadata.js to prevent errors.
    • making sure that your config class is annotated with @ConfigurationProperties("name-here") and that you have enabled it by @EnableConfigurationProperties(NameOfTheConfigClass.class)
    0 讨论(0)
  • 2020-12-24 00:47

    You turn off the inspection in the IntelliJ Settings.

    0 讨论(0)
  • 2020-12-24 00:52

    In order for IntelliJ IDEA to know your Spring Boot properties, you can define Spring Boot configuration metadata in your project.

    Option 1:

    If you can use a @ConfigurationProperties-annotated class for your properties, you can add the Spring Boot configuration annotation processor to your classpath and IntelliJ IDEA will generate the configuration metadata for you in target or out:

    Maven:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    

    Gradle:

    implementation 'org.springframework.boot:spring-boot-configuration-processor'
    

    Option 2:

    Create the configuration metadata file yourself src/main/resources/META-INF/spring-configuration-metadata.json:

    Content:

    {
      "properties": [
        {
          "name": "myapp.someprop",
          "type": "java.lang.String"
        },
        {
          "name": "myapp.someintprop",
          "type": "java.lang.Integer"
        }
      ]
    }
    

    Options 1 and 2:

    In the IntelliJ IDEA tool window of your build system (Maven/Gradle), click the "Refresh" button.

    Select Build > Rebuild Project from the menu.

    If the warning still appears, you can try to restart the IDE. Select File > Invalidate Caches / Restart and click on Invalidate and Restart.

    0 讨论(0)
  • 2020-12-24 00:56

    Please use the following for Gradle Kotlin Script for Kotlin project:

    plugins {
        kotlin("jvm")
        kotlin("kapt")
    }
    
    /* ... */
    
    dependencies {
        val configurationProcessor ="org.springframework.boot:spring-boot-configuration-processor:${BuildConstants.springBootVersion}"
    
        kapt(configurationProcessor) // for jar
        kaptTest(configurationProcessor) // for jar
        annotationProcessor(configurationProcessor) // for IntelliJ Idea
    }
    
    /* ... */
    
    kapt {
        annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
    }
    
    /* ... */
    
    tasks {
        withType<KotlinCompile> {
            dependsOn(processResources)
        }
    }
    

    Kotlin Kapt is needed to work with metadata and memory.

    From official Spring documentation, Spring Boot Configuration Processor generates special json file with properties metadata.

    Therefore, to distribute jar with property syntax highlight you need:

    • Ask Gradle to generate this file
    • Update task sequence to generate file before jar packaging by using dependsOn (not sure, that my code above is the most effective solution, however problem is solved)

    However IntelliJ Idea works with annotationProcessor Gradle configuration (unfortunately, I don't have exact answer, why it requires exact it). Therefore you need add the same processor into the annotationProcessor configuration as well.

    0 讨论(0)
提交回复
热议问题