首页 文章

Spring Boot Jar执行内部属性文件

提问于
浏览
0

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

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

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

等等

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

将App打包到可执行JAR并尝试运行它之后,Spring Boot无法识别项目的 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但也失败了...

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

2 回答

  • 0

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

  • 0

    我've found out what'错了 . 我们加载资源的内部框架正在使用 File.separator 来读取资源的路径 .

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

    当我们尝试运行jar时's trying to read the resources with ' '所以构建的路径是(例如我们选择"dev") /C:/Users/MyUser/Projects/MyApp/target/MyApp-1.0.1.jar!/BOOT-INF/classes!/ conf\dev 这就是应用程序无法加载的原因 .

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

相关问题