91
votes

I try to make completion for custom properties in Spring Boot.

I tried to create a simple project via IntelliJ IDEA 2016.3:

  1. Created a new Gradle project with Spring Boot Initializer (I haven't checked anything at all).
  2. Created a new class Properties.

When I annotated it with @ConfigurationProperties, the next notification has appeared: notification

The documentation said that I should add the following to my project:

dependencies {
    optional "org.springframework.boot:spring-boot-configuration-processor"
}

compileJava.dependsOn(processResources)

After that, I tried to rebuild the project and enable annotation processors in settings but the notification hasn't gone. Completion doesn't work too (I created a string my).

14
What Gradle version do you use? Does this answer help? - CrazyCoder
Now it works, but I forgot to add propdeps-plugin, so I cannot be sure if the new version of the IDE is the solution. Anyway, I upvoted your answer. - Feeco

14 Answers

58
votes

I had the same problem. I use idea 2017.2 and gradle 4.1, and some blog said you should add:

dependencies {
    optional "org.springframework.boot:spring-boot-configuration-processor"
}

But I changed it to this:

dependencies {
    compile "org.springframework.boot:spring-boot-configuration-processor"
}

And the warning is gone.

29
votes

According to the Spring Boot docs, the correct configuration since Gradle 4.6 is

dependencies {
    annotationProcessor group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    // ...
}

IntelliJ IDEA supports annotationProcessor scope since build 193.3382 (2019.3). Don't forget to enable annotation processing in IntelliJ IDEA settings.

15
votes

It happens to me for two reasons in IDEA:

  1. Double check if your setting is picked (enabled) in IDEA: Preferences->Annotation Processors->Enable annotation processing.
  2. After update your Idea, check your plugins and update them. It happens that plugins become incompatible with your new IDEA version, so just click to update them.
10
votes

I forgot to add propdeps-plugin. However, I remember that it didn't work for me even with the plugin on 2016.3, So as @CrazyCoder mentioned, try to downgrade Gradle or download the new 2017.1 version (details).
Also you may receive Re-run Spring Boot Configuration Annotation Processor to update generated metadata when you will solve this issue. For this, click Refresh all Gradle projects (in Gradle side menu).

9
votes

In maven project helps adding dependency spring-boot-configuration-processor and marking main class with @EnableConfigurationProperties(AppProperties.class).

Maybe somebody helps.

7
votes

For Kotlin projects, the working configuration since Gradle 4.6 is using annotation processor

apply plugin: "kotlin-kapt"

dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
    compileOnly("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
}
6
votes

In version 2018.3 of IntelliJ, I solved this problem (as per this documentation) in the following way:

With Gradle 4.5 and earlier, the dependency should be declared in the compileOnly configuration, as shown in the following example:

dependencies {
  compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}

With Gradle 4.6 and later, the dependency should be declared in the annotationProcessor configuration, as shown in the following example:

dependencies {
  annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
6
votes

For those who are using maven, Intellij was still not happy with the addition of dependency. Seems like adding annotationProcessorPaths via maven-compiler-plugin finally tamed the beast.

Make sure the version matches your spring dependencies. I suspect it would be already present in your effective POM.

Reason: I was using a custom parent-pom which had a mapstruct annotation processor set in annotationProcessorPaths and that actually triggered IntelliJ to ask for all other annotation processors to be specified manually as well.

<build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <annotationProcessorPaths>
        <path>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-configuration-processor</artifactId>
          <version>2.0.4.RELEASE</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
</plugins>
</build>
5
votes

For those using Maven or Gradle, just add the dependency on spring-boot-configuration-processor.

Maven:

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

Gradle 4.5 and earlier

dependencies {
     compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}

Gradle 4.6 and later

dependencies {
      annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}

For more information: "Generating Your Own Metadata by Using the Annotation Processor" https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor

2
votes

I had the same problem with IntelliJ version 2018.1.2. I also had to define the actual version of spring-boot-configuration-processor in order to get it worked:

compile('org.springframework.boot:spring-boot-configuration-processor:2.0.1.RELEASE') 
1
votes

You have to mention in the main class the class you want to use @ConfigurationProperties annotation like below.

@EnableConfigurationProperties(AppProperties.class)

The Property Configuration class with @ConfigurationProperties will be like this

import org.springframework.boot.context.properties.ConfigurationProperties;


@ConfigurationProperties(prefix = "app")
public class AppProperties {
    String name;
    String id;
}


The Main class will be like this

import com.auth2.demo.config.AppProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties(AppProperties.class)
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

0
votes

following works for me:

buildscript {
    repositories {
        jcenter()
        maven { url 'https://repo.jenkins-ci.org/public/' }
        maven { url 'http://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath "io.spring.gradle:propdeps-plugin:0.0.9.RELEASE"
    }
}

...

apply plugin: 'propdeps'
apply plugin: 'propdeps-eclipse'
apply plugin: 'propdeps-idea'

...

dependencyManagement {
    imports {
        mavenBom 'org.springframework.boot:spring-boot-starter-parent:2.0.0.RELEASE'
    }
}

...

dependencies {
    compile "org.springframework.boot:spring-boot-starter"
    compile "org.springframework.boot:spring-boot-starter-actuator"
    annotationProcessor "org.springframework.boot:spring-boot-configuration-processor" // for @ConfigurationProperties, make sure compileJava.dependsOn(processResources)
    ...
}

compileJava.dependsOn(processResources)
0
votes

According to the Spring Boot docs, the correct configuration for Gradle 4.5 and earlier is

dependencies {
    compileOnly group: 'org.springframework.boot', name: 'spring-boot-configuration-processor'
    // ...
}
0
votes

Spring's website has a tutorial that contains a great explanation how to make it work. Just follow the steps under "Configuration properties" section below.

It has one of the steps which none of other answers mentioned here: run ./gradlew kaptKotlin manually.

As of Mar 2021, IDEA will still show the warning on top of the Kotlin properties class, but the properties will be recognized and work. You'll be able to navigate from .properties file to the Kotlin properties class, but not the other way around.

https://spring.io/guides/tutorials/spring-boot-kotlin/ scroll down to "Configuration properties" part.

Or the same page on GitHub:

https://github.com/spring-guides/tut-spring-boot-kotlin/blob/master/README.adoc#configuration-properties