简体   繁体   English

Spring FAT 外的引导属性 JAR

[英]Spring Boot Properties Outside FAT JAR

I'm working on a project and have a use case where I need to provide application.properties file for Spring Boot from outside the JAR .我正在开发一个项目并且有一个用例,我需要为 Spring 从JAR外部启动提供application.properties文件。

According to Baeldung , the priority order for picking up the application.properties is根据Baeldung的说法,获取 application.properties 的优先顺序是

  1. A /config subdirectory of the current directory当前目录的 /config 子目录
  2. The current directory当前目录
  3. A classpath /config package类路径 /config package
  4. The classpath root类路径根

The issue with first two is I'll need to navigate to the directory containg the configs to run the JAR.前两个的问题是我需要导航到包含配置的目录才能运行 JAR。 It sounds no issue when working on local but wont be a feasible solution when deploying on remote hosts as through CI/CD frameworks.在本地工作时听起来没有问题,但在通过 CI/CD 框架部署在远程主机上时,这不是一个可行的解决方案。

I'm trying to find a mechanism using classpaths and avoid using spring boot's command line options mentioned over here or setting up environment variables.我正在尝试使用类路径找到一种机制,并避免使用此处提到的 spring 引导的命令行选项或设置环境变量。

I'm unable to figure out how to setup classpath while running FAT JAR and specify configs all together.我无法弄清楚如何在运行 FAT JAR 时设置类路径并一起指定配置。 If you can, please help me figure it out!如果可以的话,请帮我弄清楚!

Thanks in advance:)提前致谢:)

EDIT : I understand there are ways to achieve this using Spring Boot's command line options such as spring.config or loader.path etc.编辑:我知道有一些方法可以使用 Spring Boot 的命令行选项来实现这一点,例如 spring.config 或 loader.path 等。

I was trying to find a more implicit solution based on classpath and directory structures only to make it less coupled with the fact that Spring Boot is being used.我试图找到一个基于类路径和目录结构的更隐含的解决方案,只是为了减少它与正在使用 Spring 引导这一事实的耦合。

According to the Spring docs , you can define external config locations using the spring.config.location property.根据Spring 文档,您可以使用spring.config.location属性定义外部配置位置。 More specifically:进一步来说:

If spring.config.location contains directories (as opposed to files), they should end in / (and, at runtime, be appended with the names generated from spring.config.name before being loaded, including profile-specific file names).如果spring.config.location包含目录(而不是文件),它们应该以 / 结尾(并且,在运行时,在加载之前附加从spring.config.name生成的名称,包括特定于配置文件的文件名)。 Files specified in spring.config.location are used as-is, with no support for profile-specific variants, and are overridden by any profile-specific properties. spring.config.location中指定的文件按原样使用,不支持特定于配置文件的变体,并被任何特定于配置文件的属性覆盖。

Config locations are searched in reverse order.配置位置以相反的顺序搜索。 By default, the configured locations are:默认情况下,配置的位置是:

classpath:/,classpath:/config/,file:./,file:./config/.

The resulting search order is the following:生成的搜索顺序如下:

file:./config/ file:./ classpath:/config/ classpath:/

When custom config locations are configured by using spring.config.location , they replace the default locations.当使用spring.config.location配置自定义配置位置时,它们会替换默认位置。 For example, if spring.config.location is configured with the value例如,如果spring.config.location配置为

classpath:/custom-config/,file:./custom-config/ the search order becomes the following: classpath:/custom-config/,file:./custom-config/搜索顺序变成如下:

file:./custom-config/ classpath:custom-config/

Alternatively, when custom config locations are configured by using spring.config.additional-location , they are used in addition to the default locations.或者,当使用spring.config.additional-location配置自定义配置位置时,除了默认位置之外,还会使用它们。 Additional locations are searched before the default locations.在默认位置之前搜索其他位置。 For example, if additional locations of classpath:/custom-config/,file:./custom-config/ are configured, the search order becomes the following:例如,如果配置了classpath:/custom-config/,file:./custom-config/的附加位置,则搜索顺序如下:

file:./custom-config/ classpath:custom-config/ file:./config/ file:./ classpath:/config/ classpath:/

An example usage for a directory containing your external configi would look like:包含您的外部配置的目录的示例用法如下所示:

java -jar myproject.jar --spring.config.location=file:/custom-config-dir/

Or directly to an external config file:或者直接到外部配置文件:

java -jar myproject.jar --spring.config.location=file:/custom-config-dir/custom-config.properties

Specify custom config location as VM argument is another option.将自定义配置位置指定为 VM 参数是另一种选择。

java -Dspring.config.location=<config-dir-path> -jar demo.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM