Spring Boot Jar执行内部属性文件

时间:2023-01-13 12:59:41

I have a Spring Boot application that runs properly when I run it from Intellij as maven configurations.

当我从Intellij作为maven配置运行它时,我有一个正常运行的Spring Boot应用程序。

I have a project structure of environments that is defined with properties files.

我有一个用属性文件定义的环境项目结构。

resources/conf/dev/environment.properties
resources/conf/qa/environment.properties
resources/conf/general.properties

etc.

Our framework works in a way that we are choosing the env with VM arguments. for example -Denv=dev or -Denv=qa

我们的框架以我们选择带有VM参数的env的方式工作。例如-Denv = dev或-Denv = qa

After packaging the App to an executable JAR and trying to run it, Spring Boot can't identify the properties files under conf path of the project.

在将App打包到可执行JAR并尝试运行它之后,Spring Boot无法识别项目的conf路径下的属性文件。

When I look inside the JAR the properties files are under {jar-name}.jar\BOOT-INF\classes\conf.

当我查看JAR时,属性文件位于{jar-name} .jar \ BOOT-INF \ classes \ conf下。

The error is:

错误是:

java.io.FileNotFoundException: conf\general.properties (The system cannot find the path specified)

java.io.FileNotFoundException:conf \ general.properties(系统找不到指定的路径)

2017-07-11 10:52:52.864  INFO 17896 --- [main] n.lifecycle: Can't find configuration file [conf\general.properties]
2017-07-11 10:52:52.865 ERROR 17896 --- [main] n.lifecycle: configuration file [null] not found (use default properties as error handling)

I've tried to work with the documentaion here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html but nothing seems to fix it. Also tried to work with this guide - http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#executable-jar-launching but also failed...

我试图在这里使用文档:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html但似乎没有任何解决方法。还尝试使用本指南 - http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#executable-jar-launching但也失败了......

The guides are referring to the properties files as "external" but my properties are packed in the JAR.

指南将属性文件称为“外部”,但我的属性包含在JAR中。

2 个解决方案

#1


0  

Try to put properties files in resources. Not in resources / conf. It works so well.

尝试将属性文件放在资源中。不在资源/ conf。它运作良好。

#2


0  

I've found out what's wrong. Our in-house framework that's loading the resources are using File.separator to read the path of the resources.

我发现了什么是错的。我们加载资源的内部框架正在使用File.separator来读取资源的路径。

Now for some reason when the jar is created with maven spring boot plugin the classpath is built with '/' for example /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/ (the resources are in classes path inside of the jar of course)

现在出于某种原因,使用maven spring boot插件创建jar时,类路径使用'/'构建,例如/C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/ BOOT-INF / classes!/(资源在jar里面的类路径中)

And when we are trying to run the jar it's trying to read the resources with '\' so the path that is built is (if we choose "dev" for example) /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/conf\dev and that is why the app is failing to load.

当我们尝试运行jar时,它试图用'\'读取资源,因此构建的路径是(例如我们选择“dev”)/ C:/ Users / MyUser / Projects / MyApp / target / MyApp-1.0.1.jar!/ BOOT-INF / classes!/ conf \ dev这就是应用程序无法加载的原因。

I still don't know why this is happening.

我仍然不知道为什么会这样。

#1


0  

Try to put properties files in resources. Not in resources / conf. It works so well.

尝试将属性文件放在资源中。不在资源/ conf。它运作良好。

#2


0  

I've found out what's wrong. Our in-house framework that's loading the resources are using File.separator to read the path of the resources.

我发现了什么是错的。我们加载资源的内部框架正在使用File.separator来读取资源的路径。

Now for some reason when the jar is created with maven spring boot plugin the classpath is built with '/' for example /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/ (the resources are in classes path inside of the jar of course)

现在出于某种原因,使用maven spring boot插件创建jar时,类路径使用'/'构建,例如/C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/ BOOT-INF / classes!/(资源在jar里面的类路径中)

And when we are trying to run the jar it's trying to read the resources with '\' so the path that is built is (if we choose "dev" for example) /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/conf\dev and that is why the app is failing to load.

当我们尝试运行jar时,它试图用'\'读取资源,因此构建的路径是(例如我们选择“dev”)/ C:/ Users / MyUser / Projects / MyApp / target / MyApp-1.0.1.jar!/ BOOT-INF / classes!/ conf \ dev这就是应用程序无法加载的原因。

I still don't know why this is happening.

我仍然不知道为什么会这样。