Java 从 spring-boot:run 获取命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23316843/
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
Get command-line arguments from spring-boot:run
提问by sandris
Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
从命令行启动 spring-boot 应用程序 (mvn spring-boot:run) 时有什么方法可以输入参数,然后在 main() 中获取它们?
采纳答案by geoand
Looking at the source codeof the spring-boot-maven-plugin I found that you need to do:
查看spring-boot-maven-plugin的源码发现你需要这样做:
mvn spring-boot:run -Drun.arguments="arg1,arg2"
Another way to get more information about what options the run
goal of the spring-boot
plugin supports is to execute the following command:
获取有关插件run
目标spring-boot
支持哪些选项的更多信息的另一种方法是执行以下命令:
mvn help:describe -Dcmd=spring-boot:run -Ddetail
For Spring Boot 2.x, the source is hereand you now need to use -Dspring-boot.run.arguments="args1,args2"
对于 Spring Boot 2.x,源代码在这里,您现在需要使用-Dspring-boot.run.arguments="args1,args2"
If you are using Gradle and you want to be able to pass command line arguments to the Gradle bootRun
task, you first need to configure, for example like so:
如果您正在使用 Gradle 并且希望能够将命令行参数传递给 GradlebootRun
任务,则首先需要进行配置,例如像这样:
bootRun {
if ( project.hasProperty('args') ) {
args project.args.split('\s+')
}
}
and run the task using gradle bootRun -Pargs="arg1 arg2"
并使用运行任务 gradle bootRun -Pargs="arg1 arg2"
回答by aakoch
And if you're using Eclipse...
如果您使用的是 Eclipse ......
| Parameter Name | Value | | run.arguments | "--name=Adam" |
回答by Jebuselwyn Martin
When passing multiple arguments using -Drun.arguments, if the argument in turn has 'comma-separated' values, then only the first value of each argument is used. To avoid this repeat the argument as many times as the number of values.
使用 -Drun.arguments 传递多个参数时,如果参数依次具有“逗号分隔”值,则仅使用每个参数的第一个值。为了避免这种情况,重复参数与值的数量一样多。
This is more of a workaround. Not sure if there is an alternative unless the delimiter is different - like '|'.
这更像是一种解决方法。不确定是否有替代方案,除非分隔符不同 - 如“|”。
E.g Issue:
例如问题:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,dev"
Picks only 'test' profile for the above command.
为上述命令仅选择“测试”配置文件。
Workaround:
解决方法:
mvn spring-boot:run -Drun.arguments="--spring.profiles.active=test,--spring.profiles.active=dev"
Picks both 'dev' & 'test' profiles for the above command.
为上述命令选择 'dev' 和 'test' 配置文件。
回答by Anand Rockzz
This is what worked for me (spring-boot v1.4.3.RELEASE
),.
这对我spring-boot v1.4.3.RELEASE
有用(),。
mvn spring-boot:run -Dspring.profiles.active=test,local -Dlogback-debug=true
回答by davidxxx
Be aware : The way of passing arguments depends on the spring-boot
major version.
请注意:传递参数的方式取决于spring-boot
主要版本。
TLDR
TLDR
For Spring Boot 1 :
对于 Spring Boot 1:
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
For Spring Boot 2 :
对于 Spring Boot 2:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
1) spring-boot-maven-plugin
version and the the Spring Boot
version you use has to be aligned.
1)spring-boot-maven-plugin
版本和Spring Boot
您使用的版本必须对齐。
According to the Spring Boot major version used (1
or 2
), the spring-boot-maven-plugin
in the 1
or the 2
version should indeed be used.
If your pom.xml
inherits from the spring-boot-starter-parent
:
根据使用的 Spring Boot 主要版本 (1
或2
),确实应该使用spring-boot-maven-plugin
in1
或2
版本。
如果您pom.xml
继承自spring-boot-starter-parent
:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>ONE_OR_TWO_VERSION</version>
</parent>
In your pom, the version of the plugin used should not even be specified as this plugin dependency is inherited :
在你的 pom 中,甚至不应该指定使用的插件版本,因为这个插件依赖是继承的:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
...
</configuration>
</plugin>
</plugins>
In case of your pom.xml
not inheriting from spring-boot-starter-parent
, don't forget to align the version of spring-boot-maven-plugin
with the exact version of spring boot
you want to use.
如果您pom.xml
不是从 继承spring-boot-starter-parent
,请不要忘记将 的版本spring-boot-maven-plugin
与spring boot
您要使用的确切版本对齐。
2) Passing arguments in command line with spring-boot-maven-plugin:1.X.X
2) 使用spring-boot-maven-plugin:1.XX在命令行中传递参数
For one argument :
对于一个论点:
mvn spring-boot:run -Drun.arguments="argOne"
for multiple :
对于多个:
mvn spring-boot:run -Drun.arguments="argOne,argTwo"
The maven plugin pagedocuments it :
该Maven插件页面文件吧:
Name Type Since Description
arguments | String[] | 1.0 | Arguments that should be passed
to the application. On command line use
commas to separate multiple arguments.
User property is: run.arguments.
3) Passing arguments in command line with spring-boot-maven-plugin:2.X.X
3) 使用spring-boot-maven-plugin:2.XX在命令行中传递参数
For one argument :
对于一个论点:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne"
for multiple :
对于多个:
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
I didn't find the plugin documentation for the 2.X.X version that refers to that.
But the org.springframework.boot.maven.AbstractRunMojo
class of the spring-boot-maven-plugin:2.0.0.M3
plugin refers to this user property:
我没有找到 2.XX 版本的插件文档。
但是插件的org.springframework.boot.maven.AbstractRunMojo
类是spring-boot-maven-plugin:2.0.0.M3
指这个用户属性:
public abstract class AbstractRunMojo extends AbstractDependencyFilterMojo
...
@Parameter(property="spring-boot.run.arguments")
private String[] arguments;
...
protected RunArguments resolveApplicationArguments(){
RunArguments runArguments = new RunArguments(this.arguments);
addActiveProfileArgument(runArguments);
return runArguments;
}
...
}
4) Hint : as you pass more than one argument, whitespaces between commas are considered.
4) 提示:当您传递多个参数时,会考虑逗号之间的空格。
mvn spring-boot:run -Dspring-boot.run.arguments="argOne,argTwo"
will be interpreted as ["argOne", "argTwo"]
将被解释为 ["argOne", "argTwo"]
But this :
但是这个 :
mvn spring-boot:run -Dspring-boot.run.arguments="argOne, argTwo"
will be interpreted as ["argOne", " argTwo"]
将被解释为 ["argOne", " argTwo"]
回答by davidxxx
Spring Boot 1 as 2 provide a way to pass multiple profiles as argument and avoid the issue related to the comma used both as separator between the args and the values passed as active profile.
Spring Boot 1 as 2 提供了一种将多个配置文件作为参数传递的方法,并避免了与用作参数之间的分隔符和作为活动配置文件传递的值的逗号相关的问题。
So instead of writing :
所以而不是写:
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.profiles.active=test,--spring.profiles.active=dev
use the Spring Boot Maven profiles
propertythat is a convenience shortcut of spring.profiles.active
such as the followings :
使用Spring Boot Mavenprofiles
属性,它是以下内容的便捷快捷方式spring.profiles.active
:
The Maven user property is different according to the Spring Boot version.
Maven 用户属性根据 Spring Boot 版本而有所不同。
For Spring Boot 1.4+, that is run.profiles
:
对于 Spring Boot 1.4+,即run.profiles
:
mvn spring-boot:run -Drun.profiles=dev,test
For Spring Boot 2, that is spring-boot.run.profiles
:
对于 Spring Boot 2,即spring-boot.run.profiles
:
mvn spring-boot:run -Dspring-boot.run.profiles=dev,test
From the plugin documentation :
从插件文档:
profiles:
The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles.
Type: java.lang.String[]
Since: 1.3
Required: No
User Property: spring-boot.run.profiles
简介:
要激活的弹簧轮廓。指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行上使用逗号分隔多个配置文件。
类型:java.lang.String[]
自:1.3
要求:否
用户属性:spring-boot.run.profiles
回答by Pratik Shukla
For the latest version of spring use -Dspring-boot.run.arguments=as shown in the example below
对于最新版本的 spring,请使用-Dspring-boot.run.arguments=,如下例所示
spring-boot:run -Djasypt.encryptor.password=temp -Dspring-boot.run.arguments="OU,Grade"
回答by user3157855
As I checked today, the correct usage for Spring Boot 2.2.5 is:
正如我今天检查的那样,Spring Boot 2.2.5 的正确用法是:
mvn spring-boot:run -Dspring-boot.run.arguments="--arg1=value --arg2=value"
Because help says:
因为帮助说:
commandlineArguments User property: spring-boot.run.arguments Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over arguments.
commandlineArguments User property: spring-boot.run.arguments Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over arguments.