Java jar 文件之外的 application.properties 如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39427675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:08:09  来源:igfitidea点击:

application.properties outside jar file how to

javaspring-bootclasspath

提问by Gibi

As stated in spring-boot-reference:

spring-boot-reference 中所述

On your application classpath (e.g. inside your jar) you can have an application.properties that provides a sensible default property value for name. When running in a new environment, an application.properties can be provided outside of your jar that overrides the name

在您的应用程序类路径上(例如在您的 jar 中),您可以拥有一个 application.properties,它为 name 提供一个合理的默认属性值。在新环境中运行时,可以在 jar 之外提供覆盖名称的 application.properties

I place a duplicated application.properties with overridden name on the same path as jar file, however when running the application with:

我在与 jar 文件相同的路径上放置了一个具有覆盖名称的重复 application.properties,但是在运行应用程序时:

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

The name value is not overridden, it still refers to the one inside application.properties inside jar file. I also tried:

name 值没有被覆盖,它仍然指的是 jar 文件中 application.properties 中的那个。我也试过:

java -Dspring.config.location=/target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar

But it does not work, please help.

但它不起作用,请帮助。

Edit

编辑

When I change the current directory to targetand run it, it works.

当我将当前目录更改为target并运行它时,它可以工作。

java -jar myproject-0.0.1-SNAPSHOT.jar

Why? Why cannot be outside the path and run it?

为什么?为什么不能在路径之外运行它?

采纳答案by alexbt

It doesn't work because you are trying to launch the jar from another folder: spring boot looks for files/folder relative your current folder.

它不起作用,因为您正在尝试从另一个文件夹启动 jar:spring boot 查找相对于您当前文件夹的文件/文件夹。

You can:

你可以:

1) copy application.propertieseither in ./or ./config/, relative to your current folder.

1)相对于您的当前文件夹,application.properties./或 中复制./config/

2) Or specify -Dspring.config.location:

2) 或指定-Dspring.config.location

$ java -Dspring.config.location=target/application.properties -jar target/myproject-0.0.1-SNAPSHOT.jar

回答by Magnus

You spelt configas conig, should work if you spell it right.

您拼写configconig,如果拼写正确,应该可以使用。