Get command-line arguments from spring-boot:run

sandris picture sandris · Apr 26, 2014 · Viewed 77.4k times · Source

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

Answer

geoand picture geoand · Apr 27, 2014

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"

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"