【问题标题】:Springboot Gradle Plugin not working with EclipseSpring Boot Gradle 插件不适用于 Eclipse
【发布时间】:2020-05-25 03:59:07
【问题描述】:

问题

当我使用这个springboot插件springboot-gradle-plugin时,我的java项目没有在Eclipse中运行@
它抛出这个异常

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

【问题讨论】:

    标签: java eclipse spring-boot gradle buildship


    【解决方案1】:

    Gradle 的 Eclipse 支持允许您配置在项目同步或构建时应该运行的任务。它需要 Gradle 5.4 或更高版本以及 Buildship(提供 Gradle 支持的 Eclipse 插件)3.1 或更高版本。

    您可以将bootBuildInfo 配置为在Eclipse 构建项目时运行,方法是将以下内容添加到build.gradle

    eclipse {
        autoBuildTasks bootBuildInfo
    }
    
    

    您可以在this blog post 中了解有关该功能的更多信息。

    【讨论】:

    • 你好。感谢您的回答。每次我使用 Eclipse 更改文件时,autoBuildTasks 都会执行指定的任务。多亏了这个,生成和更新了 build-info.properties。但是,问题是这个文件是在我的类路径之外生成的(这里是 build\resources\main\META-INF\build-info.properties),当我在 IDE 中运行应用程序时不起作用。
    • 您可以通过 Gradle 集成在 IDE 中运行应用程序(使用 bootRun 任务),也可以修改 Eclipse 使用的类路径。
    猜你喜欢
    • 2017-12-17
    • 2018-08-25
    • 2015-06-28
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2022-01-16
    • 2023-04-05
    • 2018-09-29
    相关资源
    最近更新 更多