【问题标题】:How to pass a compile-time Maven defined property to a spring-boot application?如何将编译时 Maven 定义的属性传递给 spring-boot 应用程序?
【发布时间】:2016-10-21 09:51:24
【问题描述】:

当我使用战争插件时,我可以做到:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0.2</version>
        <configuration>
          <archive>
            <manifest>
              <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
            </manifest>
            <manifestEntries>
              <Implementation-Build>${buildNumber}</Implementation-Build>
            </manifestEntries>
          </archive>
        </configuration>
      </plugin>

在我的 src/main/resources 中,我有一个文件 (build.properties) 将其映射到 Spring Environment 属性:

build.revision=${buildNumber}

有没有办法通过 spring-boot-maven-plugin 实现这一点?

【问题讨论】:

标签: maven spring-boot


【解决方案1】:

Using spring-boot-starter-parent 给你(根据文档)

对 application.properties 和 application.yml 进行合理的资源过滤,包括特定于配置文件的文件(例如 application-foo.properties 和 application-foo.yml)”。由于默认配置文件接受 Spring 样式占位符(${...​} ) Maven 过滤更改为使用 @..@ 占位符(您可以使用 Maven 属性 resource.delimiter 覆盖它)。

因此,application.properties 中的 @buildNumber@ 可能对您有用。

我不确定“合理过滤”如何适用于其他(非应用程序配置)资源文件(例如您的 build.properties)。也可以在那里试试。


纯 Maven 选项:

您可以opt in to the Maven resource filtering:

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

这将重写您的 ${variable} 表达式。

如果您有不应该接受这种处理的文件(例如那些包含运行时占位符或二进制文件的文件),您还可以设置一个单独的resources-filtered 目录。

【讨论】:

  • 是的,将 ${___} 替换为 @___@ 效果很好,谢谢!完全错过了文档的那部分......
  • 作为记录,在 Spring Boot 1.4 中,您将拥有 support for a dedicated build info task。这适用于 Maven 和 Gradle。
猜你喜欢
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
  • 2020-12-30
  • 2022-12-17
  • 1970-01-01
  • 2020-04-07
  • 2020-12-15
  • 2014-08-24
相关资源
最近更新 更多