9

I'm having problems for IntelliJ to pickup custom spring configuration metadata with Gradle.

If I create a new Spring Boot project with the Initializer, include the Configuration Processor in the dependencies, on the Gradle task set the following tasks,

gradle info

create a class with the content:

package com.example.demo;

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

@Component
@ConfigurationProperties("mycustomconfig")
public class MyCustomConfig {

    private String name;

    public String getName() {
        return name;
    }

    public MyCustomConfig setName(String name) {
        this.name = name;
        return this;
    }
}

then IntelliJ complains in the class file "Spring Boot Configuration Annotation Processor not found in classpath", even though it is definitely on the classpath.

After running the application, there is a file generated in build/classes/java/main/META-INF/spring-configuration-metadata.json with the following content:

{
  "groups": [
    {
      "name": "mycustomconfig",
      "type": "com.example.demo.MyCustomConfig",
      "sourceType": "com.example.demo.MyCustomConfig"
    }
  ],
  "properties": [
    {
      "name": "mycustomconfig.name",
      "type": "java.lang.String",
      "sourceType": "com.example.demo.MyCustomConfig"
    }
  ],
  "hints": []
}

But IntelliJ then complains in application.properties: Cannot resolve configuration property "mycustomconfig.name".

The same experiment works flawlessly with Maven. Is there anything I'm doing wrong?

I'm using IntelliJ 2018.3 Ultimate.

My build.gradle is:

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3 Answers 3

6

Finally, I've found the cause of the issue.

The annotation processor outputs the spring-configuration-metadata.json to build/classes/java/main/META-INF`.

But: IntelliJ uses a different classpath for the resolvement. Going to Project structure/Modules/main module/Paths, you can see that for the compiler output is set to "use module compile output path" and points to out/production/classes. This is resolved from Gradle automatically; changing it will be reverted once you have any changes in Gradle.

I've found that there are two possibilites:

Configure the Spring Boot Annotation Processor manually in IntelliJ Preferences/Build, Execution, Deployment/Compiler/Annotation Processors, with the following settings:

Annotation Processor configuration

This has the benefit, that you do not need to run a complete gradle build - Just compiling from IntelliJ works. Unfortunately, every user in the project seems to manually set this up.

The second possiblity is mentioned at at this Stack overflow question. Set this idea options in Gradle:

idea{
    module{
        inheritOutputDirs = false
        outputDir = compileJava.destinationDir
        testOutputDir = compileTestJava.destinationDir
    }
}

This basically now uses one compiled target class both for IntelliJ as well as Gradle. There seem to be some caveats, though, as mentioned in the linked urls.

2

Not sure if you solved this but I just upgraded to 2018.3 Ultimate and ran into the same problem. I suspect it is an IntelliJ issue but regardless I solved it by adding the following line

implementation('org.springframework.boot:spring-boot-configuration-processor')

Just above the "annotationProcessor" line in my gradle file...

Hope that helps

-2

If you are using Maven, adding this dependency would resolve the issue.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.