IntelliJ 不获取自己的 spring 配置元数据

Hel*_*orm 9 intellij-idea gradle spring-boot

我在 IntelliJ 使用 Gradle 获取自定义 Spring 配置元数据时遇到问题。

如果我使用初始化程序创建一个新的 Spring Boot 项目,在依赖项中包含配置处理器,在 Gradle 任务上设置以下任务,

等级信息

创建一个包含以下内容的类:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后 IntelliJ 在类文件中抱怨“在类路径中找不到 Spring Boot 配置注释处理器”,即使它肯定在类路径上。

运行应用程序后,会生成一个包含build/classes/java/main/META-INF/spring-configuration-metadata.json以下内容的文件:

{
  "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": []
}
Run Code Online (Sandbox Code Playgroud)

但 IntelliJ 然后在 application.properties 中抱怨:Cannot resolve configuration property "mycustomconfig.name".

同样的实验在 Maven 上完美运行。我做错了什么吗?

我正在使用 IntelliJ 2018.3 Ultimate。

我的 build.gradle 是:

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'
}
Run Code Online (Sandbox Code Playgroud)

Hel*_*orm 6

最后,我找到了问题的原因。

注释处理器输出spring-configuration-metadata.json到 build/classes/java/main/META-INF`。

但是:IntelliJ 使用不同的类路径来解决。转到项目结构/模块/主模块/路径,您可以看到编译器输出设置为“使用模块编译输出路径”并指向out/production/classes. 这是由 Gradle 自动解决的;一旦您在 Gradle 中进行任何更改,更改就会恢复。

我发现有两种可能性:

在 IntelliJ Preferences/Build, Execution, Deployment/Compiler/Annotation Processors 中手动配置 Spring Boot Annotation Processor,设置如下:

注释处理器配置

这样做的好处是,您不需要运行完整的 gradle 构建 - 只需从 IntelliJ 进行编译即可。不幸的是,项目中的每个用户似乎都是手动设置的。

第二种可能性在 Stack Overflow Question 中提到。在 Gradle 中设置此idea选项:

idea{
    module{
        inheritOutputDirs = false
        outputDir = compileJava.destinationDir
        testOutputDir = compileTestJava.destinationDir
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,IntelliJ 和 Gradle 基本上都使用一个已编译的目标类。不过,正如链接网址中提到的,似乎有一些警告。


Jim*_* M. 2

不确定你是否解决了这个问题,但我刚刚升级到 2018.3 Ultimate 并遇到了同样的问题。我怀疑这是一个 IntelliJ 问题,但无论如何我通过添加以下行解决了它

implementation('org.springframework.boot:spring-boot-configuration-processor')
Run Code Online (Sandbox Code Playgroud)

就在我的 gradle 文件中的“annotationProcessor”行上方...

希望有帮助