问题
当我使用这个 springboot 插件时,我的 java 项目无法在 Eclipse 中运行 springboot-gradle-plugin
它抛出这个异常
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[2020-02-09 15:45:27.825] - 12256 GRAVE [main] --- org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:
***************************
APPLICATION FAILED TO START
***************************
Description:
Constructor in com.example.demo.VersionController required a bean of type 'org.springframework.boot.info.BuildProperties' that could not be found.
- Bean method 'buildProperties' in 'ProjectInfoAutoConfiguration' not loaded because @ConditionalOnResource did not find resource '${spring.info.build.location:classpath:META-INF/build-info.properties}'
存在问题的示例项目
可以在此处找到存在此问题的示例存储库:springboot-gradle-plugin-issue
要查看此问题,您需要使用 Eclipse 运行此项目(我使用 eclipse 2019-09 运行它)
这个项目的用途
这个java项目使用gradle和这个插件spring-boot-gradle-plugin .
该项目在控制台中打印我的 build.gradle 文件中声明的应用程序版本。
在我的 gradle.build 文件中,我包含以下几行:
springBoot {
buildInfo()
}
这行代码所做的就是将一个名为“bootBuildInfo”的 Gradle 任务添加到 gradle 中,并在运行此任务时创建此文件META-INF/build-info.properties>.
在Java中,运行App时,springboot会自动加载并读取META-INF/build-info.properties来创建bean。
Eclipse 的问题
当我在终端中使用 gradle 构建并运行生成的 jar 文件时,所有这些都有效,但当我通过 Eclipse 运行我的应用程序时,它不起作用。
它不会创建文件 META-INF/build-info.properties,当 springboot 尝试加载它时,会抛出一个 bean not found 异常,因为它找不到该文件。
找到解决方法
如果我执行以下操作之一,该项目将运行:
- 在 src/main/resources 文件夹下手动创建 META-INF/build-info.properties
- 在 build/resources/main 文件夹下手动创建 META-INF/build-info.properties
这些方法都不需要,因为它不会自动更新 build-info.properties
最佳答案
Gradle 的 Eclipse 支持允许您配置在同步或构建项目时应运行的任务。它需要 Gradle 5.4 或更高版本以及 Buildship(提供 Gradle 支持的 Eclipse 插件)3.1 或更高版本。
您可以通过将以下内容添加到 build.gradle
来配置 bootBuildInfo
在 Eclipse 构建项目时运行:
eclipse {
autoBuildTasks bootBuildInfo
}
您可以在this blog post中了解有关该功能的更多信息。 .
关于java - Spring Boot Gradle 插件无法与 Eclipse 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60140145/