phospodka
phospodka

Reputation: 1028

How to get Intellij to recognize properties in application.yml

I am trying to get Intellij to recognize my properties using gradle. I have followed the steps here. So this means I have a @ConfigurationProperties annotated class with some properties.

I added the spring dependency to process them:

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

compileJava.dependsOn(processResources)

I added the plugin (I've tried not using the plugin and just making it a compile dependency, no change)

buildscript {
    repositories {
        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-maven'
apply plugin: 'propdeps-idea'

When I run the build, I see a build/classes/java/main/META-INF/spring-configuration-metadata.json file is created based off of my properties. When I try to use the property in either application.yml or application.properties, Intellij says it cannot resolve it.

The docs does say it should be called additional-spring-configuration-metadata.json and may expect it to be called that to process it, but I do not see a way to make the build name it that way nor configure Intellij to expect otherwise.

Has anyone got this working with gradle? Or is this a bug.

Edit I created a repo with a pair of projects to demonstrate this. One for gradle and one for maven. I created the projects from start.spring.io and basically just added the properties configuration. I also used a straight compile dependency in both cases instead of optional / compileOnly.

I had not confirmed this before, but the code assist does work for maven, but not gradle. Both create a spring-configuration-metadata.json in the META-INF in their respective build folders. I am not exactly sure who is not picking it up.

Misc relevant versions

Intellij: 2017.3.4
Springboot: 1.5.9
Gradle: 4.4.1
Java: 8.161

Upvotes: 18

Views: 30370

Answers (3)

geneSummons
geneSummons

Reputation: 925

This answer is a combination of the (at this time) other two answers with a minor twist. In my case this is what "fixed" the issue: (in 2019.03.01-Ultimate)

  • Turn on the default annotation processing (File>Settings>Build, Execution, Deployment>Annotation Processors>Enable Annotation processing

    • Select Obtain processors from project classpath
    • Select Store generated sources relative to module output directory
    • keep other defaults
    • click OK
  • Add the code in @thyme's answer to your build.gradle

    • EXCEPT instead of into "out/production/classes"
    • use: into "build/generated/sources/annotationProcessor"

Now you should be able to run gradle clean/build and Intellij should be able to find your "additional metadata" definitions.

Notice that even though the build.gradle code doesn't explicitly mention 'additional-spring-configuration-metadata.json', it is exactly that "additional metadata" that ends up in the annotationProcessor folder as "spring-configuration-metatdata.json" where Intellij finds it.

EDIT: Also note, you need to clean / rebuild after adding any new "additional metadata" items before Intellij will see the new entries (in the freshly regenerated file).

Upvotes: 0

thyme
thyme

Reputation: 480

As far as I can tell, IntelliJ (at the time of this writing, 2018.1.2) wants the spring-configuration-metadata.json file to either be in a main source root (src/main/resources/META-INF/ or src/main/java/META-INF/) or in its default output directory for it to pick it up for autocompletion of properties in your source tree. To expand on phospodka's comment, you can add something like this to your build.gradle to satisfy IntelliJ.

task copyConfigurationMetadata(type: Copy) {
    from(compileJava) {
        include 'META-INF/spring-configuration-metadata.json'
    }
    into "out/production/classes"
}
compileJava {
    dependsOn processResources
    finalizedBy copyConfigurationMetadata
}

Upvotes: 2

Andrei Ivantsov
Andrei Ivantsov

Reputation: 121

Upvotes: 7

Related Questions