Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 226
phospodka Avatar asked Feb 08 '18 00:02

phospodka


People also ask

How do I find application properties in IntelliJ?

Create a properties fileRight-click a directory where you would like to create the file. From the context menu of the target directory, choose New | File. In the New File dialog, type the filename with the corresponding extension . properties, and click OK.

Is application Yml same as application properties?

Unlike properties files, YAML supports multi-document files by design, and this way, we can store multiple profiles in the same file no matter which version of Spring Boot we use. Note: We usually don't want to include both the standard application.


2 Answers

  • Turn the annotation processing on
  • Do not delegate IDE build/run actions to Gradle
  • Rebuild your project in IDE: Build -> Rebuild Project
like image 55
Andrei Ivantsov Avatar answered Oct 23 '22 10:10

Andrei Ivantsov


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
}
like image 1
thyme Avatar answered Oct 23 '22 10:10

thyme