Java Spring:从命令行覆盖一个 application.property
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37052857/
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
Spring: overriding one application.property from command line
提问by clueless user
I have an application.properties file with default variable values. I want to be able to change ONE of them upon running with mvn spring-boot:run
. I found how to change the whole file, but I only want to change one or two of these properties.
我有一个带有默认变量值的 application.properties 文件。我希望能够在运行时更改其中之一mvn spring-boot:run
。我找到了如何更改整个文件,但我只想更改其中一两个属性。
采纳答案by Andy Wilkinson
You can pass in individual properties as command line arguments. For example, if you wanted to set server.port
, you could do the following when launching an executable jar:
您可以将单个属性作为命令行参数传递。例如,如果您想设置server.port
,您可以在启动可执行 jar 时执行以下操作:
java -jar your-app.jar --server.port=8081
Alternatively, if you're using mvn spring-boot:run
:
或者,如果您正在使用mvn spring-boot:run
:
mvn spring-boot:run -Drun.arguments="--server.port=8081"
You can also configure the arguments for spring-boot:run
in your application's pom.xml
so they don't have to be specified on the command line every time:
您还可以spring-boot:run
在应用程序中配置参数,pom.xml
这样就不必每次都在命令行上指定它们:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<arguments>
<argument>--server.port=8085</argument>
</arguments>
</configuration>
</plugin>
回答by davidxxx
To update a little things, the Spring boot 1.X Maven plugin relies on the --Drun.arguments
Maven user property but the Spring Boot 2.X Maven plugin relies on the -Dspring-boot.run.arguments
Maven user property.
为了更新一些小东西,Spring Boot 1.X Maven 插件依赖于--Drun.arguments
Maven 用户属性,而 Spring Boot 2.X Maven 插件依赖于-Dspring-boot.run.arguments
Maven 用户属性。
So for Spring 2, you need to do :
因此,对于 Spring 2,您需要执行以下操作:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"
And if you need to pass multiple arguments, you have to use ,
as separator and never use whitespace between arguments :
如果您需要传递多个参数,则必须,
用作分隔符,并且切勿在参数之间使用空格:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081,--foo=bar"
About the the maven plugin configuration and the way of passing the argument from a fat jar, it didn't change.
So the very good Andy Wilkinson answer is still right.
关于 Maven 插件配置和从胖 jar 传递参数的方式,它没有改变。
所以非常好的安迪威尔金森答案仍然是正确的。
回答by Durgesh Kumar
In Spring Boot we have provision to override properties as below
在 Spring Boot 中,我们提供了覆盖属性的规定,如下所示
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8082
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8082