简体   繁体   English

从安装目录(不是当前目录)读取 external.properties 文件

[英]Read external .properties file from the install directory (not the current directory)

I am distributing a Spring Boot application as a zipped "bootJar" using the Gradle Application plugin and the "distZip" task.我使用 Gradle 应用程序插件和“distZip”任务将 Spring 引导应用程序作为压缩的“bootJar”分发。 The end-user will get the zip file, unzip it, and run it by just typing "myApp" (a shell script nicely created by the plugin).最终用户将获得 zip 文件,解压缩并通过键入“myApp”运行它(由插件很好地创建的 shell 脚本)。

I would like the end-user to create a "myapp.properties" file (a name I chose) and put it in the installation directory, or a "config" directory under the installation directory.我希望最终用户创建一个“myapp.properties”文件(我选择的名称)并将其放在安装目录中,或者安装目录下的“config”目录中。

Suppose I set up my embedded (in the jar) application.properties file as follows:假设我设置了我的嵌入式(在 jar 中)application.properties 文件,如下所示:

  • spring.config.import = file:./myapp.properties will only read from the current directory spring.config.import = file:./myapp.properties只会从当前目录读取
  • spring.config.import = file:/etc/myapp.properties will read from the specified directory -- but I don't know what this is at build time (the end-user determines it at installation time ) spring.config.import = file:/etc/myapp.properties将从指定目录读取——但我不知道这是在构建时是什么(最终用户在安装时确定它)

How can I set up my application so that Spring Boot can read properties from an external file whose location is specified later?如何设置我的应用程序,以便 Spring Boot 可以从稍后指定位置的外部文件中读取属性?

NOTE: I know I can play around with the generated scripts to pass in environment variables or Spring Boot properties, but I was hoping to do this completely within Spring Boot so I don't need to modify the nicely generated shell scripts.注意:我知道我可以使用生成的脚本来传递环境变量或 Spring 引导属性,但我希望在 Spring 引导中完全做到这一点,所以我不需要修改生成良好的 Z2591C98B7012445FE6248198B1E 脚本。

spring.config.import = file:./myapp.properties will only read from the current directory spring.config.import = file:/etc/myapp.properties will read from the specified directory -- but I don't know what this is at build time (the end-user determines it at installation time) spring.config.import = file:./myapp.properties 只会从当前目录读取 spring.config.import = file:/etc/myapp.properties 将从指定目录读取——但我不知道这是什么在构建时(最终用户在安装时确定)

Why overcomplicate this.为什么要把这个复杂化。

Place inside the jar all the properties that you want to be statically configured as default values when you build the application.将构建应用程序时希望静态配置为默认值的所有属性放入 jar 内。

Embedded application.properties嵌入式application.properties .properties

 server.port = 8080
 prop1.element = something

Then the client can create another file application.properties and place it in the same directory with the jar and define more properties which are not already defined.然后客户端可以创建另一个文件application.properties并将其放在与 jar 相同的目录中,并定义更多尚未定义的属性。

 prop2.element = something2   
 prop3.element = something3

By default Spring Boot will load properties both from the embedded file as well from the file in the current directory where the jar is placed during startup.默认情况下,Spring 启动会从嵌入文件以及启动期间放置 jar 的当前目录中的文件加载属性。

In the external application.properties you can also overwrite properties existing in the embedded application.properties .在外部application.properties中,您还可以覆盖嵌入式application.properties中存在的属性。 So if the external file in the current directory same as the jar is the following所以如果当前目录下与jar相同的外部文件如下

 prop2.element = something2   
 prop3.element = something3
 prop1.element = something4 <--- this value here will overwrite the value 'something' from embedded file 

According to spring doc根据spring 文档

SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment: SpringApplication 将从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

  1. /config subdirectory of the current directory. /config 当前目录的子目录。

  2. The current directory当前目录

  3. classpath /config package类路径 /config package

  4. The classpath root The list is ordered by类路径根 列表按以下顺序排序

Precedence (properties defined in locations higher in the list override those defined in lower locations ).优先级(在列表中较高位置定义的属性会覆盖在较低位置中定义的属性)。

After having more input from the comments, it seems that you face another issue as well.从评论中获得更多信息后,您似乎还面临另一个问题。 You start the application from command line from another directory so that is counted as the directory where spring will look for the external configuration instead of where the jar is placed.您从另一个目录的命令行启动应用程序,因此该目录被视为 spring 将查找外部配置的目录,而不是 jar 所在的目录。

So for example let's say that the jar is placed inside the target folder that exists in current directory.例如,假设 jar 放置在当前目录中存在的目标文件夹中。 You start the application using the following command:使用以下命令启动应用程序:

java -jar target/demo-0.0.1-SNAPSHOT.jar

But then the external application.properties existing inside target folder is not loaded from spring because you executed the command from another directory.但是,目标文件夹中存在的外部application.properties不会从 spring 加载,因为您从另一个目录执行了命令。 This can be solved if you start the application in the following way如果您按以下方式启动应用程序,则可以解决此问题

java -jar -Dspring.config.additional-location=./target/ target/demo-0.0.1-SNAPSHOT.jar

This should not be difficult as you already provide the path where the jar exists in the command line.这应该不难,因为您已经在命令行中提供了 jar 所在的路径。

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

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