Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.
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"
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
For Spring Boot 2.x, the source is here and you now need to use -Dspring-boot.run.arguments="args1,args2"
(edit from april 2021) For Spring Boot 2.2+, you now need to use -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:
bootRun { if ( project.hasProperty('args') ) { args project.args.split('\\s+') } }
and run the task using gradle bootRun -Pargs="arg1 arg2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With