Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get command-line arguments from spring-boot:run

Is there any way to input arguments when launching spring-boot application (mvn spring-boot:run) from commandline and then get them in main()?

like image 317
sandris Avatar asked Apr 26 '14 21:04

sandris


People also ask

How do I grab command line arguments?

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.


1 Answers

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"

like image 78
geoand Avatar answered Sep 21 '22 23:09

geoand