【问题标题】:Spring Boot Jar Execution internal properties filesSpring Boot Jar 执行内部属性文件
【发布时间】:2017-12-15 07:20:47
【问题描述】:

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

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

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

等等

我们的框架以我们选择带有 VM 参数的环境的方式工作。 例如-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 中。

【问题讨论】:

  • 你应该有:resources/conf/application-dev.properties resources/conf/application-ga.properties resources/conf/application.properties 我认为这是春天期望找到的
  • 我不认为这是问题所在。当我在使用“spring-boot:run”打包之前运行项目时,它工作正常。更改文件的名称将破坏我们用于管理属性的内部框架。
  • 您从 IDE 运行特定配置?或者只是正确地点击 spring boot 主类?
  • 没有右键。命令行有 spring-boot:run -Denv=dev 的 Maven 运行配置

标签: java maven spring-boot executable-jar properties-file


【解决方案1】:

尝试将属性文件放入资源中。 不在资源/conf中。 效果很好。

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-03
      • 2012-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2019-06-12
      • 2017-12-27
      • 2018-02-21
      • 2015-06-26
      相关资源
      最近更新 更多