119

enter image description here

Can not resolve configuration property '...

I have no problem accessing my properties through the @Value annotation or through an autowired Evironment. But all of my own defined properties get this warning in IDEA. What should I be doing to get IDEA to recognize these and not bother me?

Stoopkid
  • 1,885
  • 2
  • 17
  • 30
  • 2
    Don't worry about it; IntelliJ has a bit of a time recognizing custom properties anyway. At best you could file a bug with JetBrains. – Makoto Feb 23 '18 at 18:25
  • 1
    You could check which facets are associated with your project. Try ctrl + alt + shift + s, and look for facets. You might have to manually add a spring (boot) facet for intellij to understand.. – Tobb Feb 23 '18 at 19:24
  • I added the spring facet and there was no change. But I do not have Spring(boot) facet in my list. Maybe because I am on a 2016 version of IDEA. – Stoopkid Feb 23 '18 at 23:31
  • Is it possible to provide sample project example for investigation? – y.bedrov Feb 26 '18 at 07:11
  • 1
    try installing Spring Assistant plugin. – Keaz Jul 13 '18 at 03:54
  • This problem persists in intellij 2021.1. I am just going to ignore it. – Beezer Jul 19 '21 at 16:36

6 Answers6

121

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.

Igor Akkerman
  • 2,348
  • 1
  • 21
  • 24
  • 5
    This is exactly what I needed! I went with option 2! – MaxPower Jul 03 '18 at 21:30
  • 2
    FYI. Spring Reference > Appendix B. Configuration Metadata : https://docs.spring.io/spring-boot/docs/current/reference/html/configuration-metadata.html – Namo Dec 13 '18 at 08:12
  • 4
    Select Build > Rebuild Project from the menu had fixed my issue – Youness Marhrani Jan 08 '19 at 15:27
  • I wrote properties in applicaition.property and use in dependency(other jar). Can it works? – fjjiaboming Apr 04 '19 at 09:29
  • 6
    I strongly recommend to use option 1 (`@ConfigurationProperties` and annotation processor), since it saves you tedious and error-prone handwriting of meta data files. And, after the initial setup, it is even easier to use than `@Value` annotations. – deamon May 02 '19 at 08:21
  • I suggest removing this ridiculous warning at the root: IntelliJ itself. My properties are DEFINITIONS so why is the IDEA complaining that the thing I am DEFINING does not exist? Do I really need to resort to more meta-data friction, cost and complexity just to suppress an unnecessary warning? – Rick O'Shea Jan 05 '20 at 19:41
  • 1
    @Namo link is broken, I think new one is [Configuring the Annotation Processor](https://docs.spring.io/spring-boot/docs/2.4.1/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor-setup). – Marco Lackovic May 21 '21 at 13:54
  • 1
    Option 2 is nice because it allows you to specify descriptions that actually tell what those configuration fields are for. It can be really helpful instead of needing to declare that information somewhere else like Confluence or within the README. – wheeleruniverse Apr 20 '22 at 17:30
  • Still does not help with Kotlin projects. The same configuration with `@ConfigurationProperties` is working perfectly in the Java project, without any additional IntelliJ setup. For Kotlin project, nothing is helping :( – Dmitriy Popov May 08 '23 at 14:12
4

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.

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
  • 1
    Thanks for the great answer. I noticed a few things: 1. I only needed to add the dependencies, I didn't need to specify the annotation processor class in order for the files to be generated. 2. I didn't need `dependsOn(processResources)`, I couldn't even import `processResources` to begin with. 3. My IntelliJ IDEA doesn't complain if I leave out `annotationProcessor(configurationProcessor)`. Can you confirm these observations? – Igor Akkerman Mar 10 '21 at 19:42
1

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)
ToTo
  • 89
  • 6
0

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 :-/

GreenSaguaro
  • 2,968
  • 2
  • 22
  • 41
0

As an additional requirement for the answers above. After Spring-boot 2.2 you can use final keyword on the attributes together with the annotation @ConstructorBinding in order to see IntelliJ auto-complete the property name on the application.properties file. IntelliJ also recognizes if you add a java docs on the attribute.

@ConfigurationProperties(prefix = "my-config")
@ConstructorBinding
public class ConfigImportService {

    /**
     * the name of the bucket 
     * (IntelliJ shows this comment on the application.properties file)
     */
    private final String bucketName;
    private final String databaseName;

    @Autowired
    public ConfigImportService(
        @Value("${bucket.name}") String bucketName,
        @Value("${db.name}") String databaseName
    ) {
        this.bucketName = bucketName;
        this.databaseName = databaseName;
    }
}

And still necessary the dependency, of course.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
Felipe
  • 7,013
  • 8
  • 44
  • 102
0

Spring Voot 3+ in application.properties u can use

spring.ldap.urls=192.168.1.1 #instead of spring.ldap.url
spring.ldap.base=dc=example,dc=com
spring.ldap.username=uid=admin,ou=system
spring.ldap.password=secret