Spring 引导 Jar 执行内部属性文件
Spring Boot Jar Execution internal properties files
我有一个 Spring 引导应用程序,当我从 Intellij 运行 作为 maven 配置时,它 运行 是正确的。
我有一个使用属性文件定义的环境项目结构。
resources/conf/dev/environment.properties
resources/conf/qa/environment.properties
resources/conf/general.properties
等等
我们的框架以我们选择带有 VM 参数的环境的方式工作。
例如 -Denv=dev
或 -Denv=qa
将应用程序打包为可执行 JAR 并尝试 运行 后,Spring 启动无法识别项目 conf
路径下的属性文件。
当我查看 JAR 内部时,属性文件位于 {jar-name}.jar\BOOT-INF\classes\conf
.
下
错误是:
java.io.FileNotFoundException: conf\general.properties (The system cannot find the path specified)
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)
我尝试使用此处的文档: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 但也失败了...
指南将属性文件称为 "external",但我的属性打包在 JAR 中。
尝试将属性文件放入资源中。
不在资源/conf 中。
效果很好。
我发现问题所在了。
我们正在加载资源的内部框架正在使用 File.separator
来读取资源的路径。
现在由于某些原因,当使用 maven spring 引导插件创建 jar 时,类路径是用 '/' 构建的,例如 /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/
(资源在 类 路径里面当然是罐子)
当我们尝试 运行 jar 时,它会尝试使用 '\' 读取资源,因此构建的路径是(例如,如果我们选择 "dev")/C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/
conf\dev 这就是应用无法加载的原因。
我仍然不知道为什么会这样。
我有一个 Spring 引导应用程序,当我从 Intellij 运行 作为 maven 配置时,它 运行 是正确的。
我有一个使用属性文件定义的环境项目结构。
resources/conf/dev/environment.properties
resources/conf/qa/environment.properties
resources/conf/general.properties
等等
我们的框架以我们选择带有 VM 参数的环境的方式工作。
例如 -Denv=dev
或 -Denv=qa
将应用程序打包为可执行 JAR 并尝试 运行 后,Spring 启动无法识别项目 conf
路径下的属性文件。
当我查看 JAR 内部时,属性文件位于 {jar-name}.jar\BOOT-INF\classes\conf
.
错误是:
java.io.FileNotFoundException: conf\general.properties (The system cannot find the path specified)
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)
我尝试使用此处的文档: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 但也失败了...
指南将属性文件称为 "external",但我的属性打包在 JAR 中。
尝试将属性文件放入资源中。 不在资源/conf 中。 效果很好。
我发现问题所在了。
我们正在加载资源的内部框架正在使用 File.separator
来读取资源的路径。
现在由于某些原因,当使用 maven spring 引导插件创建 jar 时,类路径是用 '/' 构建的,例如 /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/
(资源在 类 路径里面当然是罐子)
当我们尝试 运行 jar 时,它会尝试使用 '\' 读取资源,因此构建的路径是(例如,如果我们选择 "dev")/C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/
conf\dev 这就是应用无法加载的原因。
我仍然不知道为什么会这样。