spring-boot之IntelliJ 不获取自己的 spring 配置元数据
我在 IntelliJ 使用 Gradle 获取自定义 Spring 配置元数据时遇到问题。
如果我使用 Initializer 创建一个新的 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;
}
}
然后 IntelliJ 在类文件“Spring Boot Configuration Annotation Processor not found in classpath”中提示,即使它肯定在类路径上。
运行应用程序后,在
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": []
}
但是 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'
}
请您参考如下方法:
最后,我找到了问题的原因。
注释处理器输出 spring-configuration-metadata.json构建/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 编译即可。不幸的是,项目中的每个用户似乎都是手动设置的。
第二种可能性在 at this Stack overflow question 中提到。 .设置此 idea Gradle 中的选项:
idea{
module{
inheritOutputDirs = false
outputDir = compileJava.destinationDir
testOutputDir = compileTestJava.destinationDir
}
}
这基本上现在为 IntelliJ 和 Gradle 使用一个已编译的目标类。不过,如链接网址中所述,似乎有一些警告。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



