135

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).

Jason Law
  • 965
  • 1
  • 9
  • 21
Boris
  • 4,944
  • 7
  • 36
  • 69
  • 2
    What Gradle version do you use? Does [this answer](http://stackoverflow.com/a/42588061/104891) help? – CrazyCoder Mar 16 '17 at 23:56
  • 1
    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. – Boris Mar 17 '17 at 23:10

15 Answers15

76

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.

Joe K
  • 18,204
  • 2
  • 36
  • 58
Icex
  • 776
  • 6
  • 2
44

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
39

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>${project.parent.version}</version>
        </path>
      </annotationProcessorPaths>
    </configuration>
  </plugin>
</plugins>
</build>
anandbibek
  • 1,659
  • 16
  • 20
  • 2
    apparently mapstruct requires that we specifically mention mapstruct in ``annotationProcessorPaths`` , and when we start specifying them manually, we must add spring-boot-configuration-processor, lombok, etc manually as well. At least that's what intellij wanted – anandbibek Oct 11 '20 at 02:36
  • 1
    Thanks for this! Intellij shows a [link to the spring boot docs](https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata-annotation-processor) when it shows the error but there's no mention of configuring annotationProcessorPaths in maven-compiler-plugin – lance-java Aug 11 '21 at 07:07
  • 6
    Please do not use hard-coded version in Maven. Inherit, so you need to change only once when upgrading SpringBoot: ${project.parent.version} – Mick Belker - Pseudonym Dec 21 '21 at 15:13
  • 2
    My parent project is not spring boot parent POM. So that is a bit of YMMV. – anandbibek Dec 23 '21 at 07:51
27

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

JeffersonSousa
  • 501
  • 5
  • 6
20

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.
  • 12
    In my IDEA, (1) is : File | Settings | Build, Execution, Deployment | Compiler | Annotation Processors – quangkid Oct 04 '19 at 01:16
11

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).

Community
  • 1
  • 1
Boris
  • 4,944
  • 7
  • 36
  • 69
  • 2
    Still not working for me. I added the `propdeps-plugin`, the `optional` line and `compileJava.dependsOn(processResources)`, got IntelliJ IDEA 2017.3. Tried running `clean` task in Gradle, then `Refresh all` and `Build`, still nothing. Any idea? – Lamberto Basti Feb 16 '18 at 15:22
10

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

Maybe somebody helps.

Oleg Pavlyukov
  • 151
  • 1
  • 11
7

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"
}
AR1
  • 4,507
  • 4
  • 26
  • 42
7

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")
}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
  • Thanks to your comment I managed to generate the meta file as well, but unfortunately IntelliJ is not picking it up. I found a [related issue in jetbrains bugtracker](https://youtrack.jetbrains.com/issue/KT-15040). Did you manage to get it working? – Marcus Held Jul 08 '20 at 12:32
4

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);
    }

}

Mafei
  • 3,063
  • 2
  • 15
  • 37
2

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') 
aristotll
  • 8,694
  • 6
  • 33
  • 53
vargapeti
  • 301
  • 2
  • 7
  • 4
    For me it was `compileOnly "org.springframework.boot:spring-boot-configuration-processor:$springVersion"` – kshep92 Jul 05 '18 at 21:03
0

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)
Dirk Hoffmann
  • 1,444
  • 17
  • 35
0

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'
    // ...
}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
0

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

Peter
  • 1,512
  • 1
  • 22
  • 40
0

This error can be resolved by adding this annotation in your mainApplicaitonClass

@EnableConfigurationProperties({class name}.class)
Asad
  • 1
  • 2