spring-boot - 使用 Gradle 在 Spring Boot 项目中自定义属性

标签 spring-boot gradle

在我使用 gradle 构建的 Spring Boot 项目 (1.5.6.RELEASE) 中,我想包含一些自定义属性。 documentation 中列出的方法似乎不起作用(在构建时,我得到:Could not set unknown property 'additional' for task ':properties' of type org.gradle.api.tasks.diagnostics.PropertyReportTask.):

springBoot {
    buildInfo {
        properties {
            additional = [
                'a': 'alpha',
                'b': 'bravo'
            ]
        }
    }
}

幸运的是我发现了这种方法here ,对我有用(没有编译错误,然后我可以从我的代码访问该属性):

springBoot{
    buildInfo {
        additionalProperties = [
            'testpropertykey': 'testpropertyvalue'
        ]
    }
}

但是,由于前者是“官方”记录的方法,我更愿意采用这种方法。我将如何使前一种方法起作用?我假设我遗漏了一些东西——除非文档有误,或者这可能是从 Spring Boot 1.5.6.RELEASE 更改而来的。

最佳答案

您链接的文档适用于插件的当前版本,该版本与 Spring Boot 的当前 GA 版本一致:2.1.7

插件的 1.5.x 版确实有一个 additionalProperties 字段:https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/buildinfo/BuildInfo.java#L66

2.1.x 版本没有,您使用 properties 代替:https://github.com/spring-projects/spring-boot/blob/2.1.x/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfo.java#L45

建议您迁移/升级到 Spring Boot 2.1.x 或 2.2.x,因为 1.5.x 已经达到 EOL:https://spring.io/blog/2018/07/30/spring-boot-1-x-eol-aug-1st-2019

关于spring-boot - 使用 Gradle 在 Spring Boot 项目中自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57497693/

相关文章:

java - Spring Boot 多上下文 application.properties 问题

android - 关于同级子模块的资源,Gradle 项目

java - 为什么Gradle任务未按正确顺序运行

java - 我可以使用 gradle 检索测试数据依赖项吗?

java - 使用spring data从Neo4j查询数据始终返回null

Spring Boot jetty/tomcat 内嵌访问日志配置

java - Spring Data MongoDB - 使用自定义 Id 字段时,注释 @CreatedDate 不起作用

java - 如何使用不同的配置值运行同一个 jhipster war 文件的相同角色的多个实例

react-native - react 原生构建失败 react-native-fbsdk

android - 从 Eclipse 迁移...错误 :(1, 0) 找不到 ID 为 'com.android.application' 的插件