Springboot Gradle 插件不能与 Eclipse 一起使用



THE PROBLEM

当我使用这个 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 中,当运行应用程序时,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
}

您可以在这篇博文中了解有关该功能的更多信息。

最新更新