从spring-boot:run获取命令行参数 [英] Get command-line arguments from spring-boot:run
问题描述
从命令行启动spring-boot应用程序(mvn spring-boot:run)然后在main()中获取它们是否有任何方法可以输入参数?
Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
推荐答案
查看源代码我发现你需要做的:
Looking at the source code of the spring-boot-maven-plugin I found that you need to do:
mvn spring-boot:run -Drun.arguments="arg1,arg2"
另一种获取运行
目标的更多信息的方法 spring-boot
插件支持是执行以下命令:
Another way to get more information about what options the run
goal of the spring-boot
plugin supports is to execute the following command:
mvn help:describe -Dcmd=spring-boot:run -Ddetail
对于Spring Boot 2.x,源是< a href =https://github.com/spring-projects/spring-boot/ blob / 68c6860d88b69756ffd0dff76c64e3e29c29ab68 / spring-boot-project / spring-boot-tools / spring-boot-maven-plugin / src / main / java / org / springframework / boot / maven / AbstractRunMojo.java#L113rel =noreferrer>这里你现在需要使用 -Dspring-boot.run.arguments =args1,args2
For Spring Boot 2.x, the source is here and you now need to use -Dspring-boot.run.arguments="args1,args2"
如果您正在使用Gradle,并且希望能够将命令行参数传递给Gradle bootRun
任务,则首先需要进行配置,例如: / p>
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:
bootRun {
if ( project.hasProperty('args') ) {
args project.args.split('\\s+')
}
}
和使用 gradle bootRun -Pargs =arg1 arg2
这篇关于从spring-boot:run获取命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!