IntelliJ不会获取自己的Spring配置元数据 [英] IntelliJ does not pick up own spring configuration metadata

查看:148
本文介绍了IntelliJ不会获取自己的Spring配置元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了IntelliJ使用Gradle获取自定义弹簧配置元数据的问题.

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

如果我使用初始化程序创建一个新的Spring Boot项目,请将Configuration Processor包含在依赖项中,在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,

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

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

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

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

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

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": []
}

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

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

同一实验在Maven上可以完美地工作.我在做错什么吗?

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

我正在使用IntelliJ 2018.3 Ultimate.

I'm using IntelliJ 2018.3 Ultimate.

我的build.gradle是:

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'
}

推荐答案

最后,我已经找到了问题的原因.

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

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

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

但是:IntelliJ使用不同的类路径进行解析.转到项目结构/模块/主模块/路径,您可以看到将编译器输出设置为使用模块编译输出路径",并指向out/production/classes.这是从Gradle自动解决的;更改后,只要您对Gradle进行了任何更改,该设置都将恢复.

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.

我发现有两种可能性:

在IntelliJ Preferences/Build,Execution,Deployment/Compiler/Annotation Processors中手动配置Spring Boot注释处理器,并进行以下设置:

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

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

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.

第二个可能性在此堆栈溢出问题的中提到.在Gradle中设置以下idea选项:

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

现在,这基本上为IntelliJ和Gradle使用了一个已编译的目标类.但是,如链接的URL中所述,似乎有些警告.

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.

这篇关于IntelliJ不会获取自己的Spring配置元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆